KoolReport's Forum

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

Need help in adding dynamically query for filtering #2475

Open Anjali opened this topic on on Dec 2, 2021 - 1 comments

Anjali commented on Dec 2, 2021

Hello, i need to add query for datatable where i need to filter by column. and i am using datatable controller and view for all the other datattable as well. SO please guide me how i can add Filter query dynamically that only one datatable get affected. Thanks

<?php

use App\Http\Helpers\Settings;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
use koolreport\datagrid\DataTables;
use koolreport\processes\Filter;

DataTables::create(array(
    "title" => $this->title,
    "name" => $this->name,
    'dataSource' => function () {
        return $this->src('mysql')
            ->query('select * from ' . $this->datasource)
            ->pipe(new \koolreport\processes\Map([
                "{value}" => function ($row) {
                         $btn = '';
                    foreach ($this->actions_btn as $key => $value) {
                        if ($key == 'deletebtn') {
                            $btn .= '<a onclick="deleteFunc(' . $row['id'] . ')" class="btn btn-danger btn-sm m-1 px-2">' . $value['icon'] . '</a>';
                        } else {
                            $btn .= '<a href="' . route($this->route . $value['route'], $row['id']) . '" class="btn btn-info btn-sm m-1 px-2">' . $value['icon'] . '</a>';
                        }
                        $row['Actions'] = $btn;
                    }
              return $row;
                }
            ]));
    },
    "columns" => $this->column,
    "plugins" => $this->plugins,
    "options" => $this->options,
    "serverSide" => true,
));
Sebastian Morales commented on Dec 3, 2021

Here's how you can catch the DataTables' server-side search parameter sent to server each time users search in DataTables:

        //MyReport.php or MyReport.view.php        
        $request = strtolower($method) === 'post' ? $_POST : $_GET;
        $id = isset($request["id"]) ? $request["id"] : null;        
        if ($id == $dataTablesName) { // DataTables::create(array( "name" => $dataTablesName, ...));
            $columnsSearch = isset($request["columns"]) ? $request["columns"] : [];
        ) else $columnsSearch = [];
        echo "columnsSearch = "; print_r($columnsSearch); // print $columnsSearch to see its structure and value

With this $columnsSearch parameter caught you could use KoolReport Filter process to filter only columns (real or fake) you want. Let's us know if there's any issue.

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