0

I am working on JQuery data tables for displaying multiple grids on the same page.

I am not able to get the data binded to datatables though data is returned from the controller. Also i am not able to reload datatable on button click event and error is displayed that datatable is already initialized.

Please let me know how this can be implemented.

1 Answers1

0

I think you need to update your JavaScript to something like this:

$('#DetailReportGrid').DataTable({
    'ajax': { // THIS IS REPLACING "DATA"
       "type"   : "POST",
       "url"    : '/path/to/your/URL',
       "data"   : function( data ) {
          return data = JSON.stringify(data);
    },
    columns: [
         { "data": "Date", "autoWidth": true },
         { "data": "Time", "autoWidth": true },
         { "data": "CallerId", "autoWidth": true },
      ]
    });

The key here is 'ajax' replaces 'data' in your code. You may even need to put your ajax code inside the DataTable 'ajax' bit. I've never put the creation of the DataTable inside an ajax call - always the other way around. It may me okay, though.

Big Daddy
  • 5,160
  • 5
  • 46
  • 76
  • Thank you, i will try that. How can we reload the data tables again based on button click. i get a error popup datatable is already initialized. – Deepak Duvvada Nov 27 '17 at 13:53
  • To add on to that, how can we pass parameters to the url in this context. – Deepak Duvvada Nov 27 '17 at 13:57
  • Check out something like this - table.ajax.reload(); Pass parameters to your wrapping js method and append them to the ajax url – Big Daddy Nov 27 '17 at 14:08
  • implemented according to the above code and its works, thank you. For reload i have used table.ajax.reload(). this refreshes the table but doesn't pass parameters to the ajax call, please let me know how to pass parameters during reload. – Deepak Duvvada Nov 28 '17 at 05:52
  • Your ajax call should have the cached the previous call made to it, so no need to pass anything. Or, you may want to do this: https://stackoverflow.com/questions/32560635/reload-ajax-request-with-new-parameters – Big Daddy Nov 28 '17 at 12:50
  • i actually need to pass dates as parameters from textbox, when we change the date values, want to reload that data based on the date range on button click. – Deepak Duvvada Nov 28 '17 at 14:15