简体   繁体   中英

How to do search with first letter in individual column searching datatable

I'am working on a web app with datatable. I've done global research bar and individual research bar, like this example: https://datatables.net/examples/api/multi_filter.html Now what I want to do is to start research by the first letter only in the individuals columns. I tried to do this whith global research and it worked fine, thank to this :

$(document).ready(function() {
   var table=$('#example').DataTable( {
       responsive: true
   } );
    $('.dataTables_filter input').on( 'keyup', function (e) {
      var regExSearch = '\\b' + this.value;
    table.search(regExSearch, true, false).draw();
   });
} );

But this is not what I want And I have programmed my individual research like that :

table.columns(':gt(1)').every( function () {
        var that = this;

        $( 'input', this.footer() ).on( 'keyup change clear', function () {
            if ( that.search() !== this.value ) {
                that
                    .search(this.value )
                    .draw();
            }
        } );
    } );

I tried to use the same logic as global research while adding .search('\\\\b' +this.value ) but when I try to search something, nothing is returned.

Maybe I do something wrong, does anyone know how to do that ?

Ok I was so close. The only thing I had to do is :

table.columns(':gt(1)').every( function () {
        var that = this;

        $( 'input', this.footer() ).on( 'keyup change clear', function () {
            if ( that.search() !== this.value ) {
                that
                    .search('\^'+this.value, true, false ) // regex=true, smart=false
                    .draw();
            }
        } );
    } );

And it work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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