Click here to Skip to main content
15,891,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
paging and sorting working fine at first time we select the drop down,if i choose some other drop down means displaying old records and paging also not working.I want to change the table with paging each time change the Drop down.

alert box showing cannot reinitialize data table.

please help me to resolve my issue .

What I have tried:

JavaScript
<script type="text/javascript">
        function funChangeTable(ddlObj) {
            var name = ddlObj.value;
            debugger; 
            $.ajax({
                url: 'JsonPaging.aspx/GetTableData',
                data: JSON.stringify({ tableName: name }),
                type: 'post',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) {
                    debugger;
                    var json = JSON.parse(response.d);
                    generateTable(json);
                  
                },
                error: function(a, b, c) {
                    console.log(a, b, c);
                }
            });
        }
  
//        function funChange(ddlObj1) {
//            var name1 = ddlObj1.value;
//            debugger;
//            $.ajax({
//            url: 'JsonPaging.aspx/Paging',
//                data: JSON.stringify({ tableName: name1 }),
//                type: 'post',
//                contentType: "application/json; charset=utf-8",
//                dataType: "json",
//                success: function(response) {
//                    debugger;
//                    var json = JSON.parse(response.d);
//                    paging(json);
//                },
//                error: function(a, b, c) {
//                    console.log(a, b, c);
//                }
//            });
//        }

//        function paging(json) {
//            var $table = $('#tblDynamic');
//            $table.find('thead').empty()
//            $table.find('tbody').empty()
//        }
//            if (json && json.length > 0) {
//                var header = json[0];
//                var columns = [];
//                for (var col in header) {
//                    columns.push('<th>' + col + '</th>');
//                }
          function generateTable(json) {
            
            var $table = $('#tblDynamic');
            $table.find('thead').empty()
            $table.find('tbody').empty()
            if (json && json.length > 0) {
                var header = json[0];
                var columns = [];
                for (var col in header) {
                    columns.push('<th>' + col + '</th>');
                }
                $table.find('thead').append('<tr>' + columns.join('') + '</tr>');
               
                var rows = [];
                for (var i = 0; i < json.length; i++) {
                  
                    var row = json[i];
                    var tds = [];
                    for (var col in row) {
                        tds.push('<td>' + row[col] + '</td>');
               
                       // tds.push('<td>' + row.city_name + '</td>');
                       // tds.push('<td>' + row.population + '</td>');
                       //tds.push('<td>' + row.year + '</td>');
                    } 
                    rows.push('<tr>' + tds.join() + '</tr>');
                } 
                $table.find('tbody').append(rows.join(''));
            }
            
            new1();
            





        }
        

        function new1() {
//   $('#tblDynamic').dataTable();
            
             $('#tblDynamic').DataTable({
          

            
                 initComplete: function() {
                     this.api().columns().every(function() {
                     debugger;
                         
                         var column = this;
                         var select = $('<select><option value=""></option></select>')
                    .appendTo($(column.footer()).empty())
                    .on('change', function() {
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );
                        //to select and search from grid  
                        debugger;
                        column
                            .search(val ? '^' + val + '$' : '', true, false)
                            .draw();
                        
                    });

                         column.data().unique().sort().each(function(d, j) {
                         select.append('<option value="' + d + '">' + d + '</option>')

                     });
                     
                     });
                 }

             });
            
             
         }

       
        
        </script>
Posted
Updated 23-May-17 5:42am

1 solution

Please refer to the following detailed article which should give you the whole idea how to achieve what you are trying to :

GridView with Server Side Filtering, Sorting and Paging in ASP.NET MVC 5

Hope it helps!
 
Share this answer
 
Comments
GrpSMK 24-May-17 3:01am    
working fine,but every time have to refresh the page otherwise not working properly.,here where i have to use window.location.reload() function?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900