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 ?

5
  • As you are getting JSON format data from API, I think you should decode. Try this var_dump(json_decode($result, true)); php.net/manual/en/function.json-decode.php Commented Nov 3, 2017 at 4: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 ? Commented Nov 3, 2017 at 4: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. Commented Nov 3, 2017 at 4:42
  • stackoverflow.com/questions/28473055/… Commented Nov 3, 2017 at 4: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 ? Commented Nov 3, 2017 at 5:53

1 Answer 1

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"
    } );
} );
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the response San K,i already know the client side,the problem is from the serverside
@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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.