Our final functionality is adding and deleting records in the grid.
We will add/delete records by clicking the respective buttons in the toolbar.
To provide the declared functionality, we will attach handler functions to the onClick event of our "Add"/"Delete" buttons.
"index.html"
toolbar.attachEvent("onclick",function(id){
if(id=="newContact"){ //'newContact' is the id of the button in the toolbar
var rowId=contactsGrid.uid(); //generates an unique id
var pos = contactsGrid.getRowsNum(); //gets the number of rows in grid
contactsGrid.addRow(rowId,["New contact","",""],pos); //adds a new row };
});
The addRow method takes 3 parameters: the row's id, cells' values and row's index.
"index.html"
var dpg = new dataProcessor("data/contacts.php");
dpg.init(contactsGrid);
dpg.attachEvent("onAfterUpdate", function(sid, action, tid, tag){ if (action == "inserted"){ contactsGrid.selectRowById(tid); //selects the newly-created row contactForm.setFocusOnFirstActive();//set focus to the 1st form's input }});
The setFocusOnFirstActive method makes the first form's input active,
i.e. opens the editor for it.
"index.html"
toolbar.attachEvent("onclick",function(id){
if(id=="newContact"){
var rowId=contactsGrid.uid();
var pos = contactsGrid.getRowsNum();
contactsGrid.addRow(rowId,["New contact","",""],pos);
contactsGrid.selectRowById(rowId);
contactForm.setFocusOnFirstActive();
};
if(id=="delContact"){
var rowId = contactsGrid.getSelectedRowId(); var rowIndex = contactsGrid.getRowIndex(rowId); if(rowId!=null){ contactsGrid.deleteRow(rowId); if(rowIndex!=(contactsGrid.getRowsNum()-1)){ contactsGrid.selectRow(rowIndex+1,true); } else{ contactsGrid.selectRow(rowIndex-1,true) } } }});