0

I am currently trying to implement the jquery datatables extension to produce a table of live results. These results are caused by my code behind function calling another web service.

I have javascript gathering my parameters and im am using the jquery.ajax() function to talk to my code behind function. The datatables function require JSON in the following format

{
  "aaData": [
    [
      "Trident",
      "Internet Explorer 5.0",
      "Win 95+",
      "5",
      "C"
    ]
 ]
}

I dont know how to get my functions response e.g. a string of comma separated information into the above format.

I have looked into json.net extension but I am not sure what approach to take. Considering that ever time my function runs I will be adding to the JSON which will need to be in a .txt file for datatables

Any suggestions appreciated!

Thanks

Code I currently have

var oTable;
$(document).ready ( function(){
      oTable=$('#mytable').dataTable({
       "sAjaxSource": 'TestData.txt'
   });
 });​

and to call my code behind and get a string back

function asyncServerCall(inputs) {
        jQuery.ajax({
            url: 'Process.aspx/SearchBtnAjax',
            data: JSON.stringify({ cities: inputs }),
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
            alert(data.d);
            oTable.fnReloadAjax();
             }
        });
    }
user1842675
  • 261
  • 4
  • 10
  • Not sure if I follow. But you can try jQuery JSON encode: http://stackoverflow.com/questions/10919965/how-do-i-encode-a-javascript-object-as-json – orolo Apr 02 '13 at 18:13
  • What does your JSON from the server look like currently? – Mike Robinson Apr 02 '13 at 18:18
  • This depends on the server side language of the webservice, if you created it, what language are you using? Ive done datatables json in coldfusion, php and C#, will be glad to help – Jay Rizzi Apr 02 '13 at 18:22
  • Also, expand more on the txt file aspect – Jay Rizzi Apr 02 '13 at 18:23
  • Thanks for all your advice so far. – user1842675 Apr 03 '13 at 07:55
  • My function is currently returning a string as I dont know the best format for it to return. As I understand datatables, i need to add the data every time i add a row to the json file, otherwise we i use paging e.t.c it wont work? Therefore I was considering writing and reading to a txt file every time i call my function – user1842675 Apr 03 '13 at 07:59

1 Answers1

0

Thats how I implemented live data in my dataTable:

For example, I have <table id="id"> with 5 cells in a row. Via AJAX I send some new values that i put to variables cell1,cell2,cell3,cell4,cell5

var table=$('#id').dataTable();
var newRow=table.fnAddData([cell1,cell2,cell3,cell4,cell5]);
table.fnSettings().aoData[newRow[0]].nTr;

Not sure that this is exactly what you need, but it works perfect for me.

Pavlo Shandro
  • 808
  • 5
  • 12
  • newRow=oTable.fnAddData([cell1,cell2]); oTable.fnSettings().aoData[newRow[0]].nTr; - not working - is there additional code for the functions fnAddData. I had to copy extra code into my JS for fnReloadAjax() – user1842675 Apr 03 '13 at 08:18