yii2-datatables icon indicating copy to clipboard operation
yii2-datatables copied to clipboard

extraColumns and serverSide does not get along well

Open adantart opened this issue 4 years ago • 2 comments

Hi! Amazing package ;-) I've tried to activate the serverSide but the datatables action ajax call gives me an error:

Unknown column 'columnNameSpecial' in 'order clause' The SQL being executed was: SELECT * FROM client ORDER BY columnNameSpecial LIMIT 10"

being "columnNameSpecial" one of the "extraColumns" I have declared in the widget.

When you use extraColumns to put enhanced information, you need to put the name of the model function in the "data" attribute of each column. I mean, for example:

            'columns' => [
                    [
                        'data' => 'columnNameSpecial',
                        'title' => 'Name',                    
                        'render' => new JsExpression('function render(data, type, row, meta ){
                            return row.columnNameSpecial;
                        }'),
                    ],
                    ....

having in the model for example:

function getColumnNameSpecial() {
   return $this->name . " " . $this->lastname;
}

But in the other hand, the applyOrder and applyFilter of 'datatables' array in actions() needs the $column["data"] to be a REAL attribute of the model, because it will construct with that name, the applyFilters and orderBy of the ActiveRecord. That's the reason I have the "sql error" of the beginning.

Any ideas ? ;-)

adantart avatar Nov 28 '19 23:11 adantart

Ok, I found some things ...

I have realized I have to add the extraColumns ALSO in the "datatables" action in the Controller. That's ok ... but I don't understand the example you put here: https://github.com/NullRefExcep/yii2-datatables#extra-columns

  1. you put in the widget configuration the extraColumn array (customPrice)
  2. you put in the "datatables" action the same extraColumn array (customPrice)
  3. but you add in the extraColumn attribute of the column configuration a "customField" (why ?)
  4. also you don't put "data" attribute in the column configuration, and that would crash the applyFilters and orderBy methods

How is the model ? Does it have getCustomPrice() and getCustomField() methods ?

adantart avatar Nov 29 '19 00:11 adantart

Hey @adantart My example is general, you don't need to put extraColumns to widget and column config at the same time. If we are using render we can skip the data attribute. We have data argument in a render function that has all the properties of a particular row. Extra columns -- it's a way to define some additional data properties, that are not defined in the database or don't mention at any data property. If you don't have a real column, you need to use an extra column instead of using data.

ZAYEC77 avatar Nov 29 '19 11:11 ZAYEC77