Skip to content

addIndexColumn not using database primary keys #2661

@schonhoff

Description

@schonhoff

Summary of problem or feature request

Hello,

I have a problem with the addIndexColumn. In my case I want that the index column reference the id of the database object like it is described in the editor case (https://editor.datatables.net/examples/advanced/jsonId.html). DataTables Editor is using DT_RowId as a standard for setting the id attribute. If you use softdeletes or other deletes the row number isn't matching the id anymore. So in case of using datatables editor it will edit a completely other entry instead the right one.

Code snippet of problem

DataTables::of(Geodaten::with('postal_code'))
            ->addIndexColumn()
            ->toJson();

Example data:
Geodaten table in database:
id name
1 Munich
2 Berlin
3 Bremen
5 Hamburg

This would be result in this json code with addIndexColumn:

{
  data: [
    {DT_RowId: 1, id: 1, name: Munich},
    {DT_RowId: 2, id: 2, name: Berlin},
    {DT_RowId: 3, id: 3, name: Bremen},
    {DT_RowId: 4, id: 5, name: Hamburg},
  ]
}

There is now a mismatch for DT_RowId and id. That can cause plugins like editor to use a false model and update the wrong model. (This was in my case very painful to undo!)

System details

  • Ubunutu/Windows
  • 7.3/7.4/8.0
  • 8.27
  • 9.15.2

Possible solution:

It would be nice, if the addIndexColumn function is not exclusively used like this:

            if ($this->includeIndex) {
                $value[$indexColumn] = ++$this->start;
            }

in src/Processors/DataProcessor.php

Maybe there is a way to set the IndexColumn to the ID of a model. Or maybe another function?

Current workaround:

https://editor.datatables.net/examples/advanced/jsonId.html
Like said in this example of datatables editor I can use another identifier to match the right model.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions