umbrella icon indicating copy to clipboard operation
umbrella copied to clipboard

Adding default custom sorting

Open jeroenmimpen opened this issue 3 years ago • 0 comments

For one of my lists I would like custom sorting (preferable on a field that is not part of the table, but is part of the entity).

I tried the following:

$table->handleRequest($request);
$data = [
    'draw'     => $table->getState()->getDraw(),
    'start'    => $table->getState()->getStart(),
    'length'   => $table->getState()->getLength(),
    'orderBy'  => $table->getState()->getOrderBy(),
    'formData' => $table->getState()->getFormData(),
    'callback' => $table->getState()->isCallback(),
];

$data['orderBy'] = [['order_by'=>['name'], 'direction'=>'asc']];
$table->getState()->updateFromArray($data);

But could not get it to work this way. The updateFromArray function resets some data and some things get lost in the process.

I am able to make this work by adjusting the query builder given to the entityAdapter in the DataTableBuilder.

$builder->useEntityAdapter([
      // ... pass data, pass function, in this function call:
      // $qb->addOrderBy('e.created_at', 'asc');
]);

But I would like to apply this sorting only if there is no sorting present yet. Any sorting applied by the CMS user should take priority. But all CMS-applied-sorting is ignored now; clicking on a column has no discernible effect.

What would be a good or the proper way to accomplish this?

jeroenmimpen avatar Dec 15 '22 14:12 jeroenmimpen