Initialize two different tables with a single AJAX call










0















I'm trying to render two different tables but with a single AJAX call.



How to initialize two different tables but using the same source of JSON data?
The only different of these two tables is that I filter the rows by a specific key in JSON.



Currently my code looks like this.



var t = $('#adminKeysTable').DataTable( 
"ajax":
"url": getKeysById,
"dataSrc": function(json)
var rows = ;
for (var i=0;i<json.keys.length;i++)
if (json.keys[i].privileges == '32')
rows.push(json.keys[i]);
allAdminKeys.push(json.keys[i].key);

return rows;

,
"columns": [
"data": null ,
"data": "name" ,
"data": "key" ,
"data": null
]
);

var tt = $('#apiAccessKeyTable').DataTable(
"ajax":
"url": getKeysById,
"dataSrc": function(json)
var rows = ;
for (var i=0;i<json.keys.length;i++)
if (json.keys[i].privileges != '32')
rows.push(json.keys[i]);

return rows;

,
"columns": [
"data": null ,
"data": "name", "className": "editable" ,
"data": "key" ,
"data": "externalUser", "className": "editable" ,
"data": "privilegesArray", "className": "edit-privileges" ,
"data": null ,
"data": null
]
);


The issue with my current code is that it tends to take longer time to load the second table after the first one has done loading. I'm assuming that's because I'm making two AJAX calls at the same time?



I'm new to AJAX. So not sure how to make only one call to the GET request, and use that JSON data to initialize two separate tables.



Appreciate if someone can guide me. Thanks.










share|improve this question
























  • simply make one ajax request out of the dataTable init code, store that response in a variable, then call 2 different dataTable with that same data

    – Towkir Ahmed
    Nov 15 '18 at 6:33











  • first make a ajax call when data is return just simply bind to Datatable.

    – Negi Rox
    Nov 15 '18 at 7:07















0















I'm trying to render two different tables but with a single AJAX call.



How to initialize two different tables but using the same source of JSON data?
The only different of these two tables is that I filter the rows by a specific key in JSON.



Currently my code looks like this.



var t = $('#adminKeysTable').DataTable( 
"ajax":
"url": getKeysById,
"dataSrc": function(json)
var rows = ;
for (var i=0;i<json.keys.length;i++)
if (json.keys[i].privileges == '32')
rows.push(json.keys[i]);
allAdminKeys.push(json.keys[i].key);

return rows;

,
"columns": [
"data": null ,
"data": "name" ,
"data": "key" ,
"data": null
]
);

var tt = $('#apiAccessKeyTable').DataTable(
"ajax":
"url": getKeysById,
"dataSrc": function(json)
var rows = ;
for (var i=0;i<json.keys.length;i++)
if (json.keys[i].privileges != '32')
rows.push(json.keys[i]);

return rows;

,
"columns": [
"data": null ,
"data": "name", "className": "editable" ,
"data": "key" ,
"data": "externalUser", "className": "editable" ,
"data": "privilegesArray", "className": "edit-privileges" ,
"data": null ,
"data": null
]
);


The issue with my current code is that it tends to take longer time to load the second table after the first one has done loading. I'm assuming that's because I'm making two AJAX calls at the same time?



I'm new to AJAX. So not sure how to make only one call to the GET request, and use that JSON data to initialize two separate tables.



Appreciate if someone can guide me. Thanks.










share|improve this question
























  • simply make one ajax request out of the dataTable init code, store that response in a variable, then call 2 different dataTable with that same data

    – Towkir Ahmed
    Nov 15 '18 at 6:33











  • first make a ajax call when data is return just simply bind to Datatable.

    – Negi Rox
    Nov 15 '18 at 7:07













0












0








0








I'm trying to render two different tables but with a single AJAX call.



How to initialize two different tables but using the same source of JSON data?
The only different of these two tables is that I filter the rows by a specific key in JSON.



Currently my code looks like this.



var t = $('#adminKeysTable').DataTable( 
"ajax":
"url": getKeysById,
"dataSrc": function(json)
var rows = ;
for (var i=0;i<json.keys.length;i++)
if (json.keys[i].privileges == '32')
rows.push(json.keys[i]);
allAdminKeys.push(json.keys[i].key);

return rows;

,
"columns": [
"data": null ,
"data": "name" ,
"data": "key" ,
"data": null
]
);

