繁体   English   中英

DataTables单个列搜索

[英]DataTables Individual Column searching

是否可以组合Select InputsText Inputs 我在几个表上使用DataTables,始终使用相同的初始化。 我想通过列标题Select Inputs的列,因为我想要选择列表的列并不总是在同一位置。 然后我想要其他所有Text Inputs 这样,我将有一列具有Select Inputs ,所有其他列具有Text Inputs

我已经能够从这两个例子中实现不同的选择文本输入。 但是我对jquery和javascript不够好,无法弄清楚如何为Select Input选择正确的列,并让其他所有列都是Text Inputs 我使用的表可以是3列到75列之间的任何位置。 我想通过Select输入的标题名称选择列。

除此之外,有没有办法让Select Input成为多选输入? 我有一个州的选择器,并希望能够一次选择多个状态。

以下是我用于SelectText输入的两种不同方式:

initComplete: function ()
{
    this.api().columns([1]).every(function ()
    {
        var column = this;
        var select = $('<select><option value=""></option></select>')
                .appendTo($(column.footer()).empty())
                .on('change', function ()
                {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );
                    var strVal = val.replace("<div class='Scrollable'>", '');
                    strVal = strVal.replace("<\/div>", '');
                    column
                        .search(strVal ? /*'^' +*/ strVal /*+ '$'*/ : '', true, false)
                        .draw();
                });

        column.data().unique().sort().each(function (d, j)
        {
            var strValue = d.replace("<div class='Scrollable'>", '');
            strValue = strValue.replace("<\/div>", '');
            select.append('<option value="' + strValue + '">' + strValue + '</option>')
        });
    });
}
,
initComplete: function ()
{
    var api = this.api();

    // Apply the search
    api.columns().every(function ()
    {
        var that = this;

        $('input', this.footer()).on('keyup change', function ()
        {
            if (that.search() !== this.value)
            {
                that
                      .search(this.value)
                      .draw();
            }
        });
    });
}

EIDT

使用@ Gyrocode的建议,我现在有了这个:

initComplete: function ()
{
    var api = this.api();

    // Apply the search
    api.columns('.dt-filter-text').every(function ()
    {
        var that = this;

        $('input', this.footer()).on('keyup change', function ()
        {
            if (that.search() !== this.value)
            {
                that
                  .search(this.value)
                  .draw();
            }
        });
    });
    api.columns('.dt-filter-select').every(function ()
    {
        var column = this;
        var select = $('<select><option value=""></option></select>')
            .appendTo($(column.footer()).empty())
            .on('change', function ()
            {
                var val = $.fn.dataTable.util.excapeRegex(
                    $(this).val()
                );
                var strVal = val.replace("<div class='Scrollable'>", "");
                strVal = strVal.replace("</div>", '');
                column
                    .search(strVal ? /*'^' +*/strVal /*+ '$'*/ : '', true, false)
                    .draw();
            });
        column.data().unique().sort().each(function (d, J)
        {
            var strValue = d.replace("<div class='Scrollable'>", '');
            strValue = strValue.replace("</div>", '');
            select.append('<option value="' + strValue + '">' + strValue + '</option>')
        });
    });
}

这几乎可行。 当我从State列中的选择器中选择一个值时,它不会搜索。 我确定我错过了一些明显的东西,但我不知道是什么。

此外,它没有获得所有状态,只有第一页上的状态。 有没有办法获得所有的状态,或者有一个数组可以保存我需要的所有值,因为状态不会改变?

我有它的工作。 只需确保将标题添加到标题中,如下所示:

<thead>
    <tr>
        <th class="dt-filter-text">Name</th>
        <th class="dt-filter-select">Position</th>
        <th class="dt-filter-select">Office</th>
        <th class="dt-filter-text">Age</th>
        <th class="dt-filter-text">Start date</th>
        <th class="dt-filter-text">Salary</th>
    </tr>
</thead>
<tfoot>
    <tr>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Age</th>
        <th>Start date</th>
        <th>Salary</th>
    </tr>
</tfoot>

JavaScript的:

$(document).ready(function() {
    // DataTable
    $('#table').DataTable({
        initComplete: function () {

            //Apply select search
            this.api().columns('.dt-filter-select').every(function () {
                var column = this;
                var select = $('<select><option value=""></option></select>')
                    .appendTo($(column.footer()).empty())
                    .on('change', function () {
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );
                        column
                            .search( val ? '^'+val+'$' : '', true, false)
                            .draw();
                    });
                column.data().unique().sort().each(function (d,j) {
                    select.append('<option value="'+d+'">'+d+'</option>')
                });
            });

            //Apply text search
            this.api().columns('.dt-filter-text').every(function () {
                var title = $(this.footer()).text();
                $(this.footer()).html('<input type="text" placeholder="Search '+title+'" />');
                var that = this;
                $('input',this.footer()).on('keyup change', function () {
                    if (that.search() !== this.value) {
                        that
                            .search(this.value)
                            .draw();
                    }
                });
            });
        }
    });
});

如果要将页脚直接放在标题下方,请将其添加到CSS:

#table tfoot {
    display: table-header-group;
}

您需要为columns() API方法指定列索引以定位特定列。

例如:

initComplete: function ()
{
    // Initialize dropdown filter in second column
    this.api().columns([1]).every(function ()
    {
       // ... skipped ...
    });

    // Initialize text filter in first, third and fourth column
    this.api().columns([0, 2, 3]).every(function ()
    {
       // ... skipped ...
    });
}

然而,而不是处理列索引,我会一个类名分配给每个th一个你可以使用文本框(元素dt-filter-text )或下拉( dt-filter-select )。 然后,您可以为columns() API方法提供选择器,例如columns('.dt-filter-text')columns('.dt-filter-select')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM