Introduction

This article explains how to implement search functionality to each column as well as implement common search using AJAX table in ASP.NET MVC.

Background

We use tables or grids in our application but users expect different functionalities in tables and grids. This article explains how to make search functionality for each field in tables. We can make common and separate field search functionality using AJAX table, jQuery and passing data as a JSON format to table. It is very helpful to users.

Steps For Create Ajax Table

  1. Add a class in model like below.
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Web;
    5. namespace AjaxTable.Models
    6. {
    7. public class Country
    8. {
    9. public int Rank { get; set; }
    10. public string CountryName { get; set; }
    11. public int pulation { get; set; }
    12. }
    13. }
  2. Add new controls and action methods and add action methods and method in controllers, like the below coding.
    1. public class TableController : Controller
    2. {
    3. // GET: Table
    4. public ActionResult Index()
    5. {
    6. return View();
    7. }
    8. private List<Country> GetPopulation()
    9. {
    10. var populationList = new List<Country>
    11. {
    12. new Country
    13. {
    14. Rank = 1,
    15. CountryName = "Chine",
    16. pulation = 1367485388
    17. },
    18. new Country
    19. {
    20. Rank = 2,
    21. CountryName = "India",
    22. pulation = 1251695584
    23. },
    24. new Country
    25. {
    26. Rank = 3,
    27. CountryName = "United State",
    28. pulation = 321368864
    29. },
    30. new Country
    31. {
    32. Rank = 4,
    33. CountryName = "Indonesia",
    34. pulation = 255993674
    35. },
    36. new Country
    37. {
    38. Rank = 5,
    39. CountryName = "Brazil",
    40. pulation = 204259812
    41. },
    42. new Country
    43. {
    44. Rank = 6,
    45. CountryName = "Pakistan",
    46. pulation = 199085847
    47. },
    48. new Country
    49. {
    50. Rank = 7,
    51. CountryName = "Nigeria",
    52. pulation = 181562056
    53. },
    54. new Country
    55. {
    56. Rank = 8,
    57. CountryName = "Bangladesh",
    58. pulation = 168957745
    59. },
    60. new Country
    61. {
    62. Rank = 9,
    63. CountryName = "Russia",
    64. pulation = 142423773
    65. },
    66. new Country
    67. {
    68. Rank = 10,
    69. CountryName = "Japan",
    70. pulation = 126919659
    71. }
    72. };
    73. return populationList;
    74. }
    75. public ActionResult getPopulation()
    76. {
    77. var population = GetPopulation();
    78. return Json(population);
    79. }
    80. }
  3. Add view for corresponding action methods. After adding view we need to add three important CDNs in our view.

    CDN for Table Styles
    1. <link href="//cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet" />
    CDN for jQuery
    1. <script src="//code.jquery.com/jquery-1.12.3.js"></script>
    2. <script src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
    3. View Page Code
    4. @{
    5. ViewBag.Title = "Index";
    6. }
    7. <link href="//cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet" />
    8. <h2>Ajax Table Columns Search</h2>
    9. <table id="tblPopulation">
    10. <thead>
    11. <tr>
    12. <td>Rank</td>
    13. <td>Country Name</td>
    14. <td>Population</td>
    15. </tr>
    16. <tr>
    17. <td>
    18. <input type="text" />
    19. </td>
    20. <td>
    21. <input type="text" />
    22. </td>
    23. <td>
    24. <input type="text"/>
    25. </td>
    26. </tr>
    27. </thead>
    28. </table>
    29. @section Scripts {
    30. <script src="//code.jquery.com/jquery-1.12.3.js"></script>
    31. <script src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
    32. <script>
    33. $(document).ready(function () {
    34. $.ajax({
    35. url: '/Table/getPopulation',
    36. dataType: "json",
    37. type: "POST",
    38. contentType: 'application/json; charset=utf-8',
    39. data: {},
    40. success: function (data) {
    41. var tr;
    42. console.log(data);
    43. for (var i = 0; i < data.length; i++) {
    44. tr = tr + "<tr>";
    45. tr = tr + "<td>" + data[i].Rank + "</td>";
    46. tr = tr + "<td>" + data[i].CountryName + "</td>";
    47. tr = tr + "<td>" + data[i].population + "</td>";
    48. tr = tr + "</tr>";
    49. }
    50. $('#tblPopulation').append(tr);
    51. tblFormate();
    52. },
    53. error: function (xhr) {
    54. alert('No Valid Data');
    55. }
    56. });
    57. function tblFormate() {
    58. var table = $('#tblPopulation').DataTable(
    59. {
    60. //"searching": false,
    61. "lengthMenu": [[5, 10, 50, 100, 150, -1], [5, 10, 50, 100, 150, "All"]]
    62. });
    63. //Apply the search
    64. table.columns().eq(0).each(function (colIdx) {
    65. $('input', table.column(colIdx).header()).on('keyup change', function () {
    66. table
    67. .column(colIdx)
    68. .search(this.value)
    69. .draw();
    70. });
    71. });
    72. }
    73. });
    74. </script>
    75. }

Explanation

We added table heading as well as added text box for each column. We will be filtering data using corresponding text boxes. Using AJAX, we call the action methods from View to Controller and pass the data as a JSON from controller to view. JSON data will be appended to the tables.

We created one function for column search and normal table converted to an AJAX table with default design.

Following function is very important to each column search.

  1. function tblFormate() {
  2. // Convert normal table to Ajax table with default design
  3. var table = $('#tblPopulation').DataTable(
  4. {
  5. "lengthMenu": [[5, 10, 50, 100, 150, -1], [5, 10, 50, 100, 150, "All"]]
  6. });
  7. //Each column seach based on columns
  8. table.columns().eq(0).each(function (colIdx) {
  9. $('input', table.column(colIdx).header()).on('keyup change', function () {
  10. table
  11. .column(colIdx)
  12. .search(this.value)
  13. .draw();
  14. });
  15. });
  16. }
Final Step

After adding the above mentioned code just build and run the applications and we can see the output looks like the below screen shorts.


After Column Search


Conclusion

I hope this article explains in a simple way, how to create each column search in ASP.Net MVC; and it helps many freshers and students.