ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

Using rowTemplate to add save and delete affordances

Open specialjyo opened this issue 13 years ago • 8 comments

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.

specialjyo avatar Feb 26 '13 18:02 specialjyo

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>'},]

orneryd avatar Feb 27 '13 19:02 orneryd

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;
};

specialjyo avatar Mar 02 '13 02:03 specialjyo

Which is the best way i can pick up the current row on which the button has been clicked ?

sudarshan89 avatar Mar 25 '13 17:03 sudarshan89

just pass in "row" as an argument on your function for ng-click. ng-click="save(row)"

orneryd avatar Mar 25 '13 18:03 orneryd

FYI, row.entity is your data model for that row

orneryd avatar Mar 25 '13 18:03 orneryd

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;

   .....
}

specialjyo avatar Mar 25 '13 18:03 specialjyo

@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!

sudarshan89 avatar Mar 26 '13 04:03 sudarshan89

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

RichardAlan avatar Apr 11 '13 14:04 RichardAlan