tigrang / cakephp-datatable

JQuery DataTable plugin server-side processing component for CakePHP
47 stars 29 forks source link

How to add class attrubite to datatable? #31

Closed ducnguyenvn closed 11 years ago

ducnguyenvn commented 11 years ago

Hi,

I'm using your plugin for showing a list products. I'm using helper to render table. For now, I want to add class attribute to tags in table. So I can bind click event when user click on tags. Can you tell me how I can do this?

One more question, how can I add my callback functions after a row/table is drawed?

Thanks so much.

tigrang commented 11 years ago

You should use a custom init script than the one provided. Here's an example of mine used to add tooltip hovers to links.

$('.dataTable').each(function () {
        var table = $(this);
        var model = table.attr('data-model');
        var settings = model ? dataTableSettings[model] : {};
        settings.sDom = "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>";
        settings.sPaginationType = "bootstrap";
        settings.aoColumnDefs = [
            { "sClass": "actions", "aTargets": [ -1 ] } // adds "actions" class to the last column
        ];
        settings.fnDrawCallback = function (oSettings) { // called when the table is drawn, page changed
            initTooltip(this);
        };
        table.addClass('table table-bordered table-striped').dataTable(settings);
    });

The js var dataTableSettings is generated by the plugin for the columns. We grab that and extend it to whatever we need.

In your controller, change your helper definition to:

public $helpers = [
    'DataTable.DataTable' => ['scriptBlock' => false],
];
ducnguyenvn commented 11 years ago

Yes, Thanks.

I also want to set different colors for table rows that depend on value of any field in data model. Could I?

Thanks again.

tigrang commented 11 years ago

You can through callbacks in the js lib. I don't provide support for that. Go to the datatables page and look through the documentation.

tigrang commented 11 years ago

Specifically http://www.datatables.net/examples/advanced_init/row_callback.html