gbrock/laravel-table

assigning column order

Closed this issue · 2 comments

I have a need to assign the order of the columns. I realize I can assign the order of columns pulled from the model, but it doesn't appear possible to control that order when using the addColumn() function. Here is a trivial example:

$rows = $model->getAll();

$table = Table::create($rows, ['name', 'length', 'width']);

$table->addColumn('area', 'Area', function($row) {
    return $row->length * $row->width;
});

How do I put the 'area' column after 'name' and before 'length'?

Assigning order isn't currently supported. You'd need to create the table without automatic columns, then explicitly add each like so (untested code):

$rows = $model->getAll();

$table = Table::create($rows, false);

$table->addColumn('name');
$table->addColumn('area', 'Area', function($row) {
    return $row->length * $row->width;
});
$table->addColumn('length');
$table->addColumn('width');

I originally had something similiar, but I didn't realize I could omit the callback function, so this is a little more bearable. Thanks