fedemotta / yii2-widget-datatables

DataTables widget for Yii2
MIT License
35 stars 28 forks source link

Javascript error when DataTable is empty #10

Closed philbluefox closed 7 years ago

philbluefox commented 8 years ago

A javascript error was displayed in firebug when there is no results found. "TypeError: nTd is undefined
column: i"

ArqinT commented 8 years ago

Looks like I have the same problem. When no result found: "Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined"

adhayward commented 7 years ago

I encountered this error too but I have found a workaround.. The problem appears to be that both the original DataTables JS library and the Yii2 GridView are trying to provide a message to the user to say the table is empty.

To work around it I have created a class that inherits from DataTables and overridden the renderTableBody function with the following

public function renderTableBody() { $models = array_values($this->dataProvider->getModels()); if (count($models) === 0) { return "<tbody>\n</tbody>"; } else { return parent::renderTableBody(); } }

This gets rid of the "No results found." message from GridView and just outputs an empty tbody instead.

I've also passed the GridView->emptyText property to clientOptions ->language->emptyTable as this means that datatables renders the same message that GridView would have done.

fedemotta commented 7 years ago

@adhayward Thanks for the comment. I am busy right now and I am not dedicating to much time for this project. Could you create a pull request? I will merge the change ASAP

adhayward commented 7 years ago

No Problem. https://github.com/fedemotta/yii2-widget-datatables/pull/16