KoolReport's Forum

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

DataGrid - Server side #2694

Open Ahmed Bucheeri opened this topic on on May 24, 2022 - 2 comments

Ahmed Bucheeri commented on May 24, 2022

I have the following code

<?php

  DataTables::create(array(
      "name"=>"userstablelms",
      "dataSource"=>$this->dataStore("manageuserslist"),
      "dataSource" => function() {
          return $this->src('etarab')
          ->query("SELECT	a.id,
                		    a.firstname,
                        a.lastname,
                        a.email,
                        b.data AS company,
                        a.country AS countryCode,
                        a.phone1 AS phone,
                        a.phone2 AS mobile
                	FROM mdl_user a
                  JOIN mdl_user_info_data b
              			ON a.id = b.userid
              		JOIN mdl_user_info_field c
              			ON b.fieldid = c.id and c.shortname = 'companyname'
                	WHERE a.deleted = 0 AND a.email NOT LIKE '%gaa.aero%' ORDER BY a.firstname ASC, a.lastname ASC")
          ->pipe(new CalculatedColumn(array(
            "country"=>array(
              "exp"=>function($data){
                if ( $data['countryCode'] ) {
                  $file = '../scripts/codes.json';
                  $json = file_get_contents($file);
                  $json_data = json_decode($json, true);
                  $result = $json_data[array_search( $data['countryCode'] , array_column($json_data, 'code'))];
                  return $result['name'];
                }
                  return null;
              }
            )
          )));
      },
      "method"=>"post",
      "serverSide"=>true,
      "searchOnEnter" => true,
      "responsive" => true,
      "themeBase" => "bs4",
      "options"=>array(
        "fastRender" => true,
        "searching" => true,
        "ordering" => true,
        "paging" => true,
        "mark" => true,
        "lengthMenu" => [[15, 30, 50, 100, -1], [15, 30, 50, 100, "All"]],
      ),
      "columns"=>array(
        "id"=>array(
          "label" => "id",
          "className" => "hidecol",
          "searchable" => false,
          "orderable" => false
        ),
        "firstname"=>array(
          "label" => "firstname",
        ),
        "lastname"=>array(
          "label" => "lastname",
        ),
        "email"=>array(
          "label" => "email",
        ),
        "company"=>array(
          "label" => "company",
          "className" => "text-center"
        ),
        "mobile"=>array(
          "label" => "mobile",
          "className" => "text-center",
        ),
        "countryCode"=>array(
          "label" => "Code",
          "className" => "text-center",
          "searchable" => false,
          "orderable" => false,
        ),
        "country"=>array(
          "label" => "country",
          "className" => "text-center",
          "searchable" => false,
          "orderable" => false,
        ),
      ),
      "cssClass"=>array(
        "table" => "table table-bordered table-striped table-sm",
        "th" => "cssHeader"
      ),
  ));
?>

Jquery Script $('#userstablelms tr').dblclick(function() {

console.log ($(this));
console.log ( $(this). closest('tr'). children('td:first'). text())
var userid = $.trim($(this). closest('tr'). children('td:first'). text());
console.log(userid)
window.location = baseurl+'/profile/profile.php?action=userprofilelms&id='+userid;

});

The problem is when "serverSide"=>true the table row is not clickable, but when I make "serverSide"=>false, I am able to click the table row and it redirects me to the page I want,

My code required the "serverSide"=>true because I am fetching big amount of data. Could you help me to resolve the issue

Sebastian Morales commented on May 24, 2022

Since data rows are rendered dynamically in server side mode you might want to bind row double click event when they are created with "createdRow" option like this:

DataTables::create(array(
    ...
    "options" => array(
        ...
        "createdRow" =>  "function( row, data, dataIndex ) {
            //Bind row double click event here
        }", // put client js function inside quotes like a string since this is inside PHP code
    )
));

Let us know if it works for you or not. Tks,

Ahmed Bucheeri commented on May 25, 2022

Just I changed my jquery script t0

$(document).on( 'dblclick', '#userstablelms tr', function () {

console.log ($(this));
console.log ( $(this). closest('tr'). children('td:first'). text())
var userid = $.trim($(this). closest('tr'). children('td:first'). text());
console.log(userid)

window.location = baseurl+'/profile/profile.php?action=userprofilelms&id='+userid; });

Now it works

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
solved

None