我正在使用DataTablesYADCF过滤表。

在某个时候,我需要从表中临时解除绑定(bind)这两个插件,然后再次绑定(bind)它们。如果不使用YADCF,则可以销毁数据表并再次对其进行初始化。但是,当我使用YADCF时,表的过滤器部分没有被破坏。

HTML:

<a href="#" id="create">Create</a> | <a href="#" id="destroy"> Destroy</a>

<table id="mytable" class="results table table-striped">
    <thead>
        <tr>
            <th>Head 1</th>
            <th>Head 2</th>
            <th>Head 3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>12</td>
            <td>13</td>
            <td>14</td>
        </tr>
        <tr>
            <td>12</td>
            <td>13</td>
            <td>14</td>
        </tr>
        <tr>
            <td>152</td>
            <td>13</td>
            <td>154</td>
        </tr>
        <tr>
            <td>1762</td>
            <td>1873</td>
            <td>1874</td>
        </tr>
        <tr>
            <td>124</td>
            <td>1343</td>
            <td>1124</td>
        </tr>
    </tbody>
</table>

不带YADCF的JS JSFIDDLE :
var oTable = $('table');

$('#create').click(function (e) {
    e.preventDefault();
    oTable.dataTable({
        "bJQueryUI": true,
            "bStateSave": true,
            "bPaginate": false,
            "bAutoWidth": false,
    });

});

$('#destroy').click(function (e) {
    e.preventDefault();
    oTable.fnDestroy();
    oTable.attr('class', '');
});

JS与YADCF JSFIDDLE :
var oTable = $('table');

$('#create').click(function (e) {
    e.preventDefault();
    oTable.dataTable({
        "bJQueryUI": true,
            "bStateSave": true,
            "bPaginate": false,
            "bAutoWidth": false,
    });


    // Add YADCF
    oTable.yadcf([{
      column_number: 1,
      filter_type: 'range_number',
      ignore_char: 'm'
    }, {
      column_number: 2,
      filter_type: 'text',
      filter_default_label: ' '
    },
    ]);


});

$('#destroy').click(function (e) {
    e.preventDefault();
    oTable.fnDestroy();
    oTable.attr('class', '');
});

有人可以建议如何销毁YADCF过滤器吗?

最佳答案

因此,这实际上是一个错误。

Issue Submitted | Workaround JSFiddle

JS:

var oTable = $('table');
var first = true;

$('#create').click(function (e) {

    e.preventDefault();
    oTable.dataTable({
        "bJQueryUI": true,
            "bStateSave": true,
            "bPaginate": false,
            "bAutoWidth": false
    });

    if (first) {
        first = false;
        // Add YADCF
        oTable.yadcf([{
            column_number: 1,
            filter_type: 'range_number',
            ignore_char: 'm'
        }, {
            column_number: 2,
            filter_type: 'text',
            filter_default_label: ' '
        } ]);
    } else {
        oTable.find('thead').find('[id^=yadcf-filter-wrapper-table]').each(function (i, v) {
            var cloned = $(this).clone(true);
            console.log( $(this) );
            $(this).closest('th').append( cloned );
            $(this).remove();
            oTable.find('.DataTables_sort_wrapper').css('display', 'inline-block');
        });
        oTable.find('[id^=yadcf]').show();
    }

});

$('#destroy').click(function (e) {
    e.preventDefault();
    oTable.fnDestroy();
    oTable.attr('class', '');
    oTable.off();
    oTable.find('[id^=yadcf]').hide();
    oTable = $('table');
});

$('#add-row').click(function (e) {
    e.preventDefault();
    $('table').append('<tr><td>' + getRandom() + '</td><td>' + getRandom() + '</td><td>' + getRandom() + '</td></tr>');
});

function getRandom() {
    return parseInt(10000 * Math.random(), 10);
}

09-25 18:34