如何禁用选择框中被筛选掉的值?

huangapple go评论55阅读模式
英文:

Datatables column select filtering - how to disable filtered out values in select box?

问题

Sure, here's the translated content without the code:

问题:如何修改JavaScript以动态更改筛选SELECT菜单,以仅显示来自过滤选择的值。例如,选择了东京后,它现在应该限制“职位”为“会计师、集成专家、支持工程师、区域营销”:
如何禁用选择框中被筛选掉的值?

(禁用“无效”选择值也是一种选择,可以使用<option value="..." disabled>...</option>

英文:

Using the datatables.net example Individual column searching (select inputs) with the example javascript to append SELECT menus into the footer:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function () {
    $(&#39;#example&#39;).DataTable({
        initComplete: function () {
            this.api()
                .columns()
                .every(function () {
                    var column = this;
                    var select = $(&#39;&lt;select&gt;&lt;option value=&quot;&quot;&gt;&lt;/option&gt;&lt;/select&gt;&#39;)
                        .appendTo($(column.footer()).empty())
                        .on(&#39;change&#39;, function () {
                            var val = $.fn.dataTable.util.escapeRegex($(this).val());
 
                            column.search(val ? &#39;^&#39; + val + &#39;$&#39; : &#39;&#39;, true, false).draw();
                        });
 
                    column
                        .data()
                        .unique()
                        .sort()
                        .each(function (d, j) {
                            select.append(&#39;&lt;option value=&quot;&#39; + d + &#39;&quot;&gt;&#39; + d + &#39;&lt;/option&gt;&#39;);
                        });
                });
        },
    });
});

<!-- end snippet -->

Question:
how to modify the JavaScript to dynamically change the filter SELECT menus to only show values from the filtered selection. For example, after selecting Tokyo it should now limit "Position" to "Accountant, Integration Specialist, Support Engineer, Regional Marketing":
如何禁用选择框中被筛选掉的值?

(Disabling the "invalid" select values would be an option as well using &lt;option value=&quot;...&quot; disabled&gt;...&lt;/option&gt;)

答案1

得分: 1

I was able to solve this for my use case. See @andrewJames comment's above about possible UI issue and how to get around it.
In my use case it works fine to disable SELECT OPTIONS that would result in 0 records shown. I'm also using a "reset filter" button.

$(document).ready(function () {
$('#example').DataTable({
initComplete: function () {
this.api()
.columns()
.every(function () {
var column = this;
// small change to add ids to the SELECT boxes
var column_idx = column.index()
var 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>');
                    });
            });
    },
});

});

// gets called on first load and every time filter or search changes
footerCallback: function ( row, data, start, end, display ) {

// loop over all columns - hard coded in my example    
for (var col = 1; col < 15; col++) {                    
    column_id = 'column_' + col
    // retrieve the unique column value after a filter has been applied
    column_values = api.column( col, {search:'applied'} ).data().unique()
    // get the select box by id
    var sel = document.getElementById(column_id)
    // on first page load select box isn't rendered yet
    if (sel != null){
        op = sel.getElementsByTagName('option');
        // disable all select options that are not included in the "available" values
        // we start at 0 because we don't want to disable the blank default                        
        for (var i = 1; i < op.length; i++) {
            if (Array.from(column_values).includes(op[i].value)){
                op[i].disabled = false
            } else {
                op[i].disabled = true
            }
        }
    }    
}    

}

英文:

I was able to solve this for my use case. See @andrewJames comment's above about possible UI issue and how to get around it.
In my use case it works fine to disable SELECT OPTIONS that would result in 0 records shown. I'm also using a "reset filter" button

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function () {
    $(&#39;#example&#39;).DataTable({
        initComplete: function () {
            this.api()
                .columns()
                .every(function () {
                    var column = this;
                    // small change to add ids to the SELECT boxes
                    var column_idx = column.index()
                    var select = $(&#39;&lt;select id=&quot;column_&#39; + column_idx + &#39;&quot;&gt;&lt;option value=&quot;&quot;&gt;&lt;/option&gt;&lt;/select&gt;&#39;)
                        .appendTo($(column.footer()).empty())
                        .on(&#39;change&#39;, function () {
                            var val = $.fn.dataTable.util.escapeRegex($(this).val());
 
                            column.search(val ? &#39;^&#39; + val + &#39;$&#39; : &#39;&#39;, true, false).draw();
                        });
 
                    column
                        .data()
                        .unique()
                        .sort()
                        .each(function (d, j) {
                            select.append(&#39;&lt;option value=&quot;&#39; + d + &#39;&quot;&gt;&#39; + d + &#39;&lt;/option&gt;&#39;);
                        });
                });
        },
    });
});

// gets called on first load and every time filter or search changes
footerCallback: function ( row, data, start, end, display ) {

    // loop over all columns - hard coded in my example    
    for (var col = 1; col &lt; 15; col++) {                    
        column_id = &#39;column_&#39; + col
        // retrieve the unique column value after a filter has been applied
        column_values = api.column( col, {search:&#39;applied&#39;} ).data().unique()
        // get the select box by id
        var sel = document.getElementById(column_id)
        // on first page load select box isn&#39;t rendered yet
        if (sel != null){
            op = sel.getElementsByTagName(&#39;option&#39;);
            // disable all select options that are not included in the &quot;available&quot; values
            // we start at 0 because we don&#39;t want to disable the blank default                        
            for (var i = 1; i &lt; op.length; i++) {
                if (Array.from(column_values).includes(op[i].value)){
                    op[i].disabled = false
                } else {
                    op[i].disabled = true
                }
            }
        }    
    }    
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月8日 02:07:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定