Build a dynamic datatable and filtering on server side

Build a dynamic datatable and filtering on server side

adrielmdqadrielmdq Posts: 2Questions: 1Answers: 0

Hi, i'm building a dynamic datatable on a proyect with MVC architecture. When the view load, I can see the information properly, but every time i try to use the filter, the legger "Proccesing" appears and the table doesn't reflect the filtered data. I can see when I'm debugging that the method on the controller is returning the desired data on exactly the same format as provided on the load of the page. Below is the code involved. Any help would be appreciated.

 $(document).ready(function () {
     var table;
     
     function initializeTable(headers) {
         var columns = headers.map(function (header) {
             return { data: header, title: header };
         });
         
         return $('#dynamicTable').DataTable({
             processing: true,
             serverSide: true,
             ajax: {
                 url: '@Url.Action("ObtainAccessRequest", "AccessRequest")',
                 type: 'GET',
                 data: function (d) {
                     
                     d.search.value = $('#searchInput').val();
                 },
                 dataSrc: 'data'
             },
             columns: columns,
             destroy: true 
         });
     }


     $.ajax({
         url: '@Url.Action("ObtainAccessRequest", "AccessRequest")',
         type: 'GET',
         success: function (json) {
             var headers = json.columns;
             var thead = '<tr>';
             headers.forEach(function (header) {
                 thead += '<th>' + header + '</th>';
             });
             thead += '</tr>';
             $('#dynamicTable thead').html(thead);

             table = initializeTable(headers);
         }
     });

     $('#searchInput').on('keyup', function () {
         if (table) {
             table.search(this.value).draw();
         }
     });
 });

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 62,497Questions: 1Answers: 10,264 Site admin
    Answer ✓

    If you are using server-side processing the filtering is the responsibility of the server-side processing script. Are you applying the filter in whatever @Url.Action("ObtainAccessRequest", "AccessRequest")' resolves to?

    The DataTables client / server parameters for server-side processing mode are documented here.

    Allan

  • adrielmdqadrielmdq Posts: 2Questions: 1Answers: 0

    I wasn't incrementing the draw parameter in the server side, your reference work out, thanks

Sign In or Register to comment.