How to insert item into a DataTable that is binded to a form

Hi Guys

I’ve got a DataTable which is binded to a form, when somebody clicks an item the form obviously get populated.

To save the changes, there are 2 buttons, “Update” to update the current DataTable row, or “Insert” to insert the changes as a new row.

For Update I simply call form.save(). which works perfectly.

But how would you handle Insert? At the moment I’m simply trying to change the rows ID field, but I get an error which says “attempt to change ID in updateitem”.

Thanks

Hi,

Here two variants are possible:

If you want to use bind API anyway, you should remove cursor from the grid data and change form values:

var values = form.getValues();
values.id = webix.uid(); //avoid non-unique id error
grid.setCursor(null);
form.setValues(values);
form.save();

On UI level, cursor is removed by unselecting the datatable item.

Alternatively, you can collect form values and add them to the datatable:

var values = form.getValues();
values.id = webix.uid();
grid.add(values);

http://webix.com/snippet/42212b70