var tt = $('#apiAccessKeyTable').DataTable(
"ajax":
"url": getKeysById,
"dataSrc": function(json)
var rows = ;
for (var i=0;i<json.keys.length;i++)
if (json.keys[i].privileges != '32')
rows.push(json.keys[i]);

return rows;

,
"columns": [
"data": null ,
"data": "name", "className": "editable" ,
"data": "key" ,
"data": "externalUser", "className": "editable" ,
"data": "privilegesArray", "className": "edit-privileges" ,
"data": null ,
"data": null
]
);


The issue with my current code is that it tends to take longer time to load the second table after the first one has done loading. I'm assuming that's because I'm making two AJAX calls at the same time?



I'm new to AJAX. So not sure how to make only one call to the GET request, and use that JSON data to initialize two separate tables.



Appreciate if someone can guide me. Thanks.










share|improve this question
















I'm trying to render two different tables but with a single AJAX call.



How to initialize two different tables but using the same source of JSON data?
The only different of these two tables is that I filter the rows by a specific key in JSON.



Currently my code looks like this.



var t = $('#adminKeysTable').DataTable( 
"ajax":
"url": getKeysById,
"dataSrc": function(json)
var rows = ;
for (var i=0;i<json.keys.length;i++)
if (json.keys[i].privileges == '32')
rows.push(json.keys[i]);
allAdminKeys.push(json.keys[i].key);

return rows;

,
"columns": [
"data": null ,
"data": "name" ,
"data": "key" ,
"data": null
]
);

var tt = $('#apiAccessKeyTable').DataTable(
"ajax":
"url": getKeysById,
"dataSrc": function(json)
var rows = ;
for (var i=0;i<json.keys.length;i++)
if (json.keys[i].privileges != '32')
rows.push(json.keys[i]);

return rows;

,
"columns": [
"data": null ,
"data": "name", "className": "editable" ,
"data": "key" ,
"data": "externalUser", "className": "editable" ,
"data": "privilegesArray", "className": "edit-privileges" ,
"data": null ,
"data": null
]
);


The issue with my current code is that it tends to take longer time to load the second table after the first one has done loading. I'm assuming that's because I'm making two AJAX calls at the same time?



I'm new to AJAX. So not sure how to make only one call to the GET request, and use that JSON data to initialize two separate tables.



Appreciate if someone can guide me. Thanks.







javascript jquery datatable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 6:34







jenna_3108

















asked Nov 15 '18 at 6:27









jenna_3108jenna_3108

10019




10019












  • simply make one ajax request out of the dataTable init code, store that response in a variable, then call 2 different dataTable with that same data

    – Towkir Ahmed
    Nov 15 '18 at 6:33











  • first make a ajax call when data is return just simply bind to Datatable.

    – Negi Rox
    Nov 15 '18 at 7:07

















  • simply make one ajax request out of the dataTable init code, store that response in a variable, then call 2 different dataTable with that same data

    – Towkir Ahmed
    Nov 15 '18 at 6:33











  • first make a ajax call when data is return just simply bind to Datatable.

    – Negi Rox
    Nov 15 '18 at 7:07
















simply make one ajax request out of the dataTable init code, store that response in a variable, then call 2 different dataTable with that same data

– Towkir Ahmed
Nov 15 '18 at 6:33





simply make one ajax request out of the dataTable init code, store that response in a variable, then call 2 different dataTable with that same data

– Towkir Ahmed
Nov 15 '18 at 6:33













first make a ajax call when data is return just simply bind to Datatable.

– Negi Rox
Nov 15 '18 at 7:07





first make a ajax call when data is return just simply bind to Datatable.

– Negi Rox
Nov 15 '18 at 7:07












4 Answers
4






active

oldest

votes


















0














Taking different into consideration, once you get data, you can store the data to the different variable and pass the data view to likewise you're passing.



var tt = $('#apiAccessKeyTable').DataTable( 
"ajax":
"url": getKeysById,
"dataSrc": function(json)
var t1rows = ;
var t2rows = ;
for (var i=0;i<json.keys.length;i++)
if (json.keys[i].privileges != '32')
t1rows.push(json.keys[i]); // Changed section
else
t2rows.push(json.keys[i]); // changed section



return rows;

,
"columns": [
"data": null ,
"data": "name", "className": "editable" ,
"data": "key" ,
"data": "externalUser", "className": "editable" ,
"data": "privilegesArray", "className": "edit-privileges" ,
"data": null ,
"data": null
]
);


Hope this helps!






