How can I get DataTables to work with my JSON response?

How can I get DataTables to work with my JSON response?

SevenDoorknobsSevenDoorknobs Posts: 2Questions: 1Answers: 0

I am attempting to get DataTables to read from my websites API but I am unable to get anything. I'd hope to include the object name, but currently I am just trying to get any data to appear. Any help would be great!

Sample JSON Response Data
{"2e2PbV3135":{"paid":"20000000000","hashes":"106834406","hashrate":0},"Z5R9c5tCtT":{"paid":"5000000000","hashes":"20893336","hashrate":0},"QbrGu6LjBP":{"paid":"5000000000","hashes":"34178052","hashrate":0},"54f5VCkR9u":{"paid":null,"hashes":"127141","hashrate":0},"eK4E8Yr9ML":{"paid":"5000000000","hashes":"15952318","hashrate":0},"Hr5418SmZN":{"paid":null,"hashes":"407911","hashrate":0},"2d2PehuPPd":{"paid":null,"hashes":"954983","hashrate":0},"rurUeFfCE3":{"paid":null,"hashes":"59931","hashrate":0},"T5fJLAMZ7w":{"paid":null,"hashes":"635408","hashrate":0},"T5fJJrv3mp":{"paid":"15000000000","hashes":"64738597","hashrate":0},"T5fJGJPQYw":{"paid":"295000000000","hashes":"1024773831","hashrate":0},"T5fJGV76Dr":{"paid":"20000000000","hashes":"126989278","hashrate":0},"sA8M2XYgSy":{"paid":null,"hashes":"1500","hashrate":0},"T5fJLVM7Rs":{"paid":null,"hashes":"1235515","hashrate":0},"rPpBixW6qG":{"paid":null,"hashes":"177288","hashrate":0},"2d2PaCFJrg":{"paid":"35000000000","hashes":"141972205","hashrate":0},"d1V3i98hjC":{"paid":"35000000000","hashes":"156579499","hashrate":0},"niDGJKBqMQ":{"paid":"2245000000000","hashes":"10096704433","hashrate":0},"epP2PtDYPp":{"paid":null,"hashes":"25893","hashrate":0},"AiFUGkixrb":{"paid":"30000000000","hashes":"34317861","hashrate":0},"2d2PaJXv2g":{"paid":null,"hashes":"464905","hashrate":0},"CC97oF8K6v":{"paid":null,"hashes":"17414","hashrate":0},"eTDFnNXMuM":{"paid":null,"hashes":"5448","hashrate":0},"CC97qJs7n9":{"paid":"10000000000","hashes":"53349632","hashrate":0},"6nB1NYrx4y":{"paid":null,"hashes":"26500","hashrate":0},"2d2PgAPB1C":{"paid":null,"hashes":"40000","hashrate":0},"2d2Pab1qS4":{"paid":"25000000000","hashes":"80325786","hashrate":0},"Mmf8o41Joi":{"paid":null,"hashes":"93104","hashrate":0},"CC97oLdZ8u":{"paid":"10000000000","hashes":"13374131","hashrate":0},"pn3CZ8oLa3":{"paid":null,"hashes":"500","hashrate":0},"T5fJJdfQK7":{"paid":null,"hashes":"5500","hashrate":0},"8VJAcJpjEJ":{"paid":null,"hashes":"1292061","hashrate":0},"BQRS3VEXFV":{"paid":null,"hashes":"744737","hashrate":0},"T5fJL33gYp":{"paid":null,"hashes":"1324042","hashrate":0},"T5fJJ6CEiV":{"paid":"75000000000","hashes":"353518027","hashrate":0},"T5fJMKpTVd":{"paid":null,"hashes":"1569149","hashrate":0},"T5fJGacbEq":{"paid":null,"hashes":"115272","hashrate":0},"2d2PgEqaFj":{"paid":"30000000000","hashes":"149577943","hashrate":0},"JuRSMic2a9":{"paid":null,"hashes":"219500","hashrate":0},"Yx22R6P6sb":{"paid":null,"hashes":"4115986","hashrate":0},"CC97o693th":{"paid":"5000000000","hashes":"23973646","hashrate":0},"2d2Pa1Gt8d":{"paid":null,"hashes":"692","hashrate":0},"FrgTYMaEX3":{"paid":null,"hashes":"8852460","hashrate":0},"67WBcnJWcj":{"paid":null,"hashes":"71605","hashrate":0},"NeW3sFf5iv":{"paid":null,"hashes":"122000","hashrate":0},"T5fJHRmwZY":{"paid":"70000000000","hashes":"156604515","hashrate":0}}

Answers

  • Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10

    It expects that in an array named "data".

    Look here under Data source types: https://datatables.net/manual/data/

  • kthorngrenkthorngren Posts: 21,302Questions: 26Answers: 4,947

    If you are unable to provide the data in the format @Loren Maxwell notes then, assuming you are using ajax in your Datatable config, you can use ajax.dataSrc to create a function to loop through the data to return it as an array of objects.

    Kevin

  • SevenDoorknobsSevenDoorknobs Posts: 2Questions: 1Answers: 0

    How could I point ajax.dataSrc to point to the correct property name, since they are at the root. I tried "_root" but it was unsuccessful. Thanks for your tips guys!

  • kthorngrenkthorngren Posts: 21,302Questions: 26Answers: 4,947
    edited February 2018

    To answer your question you would use dataSrc: "". But since your data is not an array of objects you will actually need to use a function to change the structure to an array structure. So it looks more like this:

    [
    {name:"2e2PbV3135","paid":"20000000000","hashes":"106834406","hashrate":0},
    {name:"Z5R9c5tCtT","paid":"5000000000","hashes":"20893336","hashrate":0},
    ....
    {name:"T5fJHRmwZY","paid":"70000000000","hashes":"156604515","hashrate":0}
    ]
    

    If you want the key (2e2PbV3135 for example) as part of the table it will need to be part of the object, like name in my example data.

    Haven't tried this so it may not work without some debugging but here is an example to start with:

      "ajax": {
        "url": "myUrl",
        "dataSrc": function ( json ) {
          var data = [];
          for (var item in json) {
             var obj = json[item];
             obj.name = item;
             data.push(obj);
          }
          return data;
        }
    }
    

    Kevin

  • Loren MaxwellLoren Maxwell Posts: 406Questions: 99Answers: 10
    edited February 2018

    If you are able to do this on the back end, then here's what I do:

    /* This file name is "ajaxdata.php" */
    <?php
    
    // DataTables PHP library
    include "../../Editor/php/DataTables.php"; /* Adjust your path here */
    
    use
     DataTables\Database,
     DataTables\Database\Query,
     DataTables\Database\Result;
    
    $sql = "SELECT fields FROM table";
    
    $results = $db->sql( $sql )->fetchAll();
    
    $data['data'] = array_merge( $results );
    
    echo json_encode( $data );
    

    Then I do an ajax call to "ajaxdata.php" and don't use dataSrc at all.

This discussion has been closed.