Using rowTemplate to add save and delete affordances
I'd like to simply edit the rowTemplate (possibly the wrong approach) to add a save and delete buttons for working with that row. I do see it, its just overriding the first column.
I tried adding a div with buttons to the end of a custom rowTemplate
var customRowTemplate = '<div ng-repeat="col in visibleColumns()" class="myCustomClass ngCell
{{columnClass($index)}} col{{$index}} {{col.cellClass}}" ng-cell></div><div class="row_edit"><a ng-
click="saveRow()"><i class="icon-save"></i></a></div>';
Would be cool to have a buttonTemplate for each row or something similar.
Is my approach here incorrect? I only one to save one row at a time.
you can add a couple of column definitions with blank "fields" and use the cellTemplate option to put a button in there.
columnDefs: [{field:'', cellTemplate: '<button ng-click="delete()">Delete</button>'},
{field:'', cellTemplate: '<button ng-click="save()">Save</button>'},]
Simple and elegant. And it works. Thanks! I did it this way incase someone ends up here from Google:
var buttonTemplate = '<div><button id="save_{{row.rowIndex}}" class="btn btn-small btn-inverse" disabled=true ng-click="saveRow()"><li class="icon-save"></li></button></div>';
var inputTemplate = '<input class="ngCellText colt{{$index}}" ng-model="row.entity[col.field]" ng-change="enableSave()" />';
Scope.buildColumns = function () {
var columns = Object.keys(Scope.tableData);
var columnDefs = [];
var saveColumn = {};
saveColumn.field = '';
saveColumn.cellTemplate =buttonTemplate;
columnDefs.push(saveColumn);
var column = {};
columns.forEach(function (name) {
column.field = name;
if (name != 'id') {
column.editableCellTemplate = inputTemplate;
column.enableFocusedCellEdit = true;
column.minWidth = 100;
}
columnDefs.push(column);
column = {};
});
return columnDefs;
};
Which is the best way i can pick up the current row on which the button has been clicked ?
just pass in "row" as an argument on your function for ng-click. ng-click="save(row)"
FYI, row.entity is your data model for that row
You have access to the row index and model. I have ng-click="saveRow()" on one of my buttons. Inside I grab the index and the model for the current row.
Scope.saveRow = function () {
var index = this.row.rowIndex;
var newRecord = this.row.entity;
.....
}
@timothyswt Thanks it works, i just wanted to know how does one find out that row is a variable that could passed into the click handler ? I mean is it documented somewhere ? Can this be added into the wiki ... I had asked the same question on SO, so i will answer to out their in case some else has the same issue.
Once again thanks a lot!
Would it be possible for you to add an example of this functionality, maybe together with a recommended way to add new rows to the grid. I have just started using your software. It works great but I find myself going through the issues documentation issue by issue and searching google to learn how to use a lot of the features. I hope when you get a chance you can keep adding new examples. thanks very much