Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Am following this link to make ajax calls to load Jquery Datatables Dynamically

http://datatables.net/forums/discussion/3442/x&page=1#Item_10.

Before i start trying i got stuck with my idea here.

So how do the DataTables send the properties like iDisplayLength,iDisplayStart,sEcho to make pagination and display records.

How do i handle this ?

Sample code from Link for quick reference

$.ajax( {
        "dataType": 'text',
        "type": "GET",
        "url": "dtJSON.txt",
        "success": function (dataStr) {
            var data = eval( '('+dataStr+')' );
          $('#example').dataTable({
                "aaSorting": [[0, "desc"]],
                "aaData": data.aaData,
                "aoColumns": data.aoColumns,
                "bScrollCollapse": true,
                "bFilter": false,
                "sPaginationType": "full_numbers",
                "bJQueryUI": true,
                "aoColumnDefs": data.aoColumnDefs
          });
        }
    } );

I can get the data and column details using ajax but how do i handle the parameters sent to controller in MVC ?

Some assistance would be much appreciated :)

Thanks

share|improve this question

1 Answer 1

My recomendation is to render de table in the client with handlebars and after apply data tables:

You will need an empty table:

<table id="mytable">
  <thead>
      <tr>
          <th>Col 1</th>
          <th>Col 2</th>
          <th>Col 3</th>
      </tr>
  </thead>
  <tbody id="table_data_container">
      <!-- Populated by JS -->
  </tbody>
</table>

An ajax request, don't do pagination in the server-side, datatables will handle it for you in the client-side this means that you don't have to send the current page to the server, you just return all the available rows, if the number of rows is really high try to force the user to filter like search by name, id or something else you can then send that filter in the ajax request.

$.ajax({
    type: method,
    url: url,
    async: async,
    data: parameters,
    dataType: dataType,
    success: function(json){
        var container = $('#table_data_container');
        //your response should be an object like { items : [item,item,item,...] }
        var template = '{{#each item}}<tr><td>{{this.col1}}</td><td>{{this.col1}}</td><td>{{this.col1}}</td></tr>{{/each}}' //visit http://handlebarsjs.com/ for info
        RenderJson(json,template,container); //this will generate thehtml for the rows and append it to the table
        $('#mytable').dataTable();
    },
    error: function(response){
        alert('Error!');
    }
});

And a rendering function:

function RenderJson(json,template,container) {
    var compiledTemplate = Handlebars.compile(template);
    var html = compiledTemplate(json);
    $(container).html(''); //remove previous content
    $(container).html(html); //set new content
}

Hope it helps ;)

share|improve this answer
    
Thanks for the answer.i want to load columns as well in runtime with server side pagination. Any suggestions > –  user2067567 Apr 25 '13 at 9:39
    
because if data is huge i don want to bring everything to client –  user2067567 Apr 25 '13 at 9:40
    
In my example the template is static (var template = '{{#each item}}<tr><td>{{this.col1}}</td><td>{{this.col1}}</td><td>{{this.col1}}</td></tr‌​>{{/each}}') you could have a second ajax request and generate the template in the server, so you can send selected columns and filters and do 2 ajax request one for the data and another for the template, then render it and apply datatable, if you reaply it you wil need to destroy it first: var oTable = $('#tableid').dataTable(); oTable.fnDestroy(); –  OweR ReLoaDeD Apr 25 '13 at 11:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.