سؤال

I am using SlickGrid, I have 3 coulmns in which first column is a Id column which must be kept unique(editable), I also want to auto increment the id feild of each row whenever the focus has entered new row. On form submit I must check uniqueness also.

هل كانت مفيدة؟

المحلول

I handle this by assigning a negative value for each new row and decrement the index. -1, -2, -3.

Once those rows are sent to the server and saved they come back with proper ids and everything is in good shape.

var newRowCounter = -1;

Then I added a callback for onAddNewRow to setup default values and refresh the grid.

this.Grid.onAddNewRow.subscribe(function (e, args) {
    var item = {
        myId: newRowCounter--
    };

    $.extend(item, args.item); // Merge my default item and user input
    args.item = item;

    var dataview = args.grid.getData(); // Retrieve my underlying datacontext.
    dataview.addItem(args.item); // Add my merged item to the datacontext.

    args.row = dataview.getLength() - 1;
    args.grid.invalidateRow(args.row); // Tell the grid this row changed

    self.Grid.updateRowCount(); 
    self.Grid.render(); // Display our changes.
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top