JSFiddle - React, Tailwind, and code Playground

by Rikesh Ramlochund

HTML

<script src="http://yadcf-showcase.appspot.com/resources/js/jquery.dataTables.min.js"></script>
<script src="http://yadcf-showcase.appspot.com/resources/js/jquery.dataTables.yadcf.js"></script>
<link rel="stylesheet" href="http://yadcf-showcase.appspot.com/resources/css/jquery.dataTables.css">
<link rel="stylesheet" href="http://yadcf-showcase.appspot.com/resources/css/jquery.dataTables.yadcf.css">
<a href="#" id="create">Create</a> | <a href="#" id="destroy"> Destroy</a> | <a href="#" id="add-row">Add Row</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>

JavaScript

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);
}