Unable to use vedmack/yadcf plugin with tigrang/cakephp-datatable

306 views Asked by At

I'm using tigrang/cakephp-datatable in one of my cakePHP 2.6 projects (and DT 1.10.6). So far, I have been able to successfully implement the plugin with help from the plugin's author. The plugin is excellent and I want' to keep it.

Now, I'm trying to use vedmack/YADCF plugin along with my existing data table to achieve (especially) column filtering functionality because I find the examples on official DT to be very messy and limited. I keep getting error when I initialise YADCF:

TypeError: oTable.settings is not a function
var instance = oTable.settings()[0].oInstance,

Is there a way I can get both plugins to work together? Has anyone tried this?

Below is my JS for DT using cakephp-datatable:

$('.dataTable').each(function() {
    var table = $(this);
    var model = table.attr('data-config');
    var settings = dataTableSettings[model];
    settings['dom'] = 'lrtip';
    settings['stateSave'] = true;
    settings['stateSaveCallback'] = function (settings, data) {
                $.ajax( {...});
            };
    settings['stateLoadCallback'] = function (settings) {
            ...
            };
    table.dataTable(settings);
});

I tried something like this and few other tries after the code above to get YADCF to work but I always got the same error:

var table = $('.dataTable');
yadcf.init(table, [
            {column_number: 0, filter_type: "text", filter_reset_button_text: false, filter_delay:500},
            ...,
            {column_number: 4, filter_type: "select", filter_default_label: "Select", filter_reset_button_text: false, filter_delay:500},
            {column_number: 5, filter_type: "range_number", filter_reset_button_text: false, filter_delay:500},
            ...,
        ]);

I have tried this YADCF code without the other plugin and it works flawlessly. So, I need help on modifying the code to implement with other plugin.

I also tried this(because DT was initialized using dataTable() lowercase "d") but didn't work:

$('.dataTable').each(function() {
...
...
table.dataTable(settings)
        .yadcf([
                {column_number: 0, ...},
                ...
            ]);
});

PS: I had asked the question on the first plugin's github also but I hoped the author of yadcf or someone experienced can help me out here.

1

There are 1 answers

12
Daniel On BEST ANSWER

Try the following (removed the loop / this usage)

var table;
var model = table.attr('data-config');
var settings = dataTableSettings[model];
settings['dom'] = 'lrtip';
settings['stateSave'] = true;
settings['stateSaveCallback'] = function (settings, data) {
            // Send an Ajax request to the server with the state object
            $.ajax( {
                "url": "/cake/Users/save_state",
                "data": data,
                "dataType": "json",
                "type": "POST",
                "success": function () {
                    console.log("ca");
                },
            });
        };
settings['stateLoadCallback'] = function (settings) {
        var o;

        // Send an Ajax request to the server to get the data. Note that
        // this is a synchronous request since the data is expected back from the
        // function
            $.ajax( {
                "url": "/cake/Users/get_state",
                "async": false,
                "dataType": "json",
                "success": function (json) {
                o = json;
                },
            });
        return o;
        };
table = $('.dataTable').DataTable(settings);

yadcf.init(table, [
    {column_number: 0, filter_type: "text", filter_reset_button_text: false, filter_delay:500},
    {column_number: 1, filter_type: "text", filter_reset_button_text: false, filter_delay:500},
    {column_number: 2, filter_type: "text", filter_reset_button_text: false, filter_delay:500},
    {column_number: 3, filter_type: "text", filter_reset_button_text: false, filter_delay:500},
    {column_number: 4, filter_type: "select", filter_default_label: "Select", filter_reset_button_text: false, filter_delay:500},
    {column_number: 5, filter_type: "range_number", filter_reset_button_text: false, filter_delay:500},
    {column_number: 6, filter_type: "select", filter_default_label: "Select", filter_reset_button_text: false, filter_delay:500},
]);

Or you can modify your current code into

$('.dataTable').DataTable(settings); --> return $('.dataTable').DataTable(settings);

dtable('.dataTable'); --> var oTable = dtable('.dataTable');