KoolReport's Forum

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

DataTable sort at Server Side is not working Koolreport 5.9.1 #2539

Open Ahmed Bucheeri opened this topic on on Jan 20, 2022 - 19 comments

Ahmed Bucheeri commented on Jan 20, 2022

When I use Koolreport 5.9.1 DataTable Seever Side, the Order function is not working Below is my code

<?php
    use \koolreport\datagrid\DataTables;
    use \koolreport\widgets\koolphp\Table;
    use \koolreport\processes\CalculatedColumn;
    use \koolreport\inputs\Select;


    if(!isset($_SESSION)){
        session_start();
    }

    $excluded_columns = array();
    if ($_SESSION['UserType'] != 'admin') {
       $excluded_columns= [ array( "targets" => 5 , "visible" => false), array( "targets" => 6 , "visible" => false)];
    }
?>
  <div>
    <h2 class="text-center m-3">Snags List</h2>
    <div class="row m-2">
      <div class="col-md-10">
        <form id="selectionform" name="selectionform" action='' class="col-md-9 p-0" method="post">
          <div class="row">
            <div class="col-sm-6">
              <label class="form-label">Resopurce</label>
              <?php
                Select::create(array(
                  "name"=>"selectResourceId",
                  "dataStore"=>$this->dataStore("resourceList"),
                  "defaultOption"=>array("All Resources" => 0 ),
                  "dataBind"=>array(
                      "text"=>"ResourceName",
                      "value"=>"ResourceId",
                  ),
                  "clientEvents"=>array(
                    "change"=>"function(){
                    //    console.log( assignedDevice )
                        frm = document.getElementById('selectionform') ;
                        frm.action = 'index.php?action=snags&rid='+ selectResourceId.val()+'&statusid='+ selectStatus.val() ;
                        $('#selectionform').submit();
                    }",
                  ),
                  "attributes"=>array(
                    "class" => "form-select",
                  )
                ));
              ?>
            </div>
            <div class="col-sm-3">
              <label class="form-label">Snag Status</label>
              <?php
                Select::create(array(
                  "name"=>"selectStatus",
                  "dataStore"=>$this->dataStore("SnagStatusList"),
                  "defaultOption"=>array("Opend Snags" => 0 ),
                  "dataBind"=>array(
                      "text"=>"Status",
                      "value"=>"Id",
                  ),
                  "clientEvents"=>array(
                    "change"=>"function(){
                    //    console.log( assignedDevice )
                    console.log( 'Changed' )
                        frm = document.getElementById('selectionform') ;
                        frm.action = 'index.php?action=snags&rid='+ selectResourceId.val()+'&statusid='+ selectStatus.val() ;
                        $('#selectionform').submit();
                    }",
                  ),
                  "attributes"=>array(
                    "class" => "form-select",
                  )
                ));
              ?>
            </div>
          </div>
        </form>
      </div>
      <div class="col-md-2 text-end pe-4">
        <button type="button" class= "addsnagbtn btn-primary bi bi-gear-wide-connected m-1" data-bs-toggle="tooltip" title="Create Snag"> Add Snag</button>
      </div>
    </div>
    <?php
        DataTables::create(array(
          "name" => "snagsTable",
          'dataSource' => function() {
            return $this->src('sqlserver')
              ->query($this->params['qry'])
              ->pipe(new CalculatedColumn(array(
                  "OpenDate"=>array(
                    "exp"=>function($data){
                        return formateDate ($data['OpenDate']);
                    }
                  )
              )))
              ->pipe(new CalculatedColumn(array(
                  "WeeksOpen"=>array(
                    "exp"=>function($data){
                        return round ( (strtotime(gmdate('Y-m-d H:i:s', time())) - strtotime($data['OpenDate']))/(60*60)/24/7,1);
                    }
                  )
              )))
              ->pipe(new CalculatedColumn(array(
                  "ResponsibilityName"=>array(
                    "exp"=>function($data){
                        return getUserNameById($data['ResponsibilityName']);
                    }
                  )
              )))
              ->pipe(new CalculatedColumn(array(
                  "MaintCategoryId"=>array(
                    "exp"=>function($data){
                        return getMaintCategoryById($data['MaintCategoryId']);
                    }
                  )
              )))
              ->pipe(new CalculatedColumn(array(
                  "EngineTypeId"=>array(
                    "exp"=>function($data){
                      if ( $data['EngineTypeId'] == 0 && $data['SessionId']) {
                        $sessionData = getEngineTypeBySessionId ($data['SessionId']);
                        return $sessionData ;
                      } else {
                        return getEngineTypeById($data['EngineTypeId']);
                      }
                    }
                  )
              )))
              ->pipe(new CalculatedColumn(array(
                  "editicon"=>array(
                    "exp"=>function($data){
                        return '<div snagid='. $data['Id'] . ' class="editsang btn text-primary p-0" data-bs-toggle="tooltip" title="Edit Snag">
                                  <i class="bi bi-pencil-square"></i>
                                </div>';
                    }
                  )
              )));
          },
          "themeBase" => "bs3",
          "serverSide" => true,
          "method"=>"get",
          "plugins" => ["Buttons"],
          "responsive" => true,
          "options"=>array(
            "dom" => 'Bfrtip',
            "buttons" => [ array("extend" => "excel", "className" => "excelButton", "text" => "Export to Excel"),
                          array("extend" => "pageLength", "className" => "pageButton")
            ],
            "fastRender" => true,
            "searching" => true,
            "ordering" => true,
            "searchOnEnter" => true,
            "paging" => true,
            "mark" => true,
            "order" => array( array(4,"desc") ),
            "lengthMenu" => [[15, 30, 50, 100, -1], [15, 30, 50, 100, "All"]],
            "columnDefs" => $excluded_columns,
          ),
          "columns"=>array(
            "ResourceName"=>array(
              "label" => "Resource",
            ),
            "SnagRef"=>array(
              "label" => "Ref #",
            ),
            "ManufacturerRef"=>array(
              "label" => "Manu. Ref #",
            ),
            "Class"=>array(
              "label" => "Priority",
              "className" => "text-center"
            ),
            "OpenDate"=>array(
              "label" => "Open Date",
              "className" => "text-center"
            ),
            "WeeksOpen"=>array(
              "label" => "Weeks Open",
              "className" => "text-center"
            ),
            "Problem"=>array(
              "label" => "Problem",
            ),
            "Status"=>array(
              "label" => "Status",
              "className" => "text-center",
            ),
            "RasiedBy"=>array(
              "label" => "Rasied By",
              "className" => "text-center",
            ),
            "ResponsibilityName"=>array(
              "label" => "Responsibility",
              "className" => "text-center",
            ),
            "MaintCategoryId"=>array(
              "label" => "Category",
              "className" => "text-center",
            ),
            "EngineTypeId"=>array(
              "label" => "Engine",
              "className" => "text-center",
            ),
            "editicon"=>array(
              "label" => "",
              "className" => "text-center",
              "searchable" => false,
              "orderable" => false,
            ),
          ),
          "cssClass"=>array(
            "table" => "table table-bordered table-striped align-middle small",
            "th" => "cssHeader"
          )
        ));
      ?>
