Monday, December 19, 2011
We will be using several jQuery libraries, all of which need to be added in our @section top area:
The first line for jQuery Form will work the magic of hiding/showing our editing forms. The next line is for blockUI. This library will allow us to put up a “wait” cursor when the client is waiting on the server for updates. The next two lines allow us to do some client side validation.
The $(“#dialog- portion is using the jQuery selector to find our forms by their ID, then the .dialog is transforming the div to a hidden form. The options being passed are pretty self-descriptive: height, width, etc. The major difference on the delete form is that we are defining a cancel button. While not completely necessary (the user can always click the ‘x’ to close the form) it does illustrate some of jQuery Form’s capabilities.
Running the project now should show a clean page, with the forms hidden and only the table showing. However we don’t yet have a way to trigger the forms. The work follow will follow this process:
Row selection – row selection in DataTables is pretty easy. Simple add the following code to the $(document).ready script section:
This looks for the tbody of employees div and adds a click event. When a cell is clicked it cycles through the entire table collection removing any existing occurrences of ‘row_selected’ class. It then adds the ‘row_selected’ class to the parent of the cell.
Note: If you wanted to support multi-select you could easily modify this by removing the .each loop and changing the .addClass to add/remove the ‘row_selected’ based on the current class.
To populate and open the form we need to have something to trigger the action. In this example we will add simple links at the top of the grid for Create, Edit, and Delete.
The create event is the simplest of the three. We use jQuery to find the ID of each input tag and ensure it is blank by setting it to an empty string. Once the values are blanked out it calles the jQuery Form option to set the form title to ‘Create’, then opens the form. Note that it is necessary to blank out the values because the form could have been previously populated with the edit click event, which we will look at next.
The edit event is a little more complex. First we need to get the values of the selected row. To accomplish this we call a function fnGetSelected(), which we will show later. For now just know that this function returns the node representing the currently selected row in the table. The next line assigns the cell contents of that row to the ‘rowdata’ array variable by using the jQuery .closest(‘tr’) to grab the row, then .children(‘td’) to get the collection of the children.
Now that we have all the row data we fill in the form. For the EmpllyeeID we are removing the ‘row-‘ portion of the row id, leaving us with the integer ID in the database. The next three values come straight from the rowdata array, giving us the Name, Email, and Phone values.
Once the values are filled in we set the title of the form to ‘Edit’, then open the form.
The delete uses the same process as the edit form, except we only display the Name value.