yajra / laravel-datatables

jQuery DataTables API for Laravel

Home Page:https://yajrabox.com/docs/laravel-datatables

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SearchPanel serverside does not display any data

markplus opened this issue · comments

I display the tiles correctly, everything seems to work but there are no values in the tiles

<?php

namespace App\DataTables;

use App\Models\Ticket;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\EloquentDataTable;
use Yajra\DataTables\Facades\DataTables;
use Yajra\DataTables\Html\Builder as HtmlBuilder;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Html\Editor\Editor;
use Yajra\DataTables\Html\Editor\Fields;
use Yajra\DataTables\Html\SearchPane;
use Yajra\DataTables\Services\DataTable;
use Yajra\DataTables\Html\ColumnDefinition;

class TicketsDataTable extends DataTable
{
    /**
     * Build the DataTable class.
     *
     * @param QueryBuilder $query Results from query() method.
     */
    public function dataTable(QueryBuilder $query): EloquentDataTable
    {

        return (new EloquentDataTable($query))->setRowId('id');

    }

    /**
     * Get the query source of dataTable.
     */
    public function query(Ticket $model): QueryBuilder
    {
        return $model->newQuery();
    }

    /**
     * Optional method if you want to use the html builder.
     */
    public function html(): HtmlBuilder
    {
        return $this->builder()
                    ->setTableId('tickets-table')
                    ->columns($this->getColumns())
                    ->addColumnDef([
                        'searchPanes' => ['show' => true],
                        'targets' => ['_all'],
                    ])
                    ->minifiedAjax()
                    ->orderBy(1)
                    ->selectStyleSingle()
                    ->serverSide(true)
                    ->processing(true)
                    ->stateSave(true)
                    ->select(true)
                    ->fixedHeader(true)
                    ->deferRender(true)
                    ->orderCellsTop(true)
                    ->lengthChange(true)
                    ->autoWidth(false)
                    ->responsive(true)
                    ->searchPanes(true)
                    ->editors([
                        Editor::make()
                              ->fields([
                                Fields\Text::make('id'),
                                Fields\Text::make('tipo'),
                                Fields\Text::make('stato'),
                                Fields\Text::make('segnalazione'),
                                Fields\Text::make('priorita'),
                                Fields\Text::make('chiusura'),
                                Fields\Text::make('categoria'),
                              ]),
                    ])
                    ->buttons([
                        Button::make('create')->editor('editor'),
                        Button::make('edit')->editor('editor'),
                        Button::make('remove')->editor('editor'),
                        Button::make('excel'),
                        Button::make('csv'),
                        Button::make('pdf'),
                        Button::make('print'),
                        Button::make('reset'),
                        Button::make('reload')
                    ])
                    ->parameters([
                        'dom' => '<PBfrt<t>lip>',
                        'language' => ['url' => url('vendors/it-IT.json')],
                    ]);
    }

    /**
     * Get the dataTable columns definition.
     */
    public function getColumns(): array
    {
        return [
            Column::make('id'),
            Column::make('tipo'),
            Column::make('stato'),
            Column::make('segnalazione'),
            Column::make('priorita'),
            Column::make('chiusura'),
            Column::make('categoria'),
        ];
    }

    /**
     * Get the filename for export.
     */
    protected function filename(): string
    {
        return 'Tickets_' . date('YmdHis');
    }
}
  • PHP v8.2.5
  • Laravel v10.17.0

You need to add the data you want to display in the SearchPanes to the datatable definition, you can also find info about that in the wiki:

/**
 * Build the DataTable class.
 *
 * @param QueryBuilder $query Results from query() method.
 */
public function dataTable(QueryBuilder $query): EloquentDataTable
{

    return (new EloquentDataTable($query))->setRowId('id')
        ->searchPane(
            'tipo',
             [
                 [
                      'value' => 1, // This is the value given when selected
                      'label' => 'display value',
                      'total' => 5, // optional
                      'count' => 3 // optional
                 ],
                 [
                      'value' => 2, // This is the value given when selected
                      'label' => 'display value 2',
                      'total' => 6, // optional
                      'count' => 5 // optional
                 ],
             ],
            function (\Illuminate\Database\Eloquent\Builder $query, array $values) {
                // Here you need to specify what to filter when a given value is given
                return $query
                    ->whereIn(
                        'tipo',
                        $values);
            }
        );

}

You need to add the data you want to display in the SearchPanes to the datatable definition, you can also find info about that in the wiki:

/**
 * Build the DataTable class.
 *
 * @param QueryBuilder $query Results from query() method.
 */
public function dataTable(QueryBuilder $query): EloquentDataTable
{

    return (new EloquentDataTable($query))->setRowId('id')
        ->searchPane(
            'tipo',
             [
                 [
                      'value' => 1, // This is the value given when selected
                      'label' => 'display value',
                      'total' => 5, // optional
                      'count' => 3 // optional
                 ],
                 [
                      'value' => 2, // This is the value given when selected
                      'label' => 'display value 2',
                      'total' => 6, // optional
                      'count' => 5 // optional
                 ],
             ],
            function (\Illuminate\Database\Eloquent\Builder $query, array $values) {
                // Here you need to specify what to filter when a given value is given
                return $query
                    ->whereIn(
                        'tipo',
                        $values);
            }
        );

}

thanks for the reply, but I didn't understand what I have to insert if I take the code you wrote, I see the display value written correctly, but to see what is in the database what should I insert? what kind of code i have to put to see what is in the server? I still haven't figured out how it works, isn't it automatic?

I can't figure out what I have to do, can someone help me get the panel working? I'm in total confusion as to what I should do

if it hangs and does not load without giving any error what can be done? (123,762 items)

Even if I entered the count parameter, nothing is counted, what should I change?
2e9ffab8b9e20c92d3038b92cacf6837.png

    public function dataTable(QueryBuilder $query): EloquentDataTable
    {

        return (new EloquentDataTable($query))
        ->setRowId('stato')
        ->searchPane(
            'id',
            fn() => Ticket::query()->select('id as value', 'id as label')->get(),
            function (\Illuminate\Database\Eloquent\Builder $query, array $values) {
                return $query
                ->whereIn('id', $values);
            },
            'stato',
            fn() => Ticket::query()->select('stato as value', 'stato as label')->get(),
            function (\Illuminate\Database\Eloquent\Builder $query, array $values) {
                return $query
                ->whereIn('stato', $values);
            }
        );
    }

1c437642079b4cdb2619d99b9ee80ffe.png

now I can see the values but they are not grouped with the total, it stays at 0 and they are all separate, I'm trying but I still can't do it, has anyone managed and can help me out?
fd637cec83a1abf3f7aef5bd07f287e1.png