Ahmed Bucheeri commented on Jan 20, 2022

$this->params['qry'] =

        "SELECT Snags.Id
              ,Snags.SnagRef
              ,Snags.ResourceId
              ,Snags.SessionId
              ,Resource.ResourceName
              ,Snags.ManufacturerRef
              ,Snags.PriorityId
              ,SnagPriority.Class
              ,Snags.OpenDate
              ,Snags.OpenDate AS WeeksOpen
              ,Snags.Problem
              ,Snags.StatusId
              ,SnagStatus.Status
              ,Snags.RasiedBy
              ,Snags.ResponsibilityId AS ResponsibilityName
              ,Snags.MaintCategoryId
              ,Snags.EngineTypeId AS EngineTypeId
              ,Snags.SessionId AS editicon
            FROM Snags
            JOIN Resource
              ON Snags.ResourceId = Resource.ResourceId
            JOIN SnagPriority
              ON Snags.PriorityId = SnagPriority.Id
            JOIN SnagStatus
              ON Snags.StatusId = SnagStatus.Id
         WHERE Snags.IsDeleted = 0  ". $whereStatus . $whererecId ;
Ahmed Bucheeri commented on Jan 20, 2022

I am trying to sort the data by column OpenDate, but it did not work

Ahmed Bucheeri commented on Jan 20, 2022

