yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.74k stars 863 forks source link

use pjax and service implement datatable #1486

Open arbabi2010 opened 6 years ago

arbabi2010 commented 6 years ago

Hi , I'm using service implement datatable with pjax page . when page refresh datatable show me correctly but when page change by pjax datatable doesn't work . how can i use pjax and service implement . my datatable is like this

{!! $dataTable->scripts() !!}
yajra commented 6 years ago

Hmm, I think you need to overwrite the render method and exclude the pjax request.

    public function render($view, $data = [], $mergeData = [])
    {
        if ($this->request()->ajax() && $this->request()->wantsJson() && ! request()->pjax()) {
            return app()->call([$this, 'ajax']);
        }

        parent::render($view, $data = [], $mergeData = []);
    }

Just replace __PJAX__ with the logic on how you determine pjax request.

On the other hand, maybe we can exclude pjax by default. If ever you make it work, can you submit a PR? May not be able to check this atm. Thanks!

--EDIT-- I just found out that it's just request()->pjax() to determine pjax request.

arbabi2010 commented 6 years ago

I change my render like this

    public function render($view, $data = [], $mergeData = [])
    {
        if ($this->request()->ajax() && $this->request()->wantsJson() && request()->pjax()) {
            return app()->call([$this, 'ajax']);
        }

        if ($action = $this->request()->get('action') AND in_array($action, $this->actions)) {
            if ($action == 'print') {
                return app()->call([$this, 'printPreview']);
            }

            return app()->call([$this, $action]);
        }

        return view($view, $data, $mergeData)->with($this->dataTableVariable, $this->getHtmlBuilder());
    }

but no render table in view .

arbabi2010 commented 6 years ago

I think , i need to Reinitializing datatable in pjax option but i don't now how cant i Reinitializing datatable , if i use datatable with jquery i can Reinitializing that like below but i don't khow how Reinitializing that with

{!! $dataTable->scripts() !!}

but data table jquery like below

    $(document).on('ready pjax:end', function(event) {
        $('#reportTable').dataTable();
    });