How to exclude hidden columns from being searched.

How to exclude hidden columns from being searched.

MrMattMrMatt Posts: 1Questions: 1Answers: 0

I just downloaded the newest datatables version to fix an issue with column filtering and column re-order.

The option syntax is not the same and i did know how to exclude columns that are hidden from being filtered.

How it works now is that when I hide column[0] and select a value from the dropdownlist in column[1] (which is now in column[0]'s position), the data in the array for DataTable().columns(0) is the data from the hidden column.

below is my initialize options:

var dtopts = {
        "dom": '<"clearfix"><"pull-left"l><"pull-right"Bf>r<"horizontal-scroll"t><"pull-left"i><"pull-right"p><"clearfix">',
        buttons: [
            {
                extend: 'colvis',
                className: 'btn-sm',
                prefixButtons: ['colvisRestore']
            },
            {
                extend: "excel",
                title: "DCN " + reportName + " Report Export",

                exportOptions: {
                    columns: ':visible',
                    modifier: {
                        selected: true,
                        filter: 'applied',
                        order: 'current'
                    }
                }
            },
        ],
        colReorder: true,
        
        "initComplete": function () {
            $(this.api().columns()[0]).each(function (i, item) {
                var column = $("#reportTable").DataTable().columns(item)
                var currentFilter = $("#reportTable").DataTable().columns(i).search()[0]
                var select = $("<select class='drop-down-filter'><option value=''>No Filter</option></select>")
                    .appendTo($(column.footer()).empty())
                    .on("change", function () {
                        var val = $(this).val()

                        var index = $(this).parent("th").index();
                        column = $("#reportTable").DataTable().columns(index);

                        column
                            .search(val ? '^' + val + '$' : '', true, false)
                            .draw();

                        // this is a method that rebuilds the ddl filters to reflect unique column values
                        RebuildDropDownFilters($(this)) 
                    });
                
                //builds the ddl filters to reflect unique column values
                $(_.uniq(column.data()[0])).sort().each(function (d, j) {
                    var value = removeSpanTags(j).trim()

                    if ($(select).find("option[value='" + HtmlEncode(value) + "']").length >= 1) {
                        return;
                    }

                    var opt = $("<option></option>").attr("value", value).text(value)

                    if (currentFilter != "" && currentFilter == value) {
                        opt.attr("selected", "selected")
                    }

                    opt.appendTo(select)
                });
            });
        },
        "stateSave": true,
        "stateLoadCallback": function (settings) {
            return reportJson;
        }
    }

Thanks

Answers

  • allanallan Posts: 63,356Questions: 1Answers: 10,444 Site admin

    Can you link to a test case showing the issue please.

    Thanks,
    Allan

This discussion has been closed.