I went through almost all the documentation but couldn't solve my problem for Datatables grid

I went through almost all the documentation but couldn't solve my problem for Datatables grid

mvelioglumvelioglu Posts: 2Questions: 1Answers: 0
edited August 2021 in DataTables

Link to test case: https://entellig.com/yenisistem/?ref=efeveli+8cc661701fcdcc322db14830c40128f2zzz
Debugger code (debug.datatables.net):
Error messages shown: dont show datatables
Description of problem: I was using datatables as DOM by filtering with the foreach command on my site before. However, it was preparing the data with too many rows in a very long time. That's why I decided to bring it as json or server-side. I am fetching my data on the relevant page as json. This data is not static. It changes according to the search criteria of my visitors.Transferring the incoming data to the server side is meaningless to wait for a response again, and it is meaningless because it will delay the response.On the SAME PAGE how can I immediately pass this data to the datatables grid.
Sorry, I don't understand too much javascript code. I would be grateful in advance to anyone who can help. My codes are below.

<?php
    
    $q = $baglanti3->prepare($aranacak_sorgu);
    $q->execute();
    $rows = array();
    $i = 1;
    while ($r = $q->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT, PDO::FETCH_COLUMN)){
        
    $rows[] = array(
       
                "A_m_turu" => $r[3],
                "A_m_tarih" => $r[1],           
                "A_m_evsahibi" => $r[4],
                "A_m_deplasman" => $r[5],
                
  
    );
    $i++;   
    
    }

$rt = (STRING) $q->rowCount();
$data = array(
        "draw" => 2,
        "recordsTotal" => $rt,
        "recordsFiltered" => $rt,
        "data" => $rows
    );
$gelen_veri =  json_encode($data);

echo $gelen_veri;
    
    
    
    ?>

No problem so far. The data comes in the format datatables wants. variable value from json data php $gelen_veri

My question is what should I do next. I tried all kinds of ways and failed.

<input type="hidden" name="json_veri" id="json_veri" value="<?php echo $gelen_veri; ?>" />
    
    
    
    

    <table id="example" class="display" style="width:100%">
        <thead>
 
            <tr>
            
                <th>Lig</th>
                <th>Tarih</th>
                <th>Ev Sahibi</th>
                <th>Deplasman</th>

            
                

            </tr>
            
        
        </thead>
       <tfoot>
 
            <tr>
            
                <th>Lig</th>
                <th>Tarih</th>
                <th>Ev Sahibi</th>
                <th>Deplasman</th>
                
            </tr>
            
        
        </tfoot>
    </table>
<script>
$(document).ready(function() {
var json_veri = $("#json_veri").val();  
data = JSON.parse(json_veri);   
    $('#example').DataTable( {


///WHAT TO WRITE HERE?//

    data: data,
    columns: [
            { data: "A_m_turu" },
            { data: "A_m_tarih" },
            { data: "A_m_evsahibi" },
            { data: "A_m_deplasman" }
        ],
        
    } );
} );
</script>

This page does not need to send data anywhere else.

My purpose is to just place the json data in the $gelen_veri into the Grid.

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    There's a lot going on there.

    It looks like you're trying to generate serverSide code on the server, but you're hard coding the draw to 2 - this should match what's in the request. The protocol is discussed here. Also see examples here.

    However, you've haven't enabled serverSide on the client - and you may not need it if you have less that 10-20k records in your data set. But, you also haven't defined ajax, to get the client requesting data from the server - see examples here on that.

    Cheers,

    Colin

  • mvelioglumvelioglu Posts: 2Questions: 1Answers: 0

    @colin thank you for answer. What should the draw value be? php variable $gelen_veri in json schema required sounds correct. But unable to push example database table with javascript code. What kind of way should I follow, the data should be loaded very quickly. Users should be able to see even 250,000 lines from time to time and it should be fast to load. Here, json objects are already created with mysql query. Is it necessary to post this on another page and get it collated and returned there?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    What should the draw value be?

    From the server-side docs:

    The draw counter that this object is a response to - from the draw parameter sent as part of the data request. Note that it is strongly recommended for security reasons that you cast this parameter to an integer, rather than simply echoing back to the client what it sent in the draw parameter, in order to prevent Cross Site Scripting (XSS) attacks.

    With 250k records, server-side processing sounds like the right approach. See this page for an example.

    Since you are using PHP, you might want to consider using Editor's PHP libraries to do the server-side processing for you. See this blog post.

    Allan

Sign In or Register to comment.