Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在DataTables JS中搜索数字和文本_Javascript_Datatables - Fatal编程技术网

Javascript 在DataTables JS中搜索数字和文本

Javascript 在DataTables JS中搜索数字和文本,javascript,datatables,Javascript,Datatables,我正在尝试改进基于的DataTables搜索功能。 它使表尾可搜索(作为字符串)。我希望能够通过在搜索输入前加上符号,将字符串过滤转换为数字过滤 我不擅长JS。如果能在这里迅速获胜,我们将不胜感激 <script> $(document).ready(function() { $('#{{ v.table_id }} tfoot th').each( function () {

我正在尝试改进基于的DataTables搜索功能。 它使表尾可搜索(作为字符串)。我希望能够通过在搜索输入前加上
符号,将字符串过滤转换为数字过滤

我不擅长JS。如果能在这里迅速获胜,我们将不胜感激

            <script>
                $(document).ready(function() {
                    $('#{{ v.table_id }} tfoot th').each( function () {
                        var title = $(this).text();
                        $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
                    } );

                    var table = $('#{{ v.table_id }}').DataTable(
                    {
                        dom: 'Blfrtip',
                        buttons: ['copy'],
                        search: {"caseInsensitive": true},
                        "lengthMenu": [ [12, 24, 48, -1], [12, 24, 48, "All"] ]
                    } 
                );
                    table.columns().every( function () {
                        var that = this;

                        $( 'input', this.footer() ).on( 'keyup change', function () {
                            let re_gt = /^>/
                            let re_lt = /^</
                            var gt = re_gt.test(this.value)
                            var lt = re_lt.test(this.value)

/* NEED HELP WITH THIS PART */
                            if ( re_gt.test(this.value) ) {
                                var min = parseInt( this.value.slice(1), 10 )
                                that
                                    .filter( function ( value, index ) { return value < min ? false : true; } )
                                    .draw();
                            }

                            else if ( re_lt.test(this.value) ) {
                                var max = parseInt( this.value.slice(1), 10 )
                            }

/* THIS PART WORKS */
                            else {
                                if ( that.search() !== this.value ) {
                                    that
                                        .search( this.value )
                                        .draw();
                                }
                            }
                        } );
                    } );
                } );
            </script>

$(文档).ready(函数(){
$('{v.table_id}}tfoot th')。每个(函数(){
var title=$(this.text();
$(this.html(“”);
} );
var table=$('#{{v.table_id}}').DataTable(
{
dom:'Blfrtip',
按钮:[“复制”],
搜索:{“不区分大小写”:true},
“长度菜单”:[[12,24,48,-1],[12,24,48,“全部”]]
} 
);
table.columns().every(函数(){
var=这个;
$('input',this.footer()).on('keyup change',函数(){
让re_gt=/^>/

让我们来看一下,这似乎很尴尬,但它确实有效。 如果我能够同时应用两个活动筛选器,那就太酷了。现在,如果对两列执行大于/小于筛选,则切换到第二列会重置第一列的筛选器

            <script>
                $(document).ready(function() {
                    $('#{{ v.table_id }} tfoot th').each( function () {
                        var title = $(this).text();
                        $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
                    } );

                var table = $('#{{ v.table_id }}').DataTable(
                    {
                        dom: 'Blfrtip',
                        buttons: ['copy'],
                        order: [[ {{v.sort_col}} ]],
                        search: {"caseInsensitive": true},
                        "lengthMenu": [ [12, 24, 48, -1], [12, 24, 48, "All"] ]
                    } 
                );
                    table.columns().every( function () {
                        var that = this

                        $( 'input', this.footer() ).on( 'keyup change', function () {
                            var colnumber = that[0][0] /* got that by inspecting Object.entries(that) in dev mode */
                            let gtlt = /^[<>]/
                            if ( gtlt.test(this.value) ) {
                                var sign = this.value.slice(0,1)
                                var min = ( sign === '>' ? parseInt( this.value.slice(1), 10 ) || -Infinity : -Infinity )
                                var max = ( sign === '<' ? parseInt( this.value.slice(1), 10 ) ||  Infinity :  Infinity )
                                /* console.log(sign, min, max) */
                                $.fn.dataTable.ext.search.push(
                                    function(settings, data, dataIndex) {
                                        var values = parseFloat( data[colnumber] ) || 0
                                        /* console.log(min, max, values) */
                                        if (min == null && max == null) {
                                            return true;
                                        }
                                        if (values >= min && values <= max) {
                                            return true;
                                        }
                                        return false;
                                        }
                                    );
                                table.draw()
                                /* reset min/max filters to default, inactive values */
                                min = -Infinity
                                max =  Infinity
                            }

                            else {
                                if ( that.search() !== this.value ) {
                                    that
                                        .search( this.value )
                                        .draw();
                                }
                            }
                        } );
                    } );
                } );
            </script>

$(文档).ready(函数(){
$('{v.table_id}}tfoot th')。每个(函数(){
var title=$(this.text();
$(this.html(“”);
} );
var table=$('#{{v.table_id}}').DataTable(
{
dom:'Blfrtip',
按钮:[“复制”],
顺序:[{v.sort\u col}]],
搜索:{“不区分大小写”:true},
“长度菜单”:[[12,24,48,-1],[12,24,48,“全部”]]
} 
);
table.columns().every(函数(){
var=this
$('input',this.footer()).on('keyup change',函数(){
var colnumber=that[0][0]/*是通过在开发模式下检查Object.entries(that)得到的*/
让gtlt=/^[]/
if(gtlt试验(该值)){
var sign=this.value.slice(0,1)
var min=(sign=='>'?parseInt(this.value.slice(1),10)| |-无穷:-无穷)
变量max=(符号=='