简体   繁体   English

数据表过滤器不起作用

[英]Datatable filter doesn't work

Why filtering by name column doesn't work?为什么按名称列过滤不起作用? Could somebody help with that?有人可以帮忙吗?

There is an input with id="name" which should filter values by name column of the table.有一个带有id="name"的输入,它应该按表的名称列过滤值。 But it doesn't work.但它不起作用。 What is wrong?怎么了?

HTML: HTML:

        <div>
            <input type="text" id="name" name="name" placeholder="Name">
        </div>    
<table id="shTable" class="table table-striped table-bordered dataTable no-footer dtr-inline" role="grid" aria-describedby="shTable_info">
    <thead class="">
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </thead>
</table>

JavaScript: JavaScript:

<script>
    $.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
            var max = parseInt( $('#max').val(), 10 );
            var name = $('#name').val();

            var age = parseFloat( data[3] ) || 0; // use data for the age column
            var name_table = (data[0]);

            if ( isNaN( max ) || ( age <= max ) || ( isNaN( max ) ) || ( age <= max ) || name_table.indexOf(name) > -1 )
            {
                return true;
            }
            return false;
        }
    );

    $(document).ready(function() {
        var table = $('#shTable').DataTable();

        // Event listener to the two range filtering inputs to redraw on input
        $('#max, #name').keyup( function() {
            table.draw();
        } );
    } );

</script>

It doesnt filter rows by name because you have mixed up the code you have copied directly from the datatables page .它不会按名称过滤行,因为您混淆了直接从数据表 页面复制的代码。

There, another field is also involved.在那里,还涉及另一个领域。 In your case you just have to filter only by name (and not by age as in the datatables page).在您的情况下,您只需按名称过滤(而不是按数据表页面中的年龄过滤)。 So you have to adjust the filtering functionality accordingly...所以你必须相应地调整过滤功能......

$.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
        var name = $('#name').val();


        var name_table = (data[0]);

        if (  name_table.indexOf(name) > -1 )
        {
            return true;
        }
        return false;
    }
);

See this in action here此处查看此操作

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

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