0

so i am doing dashboard project,and got a vendor table using dataTables,and fetch data to table from API,the API will give json data,but i kinda confuse,because it's my first time fill the table with api data,usually i fetch the data from local database,and because there's many reference with sql fetching

here's the example API Json data :

{"produkList":[{"product_code":"XXXXX","ticket":"UD","numbers":"1200","price": 20,"verification":true},{"produk_code":"XXXXXX","ticket":"UD","numbers":"4000","price":120,"verification":false}]}

datatables js (view) :

table = $('#table').DataTable({ 

        "processing": true, //Feature control the processing indicator.
        "serverSide": true, //Feature control DataTables' server-side processing mode.
        "order": [], //Initial no order.

        // Load data for the table's content from an Ajax source
        "ajax": {
            "url": "<?php echo site_url('dashboard/ajax_list')?>",
            "type": "POST"
        },

        //Set column definition initialisation properties.
        "columnDefs": [
        { 
            "targets": [ -1 ], //last column
            "orderable": false, //set not orderable
        },
        ],

    });

and the controller (Dashboard.php)

public function ajax_list()
{

  $curl = curl_init("http://example.com/dashboard/APIget.php");      
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");     
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
    curl_setopt($curl, CURLOPT_POSTFIELDS); 
    $result = curl_exec($curl);    
    curl_close($curl);     
    echo json_encode($result); 
}

and for the models,i still haven't got idea,because i only know to fetch from local database,and i know that i can get the data from API without serverside processing,but the result is that the bigger the data,the site will taking to long to load,so anyone know how ?

Prem
  • 15
  • 1
  • 10
  • As you are getting JSON format data from API, I think you should decode. Try this `var_dump(json_decode($result, true));` http://php.net/manual/en/function.json-decode.php – psuresh Nov 03 '17 at 04:28
  • thanks for quick response @SureshPokharel,but how about the another functionality of the datatables,like search,pagination,and others,is it will automatically been loaded ? – Prem Nov 03 '17 at 04:32
  • There must be a large amount of data so that it's taking a long time. To get small data at first and load them later is needed, you have to make changes in API. If you're using third party's API, there must be some provisions for that. Maybe you need to go after their documentation. – psuresh Nov 03 '17 at 04:42
  • https://stackoverflow.com/questions/28473055/json-error-on-using-jquery-datatable – Praveen Kumar Nov 03 '17 at 04:50
  • but that's the problem,the API i got is not in my reach,it's other vendor's API,is there any other way ? – Prem Nov 03 '17 at 05:53

1 Answers1

1

example: https://datatables.net/examples/data_sources/server_side.html

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "../server_side/scripts/server_processing.php"
    } );
} );
San K
  • 223
  • 1
  • 5
  • thanks for the response San K,i already know the client side,the problem is from the serverside – Prem Nov 03 '17 at 04:26
  • @Prem if you look at the link San put above there is an example server side script, which is written in PHP. If you are not sure what data is being sent by datatables to the server use the browsers inspector to check it. Then you can test changing pages, searching etc etc to see what values change. – mic Nov 03 '17 at 10:20
  • thank you mic,yes i got your point,but if the data is from sql,we can generally edit the serverside tables,so that the filter,search,pagination,etc can be done in serverside code,but what if the source came from API,and i don't have the authority of it ? because the problem is that if i done that without serverside processing,the performance of my dashboard will become slow,and i have try that myself,from the API,the data is so big and it's take a loong time for web to be fully loaded – Prem Nov 03 '17 at 10:43