Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  178] [ 2]  / answers: 1 / hits: 18106  / 10 Years ago, fri, april 18, 2014, 12:00:00

In angularjs script i have the ng-grid module and, in one column, i want show the checkbox control instead of the true/false string returned from json data.
The checkbox must be checked if the json variable is true and unchecked if false.



I think i must use the template but i don't know how.



How can i show the checkboxes? There are some examples?



Thanks


More From » html

 Answers
23

Like this: updated with change handler



    var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
$scope.myData = [{
name: Moroni,
age: 50,
dude: true
}, {
name: Tiancum,
age: 43,
dude: true
}, {
name: Jacob,
age: 27,
dude: false
}, {
name: Nephi,
age: 29,
dude: true
}, {
name: Enos,
age: 34,
dude: false
}];
$scope.gridOptions = {
data: 'myData',
columnDefs: [{
field: 'name',
displayName: 'Name'
}, {
field: 'age',
displayName: 'Age'
}, {
field: 'dude',
displayName: 'Dude',
cellTemplate: '<input type=checkbox ng-model=row.entity.dude ng-click=toggle(row.entity.name,row.entity.dude)>'
}]
};

$scope.toggle = function(name, value) {
//do something usefull here, you have the name and the new value of dude. Write it to a db or else.
alert(name + ':' + value);
}
});


You now can add an change handler or just set the input to disable/readonly if you just want to display the value.



Plunker



Plunker with change handler



(I'm so sorry for Jacob and Enos!)


[#71403] Wednesday, April 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jamilab

Total Points: 687
Total Questions: 88
Total Answers: 86

Location: Antigua and Barbuda
Member since Sat, Apr 24, 2021
3 Years ago
;