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

Filter between dates

Karimttarek opened this issue · comments

Summary of problem

here my code , i need to make filter between receipt_date which is column(0) i tired so hard to find any solution but i didn,t

Code snippet

private function Q(){

    return DB::table('receipts')

        ->select('receipts.uuid','receipts.no as receipt_number','receipts.receipt_date',

            'receipts.statement','invoicehead.internal_id as invoice_id','receipts.receiver_name',

            DB::raw('CASE WHEN receipts.receipt_type IN(1,7,11) THEN value END as debit, CASE WHEN receipts.receipt_type IN (3,8)THEN value END as credit'),
            DB::raw('CONCAT_WS(" ", receipts.supplier_name , receipts.receiver_name, receipts.customer_name)  as receiver'))

        ->leftJoin('invoicehead' ,'invoicehead.uuid' , '=' ,'receipts.uuid')

        ->whereIn('receipt_type' ,[1,3,7,8,11])

        ->whereBetween('invoicehead.invoice_date',[now()->startOfYear() ,now()->endOfYear()])

        ->orderBy('receipts.receipt_date','asc')

        ->orderBy('receipts.id','asc');


 }

/**

 * Build the DataTable class.
 *

 * @param QueryBuilder $query Results from query() method.
 */

public function dataTable($query)

{
    $query = $this->Q();

    return datatables($query)

    ->addColumn('receipts.receipt_date', function ($query) {

        return  Carbon::parse($query->receipt_date)->format('Y/m/d-H:i');

    })
    ->addColumn('receipts.receiver_name', function ($query) {

        return $query->receiver_name;

    })
    ->addColumn('receipts.statement', function ($query) {

        return $query->statement;

    })

    ->addColumn('debit', function ($query) {

        return number_format($query->debit , 2);

    })
    ->addColumn('credit', function ($query) {

        return number_format($query->credit , 2);

    });

}

/**
 * Get the query source of dataTable.
 */
public function query()
{
    return $this->applyScopes($this->Q(now()->startOfYear() ,now()->endOfYear()));
}
/**
 * Optional method if you want to use the html builder.
 */
public function html(): HtmlBuilder
{

    return $this->builder()
                ->setTableId('customerCard-table')
                ->columns($this->getColumns())
                ->minifiedAjax()
                ->dom('Bfrtip')
                ->orderBy(1)
                ->selectStyleSingle()
                ->buttons([
                    Button::make(['extend' =>'excel', 'text' => __('app.EXCEL')]),
                    Button::make(['extend' =>'csv','text' => __('app.CSV')]),
                    // Button::make(['extend' =>'pdf','text' => __('app.PDF')]),
                    Button::make(['extend' =>'print', 'text' => __('app.PRINT')]),
                    Button::make(['extend' =>'reset','text' => __('app.RESET')]),
                    Button::make(['extend' =>'reload','text' => __('app.RELOAD')])
                ])
                ->parameters([
                    'initComplete' => " function () {
                        this.api().columns([1,2]).every(function () {
                            var column = this;
                            var input = document.createElement(\"input\");
                            input.type = 'search';
                            input.setAttribute('class', 'form-control form-control-sm bg-white');
                            input.setAttribute('placeholder', 'Search');
                            $(input).appendTo($(column.footer()).empty())
                            .on('input', function () {
                                column.search($(this).val(), false, false, true).draw();
                            });
                        });
                        this.api().columns([0]).every(function () {
                            var column = this;
                            var input = document.createElement(\"input\");
                            input.type = 'date';
                            input.setAttribute('class', 'form-control form-control-sm bg-white');
                            $(input).appendTo($(column.footer()).empty())
                            .on('change', function () {
                                column.search($(this).val(), false, false, true).draw();
                            });
                        });
                    }",
                    'drawCallback' => " function () {
                        // Remove the formatting to get integer data for summation
                        let intVal = function (i) {
                            return typeof i === 'string'
                                ? i.replace(/[\$,]/g, '') * 1
                                : typeof i === 'number'
                                ? i
                                : 0;
                        };

                        // thirdColTotal
                        thirdColTotal = this.api()
                            .column(3, { page: 'current' }).data()
                            .reduce((a, b) => intVal(a) + intVal(b), 0);

                        // fourthColTotal
                        fourthColTotal = this.api()
                            .column(4, { page: 'current' }).data()
                            .reduce((a, b) => intVal(a) + intVal(b), 0);

                            this.api().column(3).footer().innerHTML =
                            thirdColTotal;

                            this.api().column(4).footer().innerHTML =
                            fourthColTotal;
                    }"

                ]);
}
/**
 * Get the dataTable columns definition.
 */
public function getColumns(): array
{
    return [
        [
            'name' => 'receipt_date',
            'data' => 'receipt_date',
            'title' => __('app.DATE'),
        ],
        [
            'name' => 'statement',
            'data' => 'statement',
            'title' => __('app.STATEMENT'),
        ],
        [
            'name' => 'receiver_name',
            'data' => 'receiver_name',
            'title' => __('app.RECEIVER'),
        ],
        [
            'name' => 'debit',
            'data' => 'debit',
            'title' => __('app.DEBIT'),
        ],
        [
            'name' => 'credit',
            'data' => 'credit',
            'title' => __('app.CREDIT'),
        ],
    ];
}

/**
 * Get the filename for export.
 */
protected function filename(): string
{
    return 'CustomerCard_' . date('YmdHis');
}

System details

  • Laravel Version : 10.10
  • Laravel-Datatables Version : 10.1

i did it instead of addColumn use filterColumn it worked with daterangepicker
here is my solution

  ->filterColumn('receipt_date', function ($query , $date) {

      $date = explode( '-' , $date);

      $sql = "receipt_date >= ? and receipt_date <= ?";

      $query->whereRaw($sql, [date('Y-m-d' , strtotime($date[0])) , date('Y-m-d' , strtotime($date[1]))]);

  })