简体   繁体   English

如果 function 在数据表上

[英]If function on datatables

    function sortfuction(selected) {
            var id = selected.value;
                if (id=0) {
                    $('#password-table').DataTable( {
                    "destroy": true,
                    "paging": false,
                    "searching":false,
                    "order": [[ id, "asc" ],]
                } );
                }
                }else{
                    $('#password-table').DataTable( {
                    "destroy": true,
                    "paging": false,
                    "searching":false,
                    "order": [[ id, "dsc" ],]
                }
        </script>
   <table id="password-table" class=" row-border" style="width:100%">
        <thead>
    <select id="selectorID" onchange="sortfuction(this)">
        <option value="1" >Count</option>
        <option value="0">ABC</option>
    </select>

Having this code, I want to be able to sort tables depending on what value is chosen on Select element.有了这段代码,我希望能够根据在 Select 元素上选择的值对表格进行排序。 For instance, if ABC is chosen, I want my first datatable column to be sorted in ascending order, while if chosen Cound, I want my second column to be sorted descending order.例如,如果选择 ABC,我希望我的第一个数据表列按升序排序,而如果选择 Cound,我希望我的第二列按降序排序。 At this point console returns此时控制台返回

Unexpected token 'else'意外的令牌“其他”

You have an extra } before your else statement, just delete it.在 else 语句之前有一个额外的} ,只需将其删除即可。 But you're also missing an ) and another } after your else and another } to close the function.但是您在 else 和另一个}之后还缺少一个)和另一个}来关闭 function。 Also your if statement needs a double equals or triple equals == to test for equalliing a value otherwise it's just an assignment.此外,您的 if 语句需要双等号或三等号==来测试是否等于一个值,否则它只是一个赋值。

Also a quick look at the docs show it should be 'desc' not 'dsc' to sort descending.另外快速浏览一下文档显示它应该是“desc”而不是“dsc”来排序。 I have updated the code我已经更新了代码

so所以

function sortfuction(selected) {
        var id = selected.value;
            if (id==0) {
                $('#password-table').DataTable( {
                "destroy": true,
                "paging": false,
                "searching":false,
                "order": [[ id, "asc" ]]
            } );

            }else{
                $('#password-table').DataTable( {
                "destroy": true,
                "paging": false,
                "searching":false,
                "order": [[ id, "desc" ]]
            })
            }
  }

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

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