Reference the DataTable initialization to a global variable
skiiiks opened this issue · 11 comments
Many of the DataTables advanced usage needs to refer to Datatable instance as the variable used to create the object.
In your "run" method, instead of using this:
$this->getView()->registerJs('jQuery("#' . $id . '").DataTable(' . Json::encode($this->getParams()) . ');');
you should use this:
$this->getView()->registerJs('var ' . $id . ' = jQuery("#' . $id . '").DataTable(' . Json::encode($this->getParams()) . ');');
So you can use the variable $id in Javascript to refer to the Datatable instance.
registerJs wraps code by closure
so you can't access to this var from global space
but you could get instance of Datatable by it id
In some cases I've found that when I refer to $("#id") it gives me this error: https://datatables.net/manual/tech-notes/3
Mainly when I'm in actions, init, and other advanced DataTable events.
But when I use the "global variable" (generated with the change I suggested) I have no problems ;-)
I don't know ... it's a unharmful change :-P
@skiiiks
Actually, we could provide some additional property for the widget.
e.g.globalVariable
What do you think about it?
That would be perfect and cleaner ... I used to use the same variable name as the "id" (make sense), but maybe others could need different naming.
;-)
Wow !!! I was using the Packagist version and it seems is an old one !!! I don't have DataTableColumn and there's a lot of new code for sorting columns ... :-D
if it works as should I'll release new version asap.
As far as I have tested, it's ok ... and I use it hard way , but I'd like to test with the new features I didn't enjoy :-D please, update packagist and I'll do other tests
you could test code from the master branch
you need to use dev-master
version in your composer.json
file
https://getcomposer.org/doc/articles/aliases.md
I have tested this feature with the last code from master. It works.