share|improve this answer






























    0














    You do 1 ajax call, split data based on condition and load Datatable with respective JSON



    Something like this



    var t1rows = ;
    var t2rows = ;


    function splitData()

    $.ajax(
    url: getKeysById,
    success: function(json)
    for (var i=0; i < json.keys.length ; i++)
    if (json.keys[i].privileges != '32')
    t1rows.push(json.keys[i]); // Changed section

    else
    t2rows.push(json.keys[i]); // changed section


    // load Datatables
    loadDatatable1();
    loadDatatable2();



    );



    function loadDatatable1()

    $('#apiAccessKeyTable').dataTable(
    data: t1rows,
    columns: [
    data: 'userID' ,
    data: 'userName'
    ]
    );



    function loadDatatable2()

    $('#adminKeysTable').dataTable(
    data: t2rows,
    columns: [
    data: 'userID' ,
    data: 'userName'
    ]
    );






    share|improve this answer






























      0














      I seriously have no experience with that DataTable method from jQuery. But you could do something like this:



      $.ajax(
      url: getKeysById,
      success: function(json)
      var t1rows = ;
      var t2rows = ;

      for (var i = 0; i < json.keys.length; i++)
      if (json.keys[i].privileges != '32')
      t1rows.push(json.keys[i]); // Changed section

      else
      t2rows.push(json.keys[i]); // changed section


      callback(t1rows, t2rows);

      );


      var callback = function(data1, data2)
      $('#apiAccessKeyTable').DataTable(columns: ); //add your data here: t1rows
      $('#adminKeysTable').DataTable(columns: ); //add your data here: t2rows;



      You first execute a standalone ajax call and render your tables in the success handler.



      You should look at how to add a data from that jQuery datatable that didnt come from an internal ajax request. I'm assuming it'll be relatively easy. Something like:



      var callback = function(data1, data2)
      $('#adminKeysTable').DataTable(
      dataSrc: data1,
      columns:
      );

      $('#apiAccessKeyTable').DataTable(
      dataSrc: data2,
      columns:
      );



      ?



      This can be even further simplified but for the sake of being relatively similar to your code I'm gonna leave it as is.






      share|improve this answer
































        0














        i have modified a fiddle from answer of this Datatable - Insert JSON data to the table



        You can try something like in this fiddle



        https://jsfiddle.net/gx3ktawn/311/



        HTML



        <button id="getResults">Get Results</button>
        <table id="my_logs"></table>
        <table id="my_logs2"></table>


        Script



        var myTable = $('#my_logs').DataTable(
        "paging": true,
        "lengthChange": true,
        "searching": true,
        "ordering": true,
        "info": true,
        "autoWidth": true,
        "data": ,
        "columns": [
        "title": "Date",
        "data": "log_time"
        ,
        "title": "User Name",
        "data": "user_name"

        ,
        "title": "Class",
        "data": "class_name"

        ,
        "title": "Function",
        "data": "class_function"

        ,
        "title": "Action",
        "data": "action_title"

        ,
        "title": "Description",
        "data": "action_description"

        ]
        );
        var myTable2 = $('#my_logs2').DataTable(
        "paging": true,
        "lengthChange": true,
        "searching": true,
        "ordering": true,
        "info": true,
        "autoWidth": true,
        "data": ,
        "columns": [
        "title": "Date",
        "data": "log_time"
        ,
        "title": "Class",
        "data": "class_name"

        ,
        "title": "Function",
        "data": "class_function"

        ,
        "title": "Description",
        "data": "action_description"

        ]
        );

        $(document).ready(function()
        $("#getResults").click(function()
        $.ajax(
        url: 'https://api.myjson.com/bins/ml2k2',
        success: function(logs)
        myTable.clear();
        $.each(logs, function(index, value)
        myTable.row.add(value);
        );
        myTable.draw();
        myTable2.clear();
        $.each(logs, function(index, value)
        myTable2.row.add(value);
        );
        myTable2.draw();
        ,
        error: function(jqXHR, textStatus, ex)
        alert(textStatus + "," + ex + "," + jqXHR.responseText);

        );
        );
        );





        share|improve this answer






















          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313619%2finitialize-two-different-tables-with-a-single-ajax-call%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Taking different into consideration, once you get data, you can store the data to the different variable and pass the data view to likewise you're passing.



          var tt = $('#apiAccessKeyTable').DataTable( 
          "ajax":
          "url": getKeysById,
          "dataSrc": function(json)
          var t1rows = ;
          var t2rows = ;
          for (var i=0;i<json.keys.length;i++)
          if (json.keys[i].privileges != '32')
          t1rows.push(json.keys[i]); // Changed section
          else
          t2rows.push(json.keys[i]); // changed section



          return rows;

          ,
          "columns": [
          "data": null ,
          "data": "name", "className": "editable" ,
          "data": "key" ,
          "data": "externalUser", "className": "editable" ,
          "data": "privilegesArray", "className": "edit-privileges" ,
          "data": null ,
          "data": null
          ]
          );


          Hope this helps!






          share|improve this answer



























            0














            Taking different into consideration, once you get data, you can store the data to the different variable and pass the data view to likewise you're passing.



            var tt = $('#apiAccessKeyTable').DataTable( 
            "ajax":
            "url": getKeysById,
            "dataSrc": function(json)
            var t1rows = ;
            var t2rows = ;
            for (var i=0;i<json.keys.length;i++)
            if (json.keys[i].privileges != '32')
            t1rows.push(json.keys[i]); // Changed section
            else
            t2rows.push(json.keys[i]); // changed section



            return rows;

            ,
            "columns": [
            "data": null ,
            "data": "name", "className": "editable" ,
            "data": "key" ,
            "data": "externalUser", "className": "editable" ,
            "data": "privilegesArray", "className": "edit-privileges" ,
            "data": null ,
            "data": null
            ]
            );


            Hope this helps!






            share|improve this answer

























              0












              0








              0







              Taking different into consideration, once you get data, you can store the data to the different variable and pass the data view to likewise you're passing.



              var tt = $('#apiAccessKeyTable').DataTable( 
              "ajax":
              "url": getKeysById,
              "dataSrc": function(json)
              var t1rows = ;
              var t2rows = ;
              for (var i=0;i<json.keys.length;i++)
              if (json.keys[i].privileges != '32')
              t1rows.push(json.keys[i]); // Changed section
              else
              t2rows.push(json.keys[i]); // changed section



              return rows;

              ,
              "columns": [
              "data": null ,
              "data": "name", "className": "editable" ,
              "data": "key" ,
              "data": "externalUser", "className": "editable" ,
              "data": "privilegesArray", "className": "edit-privileges" ,
              "data": null ,
              "data": null
              ]
              );


              Hope this helps!






              share|improve this answer













              Taking different into consideration, once you get data, you can store the data to the different variable and pass the data view to likewise you're passing.



              var tt = $('#apiAccessKeyTable').DataTable( 
              "ajax":
              "url": getKeysById,
              "dataSrc": function(json)
              var t1rows = ;
              var t2rows = ;
              for (var i=0;i<json.keys.length;i++)
              if (json.keys[i].privileges != '32')
              t1rows.push(json.keys[i]); // Changed section
              else
              t2rows.push(json.keys[i]); // changed section



              return rows;

              ,
              "columns": [
              "data": null ,
              "data": "name", "className": "editable" ,
              "data": "key" ,
              "data": "externalUser", "className": "editable" ,
              "data": "privilegesArray", "className": "edit-privileges" ,
              "data": null ,
              "data": null
              ]
              );


              Hope this helps!







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 15 '18 at 6:34









              varit05varit05

              1,640816




              1,640816























                  0














                  You do 1 ajax call, split data based on condition and load Datatable with respective JSON



                  Something like this



                  var t1rows = ;
                  var t2rows = ;


                  function splitData()

                  $.ajax(
                  url: getKeysById,
                  success: function(json)
                  for (var i=0; i < json.keys.length ; i++)
                  if (json.keys[i].privileges != '32')
                  t1rows.push(json.keys[i]); // Changed section

                  else
                  t2rows.push(json.keys[i]); // changed section


                  // load Datatables
                  loadDatatable1();
                  loadDatatable2();



                  );



                  function loadDatatable1()

                  $('#apiAccessKeyTable').dataTable(
                  data: t1rows,
                  columns: [
                  data: 'userID' ,
                  data: 'userName'
                  ]
                  );



                  function loadDatatable2()

                  $('#adminKeysTable').dataTable(
                  data: t2rows,
                  columns: [
                  data: 'userID' ,
                  data: 'userName'
                  ]
                  );






                  share|improve this answer



























                    0














                    You do 1 ajax call, split data based on condition and load Datatable with respective JSON



                    Something like this



                    var t1rows = ;
                    var t2rows = ;


                    function splitData()

                    $.ajax(
                    url: getKeysById,
                    success: function(json)
                    for (var i=0; i < json.keys.length ; i++)
                    if (json.keys[i].privileges != '32')
                    t1rows.push(json.keys[i]); // Changed section

                    else
                    t2rows.push(json.keys[i]); // changed section


                    // load Datatables
                    loadDatatable1();
                    loadDatatable2();



                    );



                    function loadDatatable1()

                    $('#apiAccessKeyTable').dataTable(
                    data: t1rows,
                    columns: [
                    data: 'userID' ,
                    data: 'userName'
                    ]
                    );



                    function loadDatatable2()

                    $('#adminKeysTable').dataTable(
                    data: t2rows,
                    columns: [
                    data: 'userID' ,
                    data: 'userName'
                    ]
                    );






                    share|improve this answer

























                      0












                      0








                      0







                      You do 1 ajax call, split data based on condition and load Datatable with respective JSON



                      Something like this



                      var t1rows = ;
                      var t2rows = ;


                      function splitData()

                      $.ajax(
                      url: getKeysById,
                      success: function(json)
                      for (var i=0; i < json.keys.length ; i++)
                      if (json.keys[i].privileges != '32')
                      t1rows.push(json.keys[i]); // Changed section

                      else
                      t2rows.push(json.keys[i]); // changed section


                      // load Datatables
                      loadDatatable1();
                      loadDatatable2();



                      );



                      function loadDatatable1()

                      $('#apiAccessKeyTable').dataTable(
                      data: t1rows,
                      columns: [
                      data: 'userID' ,
                      data: 'userName'
                      ]
                      );



                      function loadDatatable2()

                      $('#adminKeysTable').dataTable(
                      data: t2rows,
                      columns: [
                      data: 'userID' ,
                      data: 'userName'
                      ]
                      );






                      share|improve this answer













                      You do 1 ajax call, split data based on condition and load Datatable with respective JSON



                      Something like this



                      var t1rows = ;
                      var t2rows = ;


                      function splitData()

                      $.ajax(
                      url: getKeysById,
                      success: function(json)
                      for (var i=0; i < json.keys.length ; i++)
                      if (json.keys[i].privileges != '32')
                      t1rows.push(json.keys[i]); // Changed section

                      else
                      t2rows.push(json.keys[i]); // changed section


                      // load Datatables
                      loadDatatable1();
                      loadDatatable2();



                      );



                      function loadDatatable1()

                      $('#apiAccessKeyTable').dataTable(
                      data: t1rows,
                      columns: [
                      data: 'userID' ,
                      data: 'userName'
                      ]
                      );



                      function loadDatatable2()

                      $('#adminKeysTable').dataTable(
                      data: t2rows,
                      columns: [
                      data: 'userID' ,
                      data: 'userName'
                      ]
                      );







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 15 '18 at 6:50









                      MyTwoCentsMyTwoCents

                      3,0992930




                      3,0992930





















                          0














                          I seriously have no experience with that DataTable method from jQuery. But you could do something like this:



                          $.ajax(
                          url: getKeysById,
                          success: function(json)
                          var t1rows = ;
                          var t2rows = ;

                          for (var i = 0; i < json.keys.length; i++)
                          if (json.keys[i].privileges != '32')
                          t1rows.push(json.keys[i]); // Changed section

                          else
                          t2rows.push(json.keys[i]); // changed section


                          callback(t1rows, t2rows);

                          );


                          var callback = function(data1, data2)
                          $('#apiAccessKeyTable').DataTable(columns: ); //add your data here: t1rows
                          $('#adminKeysTable').DataTable(columns: ); //add your data here: t2rows;



                          You first execute a standalone ajax call and render your tables in the success handler.



                          You should look at how to add a data from that jQuery datatable that didnt come from an internal ajax request. I'm assuming it'll be relatively easy. Something like:



                          var callback = function(data1, data2)
                          $('#adminKeysTable').DataTable(
                          dataSrc: data1,
                          columns:
                          );

                          $('#apiAccessKeyTable').DataTable(
                          dataSrc: data2,
                          columns:
                          );



                          ?



                          This can be even further simplified but for the sake of being relatively similar to your code I'm gonna leave it as is.






                          share|improve this answer





























                            0














                            I seriously have no experience with that DataTable method from jQuery. But you could do something like this:



                            $.ajax(
                            url: getKeysById,
                            success: function(json)
                            var t1rows = ;
                            var t2rows = ;

                            for (var i = 0; i < json.keys.length; i++)
                            if (json.keys[i].privileges != '32')
                            t1rows.push(json.keys[i]); // Changed section

                            else
                            t2rows.push(json.keys[i]); // changed section


                            callback(t1rows, t2rows);

                            );


                            var callback = function(data1, data2)
                            $('#apiAccessKeyTable').DataTable(columns: ); //add your data here: t1rows
                            $('#adminKeysTable').DataTable(columns: ); //add your data here: t2rows;



                            You first execute a standalone ajax call and render your tables in the success handler.



                            You should look at how to add a data from that jQuery datatable that didnt come from an internal ajax request. I'm assuming it'll be relatively easy. Something like:



                            var callback = function(data1, data2)
                            $('#adminKeysTable').DataTable(
                            dataSrc: data1,
                            columns:
                            );

                            $('#apiAccessKeyTable').DataTable(
                            dataSrc: data2,
                            columns:
                            );



                            ?



                            This can be even further simplified but for the sake of being relatively similar to your code I'm gonna leave it as is.






                            share|improve this answer



























                              0












                              0








                              0







                              I seriously have no experience with that DataTable method from jQuery. But you could do something like this:



                              $.ajax(
                              url: getKeysById,
                              success: function(json)
                              var t1rows = ;
                              var t2rows = ;

                              for (var i = 0; i < json.keys.length; i++)
                              if (json.keys[i].privileges != '32')
                              t1rows.push(json.keys[i]); // Changed section

                              else
                              t2rows.push(json.keys[i]); // changed section


                              callback(t1rows, t2rows);

                              );


                              var callback = function(data1, data2)
                              $('#apiAccessKeyTable').DataTable(columns: ); //add your data here: t1rows
                              $('#adminKeysTable').DataTable(columns: ); //add your data here: t2rows;



                              You first execute a standalone ajax call and render your tables in the success handler.



                              You should look at how to add a data from that jQuery datatable that didnt come from an internal ajax request. I'm assuming it'll be relatively easy. Something like:



                              var callback = function(data1, data2)
                              $('#adminKeysTable').DataTable(
                              dataSrc: data1,
                              columns:
                              );

                              $('#apiAccessKeyTable').DataTable(
                              dataSrc: data2,
                              columns:
                              );



                              ?



                              This can be even further simplified but for the sake of being relatively similar to your code I'm gonna leave it as is.






                              share|improve this answer















                              I seriously have no experience with that DataTable method from jQuery. But you could do something like this:



                              $.ajax(
                              url: getKeysById,
                              success: function(json)
                              var t1rows = ;
                              var t2rows = ;

                              for (var i = 0; i < json.keys.length; i++)
                              if (json.keys[i].privileges != '32')
                              t1rows.push(json.keys[i]); // Changed section

                              else
                              t2rows.push(json.keys[i]); // changed section


                              callback(t1rows, t2rows);

                              );


                              var callback = function(data1, data2)
                              $('#apiAccessKeyTable').DataTable(columns: ); //add your data here: t1rows
                              $('#adminKeysTable').DataTable(columns: ); //add your data here: t2rows;



                              You first execute a standalone ajax call and render your tables in the success handler.



                              You should look at how to add a data from that jQuery datatable that didnt come from an internal ajax request. I'm assuming it'll be relatively easy. Something like:



                              var callback = function(data1, data2)
                              $('#adminKeysTable').DataTable(
                              dataSrc: data1,
                              columns:
                              );

                              $('#apiAccessKeyTable').DataTable(
                              dataSrc: data2,
                              columns:
                              );



                              ?



                              This can be even further simplified but for the sake of being relatively similar to your code I'm gonna leave it as is.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 15 '18 at 6:52

























                              answered Nov 15 '18 at 6:41









                              Abana ClaraAbana Clara

                              1,666920




                              1,666920





















                                  0














                                  i have modified a fiddle from answer of this Datatable - Insert JSON data to the table



                                  You can try something like in this fiddle



                                  https://jsfiddle.net/gx3ktawn/311/



                                  HTML



                                  <button id="getResults">Get Results</button>
                                  <table id="my_logs"></table>
                                  <table id="my_logs2"></table>


                                  Script



                                  var myTable = $('#my_logs').DataTable(
                                  "paging": true,
                                  "lengthChange": true,
                                  "searching": true,
                                  "ordering": true,
                                  "info": true,
                                  "autoWidth": true,
                                  "data": ,
                                  "columns": [
                                  "title": "Date",
                                  "data": "log_time"
                                  ,
                                  "title": "User Name",
                                  "data": "user_name"

                                  ,
                                  "title": "Class",
                                  "data": "class_name"

                                  ,
                                  "title": "Function",
                                  "data": "class_function"

                                  ,
                                  "title": "Action",
                                  "data": "action_title"

                                  ,
                                  "title": "Description",
                                  "data": "action_description"

                                  ]
                                  );
                                  var myTable2 = $('#my_logs2').DataTable(
                                  "paging": true,
                                  "lengthChange": true,
                                  "searching": true,
                                  "ordering": true,
                                  "info": true,
                                  "autoWidth": true,
                                  "data": ,
                                  "columns": [
                                  "title": "Date",
                                  "data": "log_time"
                                  ,
                                  "title": "Class",
                                  "data": "class_name"

                                  ,
                                  "title": "Function",
                                  "data": "class_function"

                                  ,
                                  "title": "Description",
                                  "data": "action_description"

                                  ]
                                  );

                                  $(document).ready(function()
                                  $("#getResults").click(function()
                                  $.ajax(
                                  url: 'https://api.myjson.com/bins/ml2k2',
                                  success: function(logs)
                                  myTable.clear();
                                  $.each(logs, function(index, value)
                                  myTable.row.add(value);
                                  );
                                  myTable.draw();
                                  myTable2.clear();
                                  $.each(logs, function(index, value)
                                  myTable2.row.add(value);
                                  );
                                  myTable2.draw();
                                  ,
                                  error: function(jqXHR, textStatus, ex)
                                  alert(textStatus + "," + ex + "," + jqXHR.responseText);

                                  );
                                  );
                                  );





                                  share|improve this answer



























                                    0














                                    i have modified a fiddle from answer of this Datatable - Insert JSON data to the table



                                    You can try something like in this fiddle



                                    https://jsfiddle.net/gx3ktawn/311/



                                    HTML



                                    <button id="getResults">Get Results</button>
                                    <table id="my_logs"></table>
                                    <table id="my_logs2"></table>


                                    Script



                                    var myTable = $('#my_logs').DataTable(
                                    "paging": true,
                                    "lengthChange": true,
                                    "searching": true,
                                    "ordering": true,
                                    "info": true,
                                    "autoWidth": true,
                                    "data": ,
                                    "columns": [
                                    "title": "Date",
                                    "data": "log_time"
                                    ,
                                    "title": "User Name",
                                    "data": "user_name"

                                    ,
                                    "title": "Class",
                                    "data": "class_name"

                                    ,
                                    "title": "Function",
                                    "data": "class_function"

                                    ,
                                    "title": "Action",
                                    "data": "action_title"

                                    ,
                                    "title": "Description",
                                    "data": "action_description"

                                    ]
                                    );
                                    var myTable2 = $('#my_logs2').DataTable(
                                    "paging": true,
                                    "lengthChange": true,
                                    "searching": true,
                                    "ordering": true,
                                    "info": true,
                                    "autoWidth": true,
                                    "data": ,
                                    "columns": [
                                    "title": "Date",
                                    "data": "log_time"
                                    ,
                                    "title": "Class",
                                    "data": "class_name"

                                    ,
                                    "title": "Function",
                                    "data": "class_function"

                                    ,
                                    "title": "Description",
                                    "data": "action_description"

                                    ]
                                    );

                                    $(document).ready(function()
                                    $("#getResults").click(function()
                                    $.ajax(
                                    url: 'https://api.myjson.com/bins/ml2k2',
                                    success: function(logs)
                                    myTable.clear();
                                    $.each(logs, function(index, value)
                                    myTable.row.add(value);
                                    );
                                    myTable.draw();
                                    myTable2.clear();
                                    $.each(logs, function(index, value)
                                    myTable2.row.add(value);
                                    );
                                    myTable2.draw();
                                    ,
                                    error: function(jqXHR, textStatus, ex)
                                    alert(textStatus + "," + ex + "," + jqXHR.responseText);

                                    );
                                    );
                                    );





                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      i have modified a fiddle from answer of this Datatable - Insert JSON data to the table



                                      You can try something like in this fiddle



                                      https://jsfiddle.net/gx3ktawn/311/



                                      HTML



                                      <button id="getResults">Get Results</button>
                                      <table id="my_logs"></table>
                                      <table id="my_logs2"></table>


                                      Script



                                      var myTable = $('#my_logs').DataTable(
                                      "paging": true,
                                      "lengthChange": true,
                                      "searching": true,
                                      "ordering": true,
                                      "info": true,
                                      "autoWidth": true,
                                      "data": ,
                                      "columns": [
                                      "title": "Date",
                                      "data": "log_time"
                                      ,
                                      "title": "User Name",
                                      "data": "user_name"

                                      ,
                                      "title": "Class",
                                      "data": "class_name"

                                      ,
                                      "title": "Function",
                                      "data": "class_function"

                                      ,
                                      "title": "Action",
                                      "data": "action_title"

                                      ,
                                      "title": "Description",
                                      "data": "action_description"

                                      ]
                                      );
                                      var myTable2 = $('#my_logs2').DataTable(
                                      "paging": true,
                                      "lengthChange": true,
                                      "searching": true,
                                      "ordering": true,
                                      "info": true,
                                      "autoWidth": true,
                                      "data": ,
                                      "columns": [
                                      "title": "Date",
                                      "data": "log_time"
                                      ,
                                      "title": "Class",
                                      "data": "class_name"

                                      ,
                                      "title": "Function",
                                      "data": "class_function"

                                      ,
                                      "title": "Description",
                                      "data": "action_description"

                                      ]
                                      );

                                      $(document).ready(function()
                                      $("#getResults").click(function()
                                      $.ajax(
                                      url: 'https://api.myjson.com/bins/ml2k2',
                                      success: function(logs)
                                      myTable.clear();
                                      $.each(logs, function(index, value)
                                      myTable.row.add(value);
                                      );
                                      myTable.draw();
                                      myTable2.clear();
                                      $.each(logs, function(index, value)
                                      myTable2.row.add(value);
                                      );
                                      myTable2.draw();
                                      ,
                                      error: function(jqXHR, textStatus, ex)
                                      alert(textStatus + "," + ex + "," + jqXHR.responseText);

                                      );
                                      );
                                      );





                                      share|improve this answer













                                      i have modified a fiddle from answer of this Datatable - Insert JSON data to the table



                                      You can try something like in this fiddle



                                      https://jsfiddle.net/gx3ktawn/311/



                                      HTML



                                      <button id="getResults">Get Results</button>
                                      <table id="my_logs"></table>
                                      <table id="my_logs2"></table>


                                      Script



                                      var myTable = $('#my_logs').DataTable(
                                      "paging": true,
                                      "lengthChange": true,
                                      "searching": true,
                                      "ordering": true,
                                      "info": true,
                                      "autoWidth": true,
                                      "data": ,
                                      "columns": [
                                      "title": "Date",
                                      "data": "log_time"
                                      ,
                                      "title": "User Name",
                                      "data": "user_name"

                                      ,
                                      "title": "Class",
                                      "data": "class_name"

                                      ,
                                      "title": "Function",
                                      "data": "class_function"

                                      ,
                                      "title": "Action",
                                      "data": "action_title"

                                      ,
                                      "title": "Description",
                                      "data": "action_description"

                                      ]
                                      );
                                      var myTable2 = $('#my_logs2').DataTable(
                                      "paging": true,
                                      "lengthChange": true,
                                      "searching": true,
                                      "ordering": true,
                                      "info": true,
                                      "autoWidth": true,
                                      "data": ,
                                      "columns": [
                                      "title": "Date",
                                      "data": "log_time"
                                      ,
                                      "title": "Class",
                                      "data": "class_name"

                                      ,
                                      "title": "Function",
                                      "data": "class_function"

                                      ,
                                      "title": "Description",
                                      "data": "action_description"

                                      ]
                                      );

                                      $(document).ready(function()
                                      $("#getResults").click(function()
                                      $.ajax(
                                      url: 'https://api.myjson.com/bins/ml2k2',
                                      success: function(logs)
                                      myTable.clear();
                                      $.each(logs, function(index, value)
                                      myTable.row.add(value);
                                      );
                                      myTable.draw();
                                      myTable2.clear();
                                      $.each(logs, function(index, value)
                                      myTable2.row.add(value);
                                      );
                                      myTable2.draw();
                                      ,
                                      error: function(jqXHR, textStatus, ex)
                                      alert(textStatus + "," + ex + "," + jqXHR.responseText);

                                      );
                                      );
                                      );






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 15 '18 at 7:23









                                      RAGINROSERAGINROSE

                                      1678




                                      1678



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313619%2finitialize-two-different-tables-with-a-single-ajax-call%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          這個網誌中的熱門文章

                                          Barbados

                                          How to read a connectionString WITH PROVIDER in .NET Core?

                                          Node.js Script on GitHub Pages or Amazon S3