KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

New pdo DataSource for Datagrid #1773

Open ankit raj opened this topic on on Dec 18, 2020 - 8 comments

ankit raj commented on Dec 18, 2020

Hi , I need to use server side features for datagrid , but as i was informed , it needs a datasource to connect to sql rather than json. Hence i extended the datasource using example but i only get the column header correct and not the column data. Can you tell what is going wrong?

file:

  \koolreport\datagrid\DataTables::create(array(
            "themeBase"=>"bs4",
            "serverSide"=>true,
            "ajax"=>'strEventId=5',
            "method"=>'post',
            "cssClass"=>array(
                "table"=>"table table-striped table-bordered"
            ),
            "dataSource"=>function($scope) {
          
                return new MyNewDataSource($this->vDbConn);
                
                
                
            },
            "showFooter"=>true,
            "options"=>array(
                "order" => [],
                "bInfo" => false,
                "dom" => 'Bfrtip',
                "buttons" => [
                    'csv', 'excel'
                ],
              
                "pageLength"=>10,
                "pageIndex"=>0,
            )
        ));

And this is my new datasource :

<?php
require_once("../vendor/autoload.php");

class MyNewDataSource extends \koolreport\core\DataSource
{
    private $rs;
    
    /**
     * Be called when datasource is initiated
     * 
     * @return null
     */
    protected function onInit()
    {
        // This method is called when datasource is initated
        // You may get all the parameters of datasource through $this->params
    }

    public function __construct($vDbConn)
    {
        print_r($vDbConn);
        parent::__construct();
        $stmt =  'select Partner_Name from TBL_DUMMY_PARTNER_DATA';
        $res =  $vDbConn->execute_query ( $stmt, $err_msg, $affected_rows );
        $this->rs = $res;
    }




    /**
     * Start piping data
     * 
     * @return null
     */
    public function start()
    {
        $metaData = array(
            "columns"=>array(
                "Partner_Name"=>array(
                    "type"=>"string"
                ),
               
            )
                );
        // Everything start with sending meta data
        $this->sendMeta($metaData,$this);

        //Call startInput() to begin data pipe
        $this->startInput(null);

        //Loop through your data and use next() to send row by row of data
        while($row= $this->rs->fetch_assoc())
        {
            // var_dump($row);
            $this->next($row);
        }

        //At the end, call endInput() to close the data pipe
        $this->endInput(null);
    }
}
?>

vDbConn is my CPDO Library object , and fetch_assoc is a wrapper to fetch data in associative array. I Went and printed the data in Node.php and i am getting it but the output of dataGrid is only the table header is coming with no table data. Please help!

Sebastian Morales commented on Dec 21, 2020

Pls try to add the following method to your datasource:

    public function queryProcessing($queryParams) 
    {
        print_r($queryParams);
        //... modify your datasource query based on $queryParams which contains parameters about paging, filtering, sorting of current DataTables' page
        return $this;
    } 
ankit raj commented on Dec 21, 2020

Hi, i added this but i am still getting datatables warning and the print which u added in that function is giving this: Array ( [start] => 0 [length] => 1 [searchParams] => Array ( ) [search] => 1=1 AND (1=1) AND (1=1 ) [countTotal] => 1 [countFilter] => 1 )

Sebastian Morales commented on Dec 21, 2020

Sorry, pls comment out the print_r($queryParams); command. Also check for the tag <dt-ajax in your browser developer tool's Network tab's XHR request's response to see if there's any data in it.

ankit raj commented on Dec 21, 2020

Hi , I commented the print. In the ajax request , i am getting this response:

<script type="text/javascript">
    KoolReport.widget.init(
        {"js":["\path\jquery\/jquery.min.js",["\path\/datatables.min.js",["\path\/DataTables\/pagination\/input.js","\/\path\/DataTables\/datatables.bs4.min.js"]]],"css":["\pathname/datatables.bs4.min.css"]},
        function() {
            
            var name = 'datatables5fe055918180b1';
            var dt = window[name] = $('#' + name).DataTable({"searching":false,"paging":false,"order":[],"bInfo":false,"dom":"Bfrtip","buttons":["csv","excel"],"pageLength":10,"pageIndex":0,"serverSide":true,"ajax":{"url":"","data":function(d) {
                        d.id = 'datatables5fe055918180b1';
                        d.scope = [];
                    },"type":"POST","dataFilter":function(data) {
                        var markStart = "<dt-ajax id='dt_datatables5fe055918180b1'>";
                        var markEnd = '</dt-ajax>';
                        var start = data.indexOf(markStart);
                        var end = data.indexOf(markEnd);
                        var s = data.substring(start + markStart.length, end);
                        return s;
                    }},"columns":[{"data":"Partner_Name"}]});

                        
            
            
                                }
    );
</script>

I have put a var dump inside node.php and that is displaying each partner name through each iteration :

    public function next($data)
    {
        var_dump($data);
        if ($this->destinations!=null) {
            foreach ($this->destinations as $node) {
                print('inside');
                var_dump($data);
                $node->input($data, $this);
            }
        }
    }

And i am able to see each result in the UI var_dump but no data in datatable :

e/src/core/Node.php:203:
array (size=1)
  'Partner_Name' => string 'abc' (length=3)
Sebastian Morales commented on Dec 21, 2020

Pls look for the dt-ajax tag in the html part, not script part of your ajax response. It's where the ajax data is.

ankit raj commented on Dec 21, 2020

Hi, Its only present at the script part and not anywhere inside html. But when i comment server side =>'true' in my code , i am able to see the data. Hence the new datasource is working properly , but not when server side is turned on. So, is there any other way to create server side datatable pagination by passing a pdo connection object instead of username , password in a settings file , because my vdbconn is a pdo object itself. when i print it , this is the response:

CPDOLibrary Object ( [start_time:CPDOLibrary:private] => 0 [end_time:CPDOLibrary:private] => 0 [time_limit:CPDOLibrary:private] => 10 [status_code] => 0 [affected_rows] => [error_msg] => [param_arr:CPDOLibrary:private] => Array ( ) [email_addr:CPDOLibrary:private] => [email_flag:CPDOLibrary:private] => [debug_mode:CPDOLibrary:private] => [file_log:CPDOLibrary:private] => [preserve:CPDOLibrary:private] => [in_clause_counter:CPDOLibrary:private] => 1 )

I just want to know what should i write in datasource assuming its a simple select * query. Currently i am writing this and getting the header names but no result.:

  "dataSource"=>function($scope) {
                // print_r($this->vDbConn);
                $stmt =  'select * from TBL_DUMMY_PARTNER_DATA';
                $res =  $this->vDbConn->execute_query ( $stmt, $err_msg, $affected_rows );
                // var_dump($res->fetch_assoc());
                return $res;
                // return new MyNewDataSource($this->vDbConn);
                
                
                
            }
Sebastian Morales commented on Dec 21, 2020

Oh, in case your vdbconn is a pdo object, how about your custom datasource class extending \koolreport\datasources\PdoDataSource intead of \koolreport\core\DataSource?

ankit raj commented on Dec 21, 2020

Hi , i tried extending it but getting the same header with no body and an error in console. Attached image:

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

DataGrid