Advanced jQuery DataTables in MVC 4- Part 4

If you are new to this plug-in, please go through the previous articles to learn the basics of a jQuery plug-in.

Based on the project needs we might need to customize the existing component. Already utilized some of the built-in properties within the project. We will see one by one how to actually use it with an example.

1. Property: Destroy

Generating the new DataTable with a new set of records, it will expect the Boolean property and the default will be False.

2. Property: PagingType

There are four built-in options, the default will be Simple_numbers option.

Simple: It will show Previous and Next only:

table

Simple_numbers: Show Previous and Next along with numeric numbers:

address

Full: First, Prev, Next, Last buttons only.

name

Full_numbers: First, Prev, Next, Last buttons with numeric numbers also.

numeric numbers

3. Property: Processing

Showing the processing indicator when performing some operation on top of the DataTable such as Sorting, Paging, and so on. Basically it will expect a Boolean property.

4. Function: fnServerParams

Sometimes you might need to pass some data when an invoking Ajax request to the server side, so you can the utilize these events.

fnServerParams

Just consider the previous image that has a similar search page, when triggering the Ajax event we need to pass the user-provided input value to the Ajax method. So at that time you can use this function and get the user input data and pass it into the Ajax method.

  1. "fnServerParams": function(data) {  
  2.     data.push({  
  3.         "name""ID",  
  4.         "value": $(‘txtID’).val()  
  5.     });  
  6.     data.push({  
  7.         "name""Name",  
  8.         "value": $(‘txtName’).val()  
  9.     });  
  10.     data.push({  
  11.         "name""Address",  
  12.         "value": $(‘txtAddress’).val()  
  13.     });  
  14.     data.push({  
  15.         "name""Designation",  
  16.         "value": $(‘txtDesg’).val()  
  17.     });  
  18.     data.push({  
  19.         "name""DateOfJoining",  
  20.         "value": $(‘txtDOJ’).val()  
  21.     });  
  22. }  
So the preceding function generates an Array list object and creates a name and value pair. Be sure the name property matches your Ajax method property name.
  1. public ActionResult JsonOutputMethod (string ID, string Name, string Address, string Designation, string DateOfJoining)  
  2. {  
  3.    //Code Implementation  
  4. }  
5. Event: CreatedRow

Sometimes you may need to create a Link button such as Adding, Editing, Deleting records into the existing rows, this event will be the best approach. Once the current row has been generated, if you are able to add some piece of HTML tag to any of the cells.
  1. "createdRow": function(row, data, index)   
  2. {  
  3.     //Code implementation.  
  4. }  
  5.     //Row–Generated current row object.  
  6.     //Data–All column data  
  7.     //Index–Current row index  
  8.   
  9. "createdRow": function(row, data, index)  
  10. {  
  11.   
  12.     $('td', row).eq(1).css("color""red");  
  13.     var aJueryTag = $("<a>");  
  14.     var imageTag = $("<img>");  
  15.     imageTag.attr("src""img/view.png");  
  16.     aJueryTag.attr("href""javascript:void(0);");  
  17.     aJueryTag.attr("onclick""showImg('/content/upload/" + data["imgname"].toString() + "','" + data["description"].toString() + "')");  
  18.   
  19.     aJueryTag.wrapInner(imageTag)  
  20.     $('td', row).eq(10).attr("align""center");  
  21.     $('td', row).eq(10).html(aJueryTag).text();  
  22. }  
In the preceding createdRow function code dynamically adds an anchor tag targeted to the destination cell using the help of jQuery and changing the background color of the cell.

6. Property: Column

Using this property we can pass dynamic columns to the jQuery DataTable plugin.

Example
  1. "columns": [{  
  2.     "data""Id"  
  3. }, {  
  4.     "data""Name"  
  5. }, {  
  6.     "data""Address"  
  7. }, {  
  8.     "data""Designation"  
  9. }, {  
  10.     "data""DataOfJoining"  
  11. }]  
Sometimes you may need to pass Dynamic columns to the jQuery Data table. To do that just declare one JavaScript variable and generate the two-dimensional JavaScript array and pass the variable within the column collection. Please refer to the following example:
  1. var dynColumns = [];  
  2. dynColumns.push({  
  3.     "data""ID"  
  4. });  
  5. dynColumns.push({  
  6.     "data""Name"  
  7. });  
  8. dynColumns.push({  
  9.     "data""Address"  
  10. });  
  11. dynColumns.push({  
  12.     "data""Designation "  
  13. });  
  14. dynColumns.push({  
  15.     "data""DateOfJoining"  
  16. });  
  17.   
  18. $('#tableData').DataTable({  
  19.   
  20.     "columns": dynColumns  
  21. });  
I hope you guys enjoyed a lot. After reading this article please don't forget to provide your valuable comments.
Your comments will be highly appreciated.


Similar Articles