2013-08-26 98 views
2

我有一個JSON對象從我使用ng-repeatorderBy: 'name'建設複選框建設複選框過濾器:採用NG-重複

<div ng-controller="Ctrl"> 
    <ul> 
     <li ng-repeat="fruit in fruits | orderBy:'name'"> 
      <label> 
       <input type="checkbox" name="{{fruit.name}}" id="{{fruit.name}}"> 
       {{fruit.name}} 
      </label> 
     </li> 
    <ul> 
</div> 

現在,我想我的項目首先通過「檢查」,然後按字母順序排序。

這裏有一個小提琴:http://jsfiddle.net/DqfTw/67/

+0

「現在我想檢查顯示在上面,然後按字母順序列出」 - 這是唯一有用的句子,但你使它變得模糊。改寫它。 – zsong

+0

@sza OP想要首先按'checked'進行排序,然後按字母順序排序(假設爲'value')。 –

回答

2

你應該在你的數據中有一個額外的狀態欄,默認爲false(未選中),或者你需要。然後將其用於ng-model 您的數據應如下所示。

$scope.fruits = 
      [{'name':'apple',status:false}, 
     {'name':'Mary',status:false}, 
     {'name':'Mike',status:false}, 
     {'name':'Adam',status:false}, 
     {'name':'Julie',status:false}] 

    } 

那麼你的HTML將作爲

<ul> 
<li ng-repeat="fruit in fruits | orderBy:['!status','name']"> 
    <label><input type="checkbox" name="{{fruit.name}}" ng-model="fruit.status" >{{fruit.name|uppercase}}</label></li> 
<ul> 

檢查下面的小提琴鏈接。這個完美的作品爲u需要, http://jsfiddle.net/DqfTw/90/

@Mickael甚至更新的一個不完美,

1

您可以在對象上綁定一個檢查屬性,那麼你的排序依據子句中使用這樣的:

<div ng-controller="Ctrl"> 
    <ul> 
     <li ng-repeat="fruit in fruits | orderBy:['checked', 'name']"> 
      <label><input type="checkbox" name="{{fruit.name}}" id="{{fruit.name}}" ng-model="fruit.checked">{{fruit.name}}</label> 
     </li> 
    <ul> 
</div> 

你的小提琴是最新的。

編輯:因爲undefined並不嚴格等於false,所以當您選中並取消選中複選框時,您可以按順序出現錯誤。您可以初始化每個數據的檢查屬性或使用自定義函數檢查屬性(我認爲自定義函數更好)。 下面是代碼:

HTML:

<div ng-controller="Ctrl"> 
    <ul> 
     <li ng-repeat="fruit in fruits | orderBy:[checkedFunction, 'name']"> 
      <label><input type="checkbox" name="{{fruit.name}}" id="{{fruit.name}}" ng-model="fruit.checked">{{fruit.name}}</label> 
     </li> 
    <ul> 
</div> 

控制器:

var app = angular.module('app', []); 

function Ctrl($scope) { 
    $scope.fruits = [ 
     {'name':'apple'}, 
     {'name':'Mary'}, 
     {'name':'Mike'}, 
     {'name':'Adam'}, 
     {'name':'Julie'} 
    ]; 

    $scope.checkedFunction = function(fruit) { 
     return fruit.checked || false; 
    }; 
} 

你的小提琴是最新的與第二個解。

+0

謝謝,但是如果我想用ng模型來過濾數據怎麼辦? –

+0

不確定要明白:你總是可以在過濾器中使用「fruit.checked」(就像我對「orderBy」所做的那樣)。 – Mickael

+0

@RamalingaRaju你是什麼意思「使用ng模型來過濾數據」? – zsong

0

這裏是綁定在NG-重複複選框的NG-模型數據一個簡單的例子:tutorial here

這是一個很好的例子,與NG-價值的真實,NG-值假:

<div ng-repeat="fruit in fruits"> 
<input type="checkbox" ng-model="fruit.name" ng-true-value="{{fruit.name}}" ng-false-value="{{fruit-name}} - unchecked" ng-change="filterMultipleSystem()"/><br/> 
</div>