The above works perfectly on Koolreport version 5.0.1

Sebastian Morales commented on Jan 20, 2022

Pls set "method"=>"post" to see if it works. Rgds,

Ahmed Bucheeri commented on Jan 20, 2022

Thanks for reply, I set "method"=>"post. I did not work

Sebastian Morales commented on Jan 20, 2022

How about "fastRender" => false?

Ahmed Bucheeri commented on Jan 20, 2022

It is the same

Sebastian Morales commented on Jan 20, 2022

Could you pls set up an online link where this happens and send us the link (via email to support@koolreport.com if it's private) so that we can investigate it. Our server side DataTables example has no sorting problem with KoolReport Pro 5.9.1. Rgds,

Ahmed Bucheeri commented on Jan 20, 2022

My application is not published , it is inside use only.

Sebastian Morales commented on Jan 20, 2022

Can you check your sqlserver's log whenever you click order on a column to see which sql query is called?

Ahmed Bucheeri commented on Jan 20, 2022

I am not expert on sqlserver, can you advise how to check it

Ahmed Bucheeri commented on Jan 20, 2022

if you want i can provide remote access via Anydesk application

Sebastian Morales commented on Jan 20, 2022

Pls email us to support@koolreport.com your Anydesk Id. Tks,

Ahmed Bucheeri commented on Jan 20, 2022

it is sent

Ahmed Bucheeri commented on Jan 20, 2022

Thanks, It has been resolved

Sebastian Morales commented on Jan 20, 2022

You're welcome! We will fix this issue in the next release of Datagrid and KoolReport Pro. Tks for your valuable feedback!

Ahmed Bucheeri commented on Jan 21, 2022

dear Sorry by mistake i have deleted koolreport 5.9.1 you modified yesterday to fix sorting server side Could you please advise how i can redo the update you made sorry for that

Sebastian Morales commented on Jan 21, 2022

Pls open the file koolreport/datagrid/DataTables.php and replace these lines:

            $orders = Util::get($request, 'order', []);
            $orderSql = "";
            foreach ($orders as $order) {
                $orderable = Util::get($col, 'orderable', true);
                if ($orderable !== "true") continue;
                $colKey = $columns[$order['column']]['data'];
                if (!in_array($colKey, $trueColKeys)) continue;
                $dir = strtolower($order['dir']);
                if ($dir !== "asc"  && $dir !== "desc") continue;
                $orderSql .= $colKey . " " . $dir . ",";
            }
            if (!empty($orderSql)) {
                $orderSql = substr($orderSql, 0, -1);
                $queryParams['order'] = $orderSql;
            }

with these ones:

            $orders = Util::get($request, 'order', []);
            $orderSql = "";
            foreach ($orders as $order) {
                $colNum = $order['column'];
                $colMeta = Util::get($columns, $colNum, []);
                $colKey = Util::get($colMeta, 'data');
                if (!in_array($colKey, $trueColKeys)) continue;
                $orderable = Util::get($colMeta, 'orderable', true);
                if ($orderable !== "true") continue;

                $dir = strtolower($order['dir']);
                if ($dir !== "asc"  && $dir !== "desc") continue;
                $orderSql .= $colKey . " " . $dir . ",";
            }
            if (!empty($orderSql)) {
                $orderSql = substr($orderSql, 0, -1);
                $queryParams['order'] = $orderSql;
            }
Ahmed Bucheeri commented on Jan 21, 2022

Thanks for quick response

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