2013-11-26 82 views
3

我想建立一個表,其中列之一被選中,我想選擇與我從服務器獲得的值的選項。 我從服務器獲取4,但選擇的是第一個選項。Angularjs ng格與選擇單元格沒有選擇正確的值

$scope.lotteryOptions = { 
     data: 'myData', 
     enableColumnResize: true, 
     keepLastSelected: false, 
     enableRowSelection: false, 
     columnDefs: [{field: 'field1', displayName: 'field1'}, 
      {field: 'Status', displayName: 'Status', cellTemplate: selectTableTemplate, enableCellEdit: true}, 
    }; 

var selectTableTemplate = "<select ng-selected=\"{{row.getProperty('Status')}}\">   <option value='1' class='ng-binding'>" + 1+ "</option>" + 
          "<option value='2' class='ng-binding'>" + 2 + "</option>" + 
          "<option value='3' class='ng-binding'>" + 3 + "</option>" + 
          "<option value='4' class='ng-binding'>" + 4 + "</option>" + 
          "<option value='5' class='ng-binding'>" + 5 + "</option>" + 
        "<option value='6' class='ng-binding'>" + 6 + "</option></select>"; 

HTML結果是:

<select ng-selected="4">...</select> 

但不選擇4選擇

回答

2

ng-selected應該被施加到option標籤,不select(參見the docs

$scope.lotteryOptions = { 
    columnDefs: [ 
     {field: 'field1'}, 
     {field: 'Status', cellTemplate: selectTableTemplate, enableCellEdit: true} 
    ], 
    data: 'myData', 
    enableColumnResize: true, 
    enableRowSelection: false, 
    keepLastSelected: false 

}; 

var selectTableTemplate = '<select>' + 
          ' <option value="1" class="ng-binding" ng-selected="COL_FIELD == 1">' + 1 + '</option>' + 
          ' <option value="2" class="ng-binding" ng-selected="COL_FIELD == 2">' + 2 + '</option>' + 
          ' <option value="3" class="ng-binding" ng-selected="COL_FIELD == 3">' + 3 + '</option>' + 
          ' <option value="4" class="ng-binding" ng-selected="COL_FIELD == 4">' + 4 + '</option>' + 
          ' <option value="5" class="ng-binding" ng-selected="COL_FIELD == 5">' + 5 + '</option>' + 
          ' <option value="6" class="ng-binding" ng-selected="COL_FIELD == 6">' + 6 + '</option>' + 
          '</select>'; 
+0

謝謝!它的工作 – Asaf

0

ng-selected已經是一個角度屬性,這樣就沒有必要進行插值{{ }}

var selectTableTemplate = "<select>   <option ng-selected=\"row.getProperty('Status') == 1\" value='1' class='ng-binding'>" + 1 + "</option>" + 
          "<option ng-selected=\"row.getProperty('Status') == 2\" value='2' class='ng-binding'>" + 2 + "</option>" + 
          "<option ng-selected=\"row.getProperty('Status') == 3\" value='3' class='ng-binding'>" + 3 + "</option>" + 
          "<option ng-selected=\"row.getProperty('Status') == 4\" value='4' class='ng-binding'>" + 4 + "</option>" + 
          "<option ng-selected=\"row.getProperty('Status') == 5\" value='5' class='ng-binding'>" + 5 + "</option>" + 
        "<option ng-selected=\"row.getProperty('Status') == 6\" value='6' class='ng-binding'>" + 6 + "</option></select>"; 

注意:這也假定Status是一個值,而不是一個對象。

更新: ng-selected不是select的屬性。它只是option的一個屬性。

+0

不工作,我的代碼的HTML的結果是:<選擇納克選擇= 「4」> ...但不是選擇4選擇 – Asaf

+0

更新。 ng-selected只能用於選項,不能選擇。它需要是布爾返回表達式。 –