yajra / laravel-datatables

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

How to solve "Exception Message:\n\nUndefined index: column" #2658

Closed Naghal closed 1 year ago

Naghal commented 3 years ago

Summary of problem or feature request

Following an update to Laravel 8, I have updated the package and followed the update guide of this package. However, some datatable in my application fail with the error Exception Message:\n\nUndefined index: column, but only when changing page with a column ordered. (The inital render works fine, and I can succesfully reorder by any column if I stay on page 1)

Code snippet of problem

Using the following datatable definition:

import React from 'react';
import Datatable, {
    createColumn,
    createCustomColumn,
    buttons,
} from 'components/Datatable';
import { userHasAdminPermission } from '../../../../Helpers/permissions';

const DATAGRID_URL = 'admin/customers/table';

const getColumns = (user) => {
    const canManage = userHasAdminPermission(user, 'Gérer les clients');
    return ([
        createColumn('name'),
        createCustomColumn('CustomerNumber', 'customer_number'),
        ... canManage ? [createColumn('manage', {
            render: (_value, { id }) => buttons.edit(`/admin/customers/${id}`, ''),
            orderable: false, searchable: false
        })] : [],
    ]);
};

const propsAreEqual = (prev, next) => prev.textFilter === next.textFilter && prev.user === next.user;

const UserTable = React.memo(({ textFilter, user }) => {
    const getRequestParameters = () => ({});
    return (
        <Datatable
            url={DATAGRID_URL}
            columns={getColumns(user)}
            requestParameters={getRequestParameters()}
            searchInput={textFilter}
            hideHeader
            orderColumnIndex={0}
        />
    );
}, propsAreEqual);

export default UserTable;

The following headers are sent to the server:

columns[0][searchable]: true
columns[0][orderable]: true
columns[0][search][value]: 
columns[0][search][regex]: false
columns[0][name]: name
columns[0][data]: name
columns[1][searchable]: true
columns[1][orderable]: true
columns[1][search][value]: 
columns[1][search][regex]: false
columns[1][name]: CustomerNumber
columns[1][data]: customer_number
columns[2][searchable]: false
columns[2][orderable]: false
columns[2][search][value]: 
columns[2][search][regex]: false
columns[2][name]: manage
columns[2][data]: manage
start: 10
length: 10
search[value]: 
search[regex]: false
order[0][column]: 0
order[0][dir]: desc
draw: 2

and the response is

image

Controller code:

use Yajra\DataTables\DataTables;
[...]
public function customers(Request $request)
    {
        $query = Customer::actives();
        return DataTables::of($query)->make(true);
    }

EDIT Changing the controller code to this "solves" the issue.

public function customers(Request $request)
    {
        $query = Customer::actives();
        return DataTables::of($query)->order(function($query) {})
        ->make(true);
    }

However, using the orderColumn api, it results in the same error. There seem to have a bug with the order and orderColumn api?

public function customers(Request $request)
    {
        $query = Customer::actives();
        return DataTables::of($query)->orderColumn('customer_number', 'CustomerNumber $1')
        ->make(true);
    }

System details

yajra commented 3 years ago

Need help on this one as I haven't used reactjs and can't replicate the issue. However, I am currently playing with VueJS and all seems to work fine along with the html builder and via manually jquery scripts.

The params sent to server looks correct but it seems like the request data is not being read by server properly.

order[0][column]: 0
order[0][dir]: desc
xPlux commented 3 years ago

I had exactly the same issue. The problem was not associated with reactjs, but with the MSSQL database. The exception was in Illuminate\Database\Query\Grammars\SqlServerGrammar: 220 I solved the problem by bumping Laravel version.

Related Laravel PR: https://github.com/laravel/framework/pull/37859

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 7 days since being marked as stale.