Reputation: 13
I'm trying to create a table using ng-grid (I'm new to angularjs and ng-grid), which has the following columns:
What I want to achieve:
Snippet of what I'm doing in $scope.gridOptions
:
columnDefs : [ {
field: '',
displayName: 'Checkbox',
cellTemplate: '<div class="ngSelectionCell"><input tabindex="-1" class="ngSelectionCheckbox" type="checkbox" ng-checked="true" /></div>'
},
{
field:'',
displayName:'Radio'
cellTemplate :'<div class="ngSelectionCell"><input tabindex="-1" type="radio" ng-checked="{{defaultValue}}"/></div>'
},
{
field:'name',
displayName:'Name'
} ],
multiSelect: true,
enableRowSelection: true, // needed to be able to select via radio button
plugins: [new ngGridFlexibleHeightPlugin()],
selectedItems:$scope.selectedRows,
afterSelectionChange: function(rowItem, event) {
// What should be done?
}
Any suggestions on how I can achieve what I want to would be great. Thank you very much for your time.
Upvotes: 1
Views: 5052
Reputation: 8331
I'm not sure what it is good for, but it sounded like an interesting question.
Your grid data should look something like this:
$scope.myData = [{id:1,name: "Moroni", age: 50,changeable:true},
{id:2,name: "Tiancum", age: 43,changeable:true},
{id:3,name: "Jacob", age: 27,changeable:false},
{id:4,name: "Nephi", age: 29,changeable:true},
{id:5,name: "Enos", age: 34,changeable:true}];
$scope.selID=2;
The id
and the changeable
attribute in myData
are there to determine which row is affected by the checkbox.
The $scope.selID
is there to find out which row is currently checked by the radio button.
The actual columnDefs
contains the templates:
columnDefs : [ {
field: 'changeable',
displayName: 'Checkbox',
cellTemplate: '<div class="ngSelectionCell"><input tabindex="-1" class="ngSelectionCheckbox" type="checkbox" ng-model="row.entity.changeable"/></div>'
},
{
field:'name',
displayName:'Radio',
cellTemplate :'<div class="ngSelectionCell"><input tabindex="-1" type="radio" ng-disabled="(row.entity.changeable==false)" ng-checked="isChecked(row.entity.id)" ng-click="setSel(row.entity.id)"/></div>'
},
{
field:'name',
displayName:'Name'
},{
field:'age',
displayName:'Age'
} ]
There are two additional functions in the scope to toggle the (radio) selected row and to find out if a row is changeable:
$scope.isChecked=function(id){
if (id==$scope.selID){
return true;
}else{
return false;
}
}
$scope.setSel=function(id){
$scope.selID=id;
}
Here is a Plunker with full code that worked for me several seconds ago. For the moment it does not display anything since the ng-grid server seems to be down (again!). Try again in a few hours!
Upvotes: 1