动态更改可搜索的数据表列属性

问题描述 投票:0回答:1

在初始化列的数据表bSearchable属性之后,我必须更改一个函数。我需要先启用然后禁用它。

我找到了一个可见性示例:

$(document).ready(function() {
    var table = $('#example').DataTable( {
        "scrollY": "200px",
        "paging": false
    } );

    $('a.toggle-vis').on( 'click', function (e) {
        e.preventDefault();

        // Get the column API object
        var column = table.column( $(this).attr('data-column') );

        // Toggle the visibility
        column.visible( ! column.visible() );
    } );
} );

在我的情况下也可以使用类似的东西吗?像column.seachable这样的东西?

javascript jquery datatables
1个回答
0
投票

这是一个老问题,但是花了我几个小时来研究答案......主要是基于我缺乏JS技能。

用例是我有一个带有ID列的表,这是一个GUID。这个Id很少使用占用大量空间,因此它是隐藏的,默认情况下不可搜索。但对于可能有权访问日志或数据库的内部用户,我们也需要能够搜索ID列。

我为这些用户添加了一个复选框,它应该更改ID列的可见性和可搜索属性。我的解决方案基于another question regarding searching on columns。但是在这个问题中,搜索只针对一个单独的列而不是所有可搜索的列(正常的DataTable.search() API)。遗憾的是,要改变searchable属性,似乎没有公共API。如果更改searchable属性,则必须使表中的数据无效,否则列数据仍将/不存在于表aoFilterData中。这会强制表重新加载数据源(在本例中为DOM)中的数据。

在正确的地方注册eventhandler我遇到了很多问题。过滤器字段不存在于表的第一个draw事件中,因此我必须在表完全初始化后触发的init事件中注册处理程序。

var userTable;
var idSearchCbx;
var searchField;

$(document).ready(function () {
userTable = document.getElementById("table-users");
if (userTable) {
    userTable = $("#table-users").DataTable(
        {
            "columnDefs": [
                {
                    //First column invisible and not searchable by default
                    "targets": [0], //first column = ID
                    "visible": false,
                    "searchable": false
                }],
          //Other initialization options
    ); //Table creation
    userTable.on("init",
        function () {
            $("input[type='search']").on("change cut input keypress keyup paste search recheck",
                function () {
                    var searchValue = $(this).val();
                    userTable.search(searchValue).draw();
                    //width attribute is calculated and added to the table
                    //it loses 2 pixels everytime the checkbox is checked/
                    //unchecked somehow - so delete it
                    document.getElementById("table-users").removeAttribute("style");
                });

            idSearchCbx = $("#cbx-search-id");
            idSearchCbx.on("change",
                function () {
                    if (this.checked === true) {
                        //this makes the first column searchable, not official API
                        userTable.context[0].aoColumns[0].bSearchable = true;
                        //make it visible as well, official API
                        userTable.columns(0).visible(true);
                        //invalidates cached table data so that the column values are
                        //added to the filterdata
                        userTable.rows().invalidate().draw();
                    } else {
                        userTable.context[0].aoColumns[0].bSearchable = false;
                        userTable.columns(0).visible(false);
                        userTable.rows().invalidate().draw();
                    }
                    //trigger the normal search again so that the
                    //filter is / is not applied to the first column
                    $("input[type='search']").trigger("recheck");
                });
        }); //DT on init
    } //table present
});//document ready

我假设上下文与settings()有关,因此请记住,上下文中的名称可能会发生变化,即使在次要版本中,这很可能也不是版本稳定的。但我没有找到任何其他方式。希望它可以帮助某人。

© www.soinside.com 2019 - 2024. All rights reserved.