2016-11-08 16 views
0

我有這個html代碼,它使用angularjs來顯示錶中的內容。將此數字字符更改爲angularjs上的表格內的複選框

<div ng-controller="CheckCtrl"> 
    <table class="table table-hover data-table sort display"> 
     <thead> 
     <tr> 
      <th class="Serial_"> 
       Serial 
      </th> 
      <th class="Name_"> 
       Name 
      </th> 
      <th class="ID_"> 
       ID 
      </th> 
      <th class="On_off_"> 
       On/off 
      </th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr ng-repeat="item in check_items"> 
      <td>{{item.SERIAL}}</td> 
      <td>{{item.NAME}}</td> 
      <td>{{item.ID}}</td> 
      <td>{{item.ON_OFF}}</td> 
     </tbody> 
    </table> 
</div> 

該網頁看起來像這樣;

enter image description here

這裏是控制器的代碼。

.controller('CheckCtrl', ['$scope', '$http', 'configuration', 
    function ($scope, $http, $configuration) { 
     var url_api = $configuration.host + "cloe/webroot/cloe-cloud/app/API.json"; 
     $http.get(url_api).success(function(data) 
     { 
      $scope.check_items = data; 
     }); 

我想將開/關列數字字符更改爲複選框。如果數字字符爲'0',則複選框未被選中。如果數字字符爲'1',則複選框被選中。

我正在使用angularjs v1和twitter bootstrap。

編輯:對不起,我意識到我的複選框值是一個字符,而不是數字。它們是'0','1'而不是0,1。

+0

通過將輸入更改爲複選框來嘗試此ng-checked =「item.ON_OFF == 0」如果您可以爲我們製作一個調度器 –

回答

4

function myCtrl($scope) { 
 
    $scope.check_items = [ { 
 
     'SERIAL':12345, 
 
     'NAME': 'ANil Kumar Ram', 
 
     'ID' : 1, 
 
     'ON_OFF': '1' 
 
    },{ 
 
     'SERIAL':6453, 
 
     'NAME': 'Rohan Ram', 
 
     'ID' : 2, 
 
     'ON_OFF': '0' 
 
    },{ 
 
     'SERIAL':732, 
 
     'NAME': 'Sunil Ram', 
 
     'ID' : 3, 
 
     'ON_OFF': '0' 
 
    },{ 
 
     'SERIAL':1261, 
 
     'NAME': 'Ram Jitesh', 
 
     'ID' : 4, 
 
     'ON_OFF': '1' 
 
    }] 
 
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 

 
<div ng-app ng-controller="myCtrl"> 
 
    <table class="table table-hover data-table sort display" style="width:100%"> 
 
     <thead> 
 
     <tr> 
 
      <th class="Serial_"> 
 
       Serial 
 
      </th> 
 
      <th class="Name_"> 
 
       Name 
 
      </th> 
 
      <th class="ID_"> 
 
       ID 
 
      </th> 
 
      <th class="On_off_"> 
 
       On/off 
 
      </th> 
 
     </tr> 
 
     </thead> 
 
     <tbody> 
 
     <tr ng-repeat="item in check_items"> 
 
      <td>{{item.SERIAL}}</td> 
 
      <td>{{item.NAME}}</td> 
 
      <td>{{item.ID}}</td> 
 
      <td> <input type="checkbox" ng-checked="item.ON_OFF == '1'"></td> 
 
     </tbody> 
 
    </table> 
 
</div>

希望這將價值幫幫我。

+0

非常感謝。我有個問題。我剛剛意識到複選框的值是以字符串形式存在的。他們是'ON_OFF':'1',而不是'ON_OFF':1.我將相應地更改問題細節。 – user781486

+0

在這種情況下,你必須比較字符串與'==' –

+1

我已修改的代碼段,因爲你使用ON_OFF作爲字符串..如果這有幫助,你可以接受我的回答 –

2
<input type='checkbox' ng-checked='{{item.ON_OFF == 1}}'> 

認爲應該這樣做。

3
<input type="checkbox" ng-model="{{item.ON_OFF}}" ng-true-value="1" ng-false-value="0"> 

ng-true-value將設置輸入的值檢查 ng-false-value="0",將設置輸入選中

角文檔 https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D

+0

語法更正 Zalbl123

+0

您可以使用評論1中提到的正確語法編輯您的答案,這將有助於其他人 –

+0

根據要求沒有問題。 – Zalbl123

相關問題