Search: filter only the field which start by the searched expression

Search: filter only the field which start by the searched expression

duanraduanra Posts: 2Questions: 1Answers: 0

Hi,
By using the global input search box (on top right of the table), I would like to display only the row where the field of the first column start by the searched expression.

For example I have these data in my table (where | is the column separator):
arnaud | manager
sarah | employee
archie | arranger

actually when I enter "ar" in the input search area, all rows are displayed, but only the rows 1 and 3 have "ar" at the beginning of the word. If I search "arn" only the first row should be displayed.

Hope you understand me...

Please could you help me?

Best regards

Answers

  • colincolin Posts: 7,698Questions: 0Answers: 1,297

    You would need to use a regex search() for that, something like search('^arn', true, false) - see examples here.

    This thread shows you how to reuse the existing search box for your own purposes.

    Colin

  • duanraduanra Posts: 2Questions: 1Answers: 0
    edited January 13

    Thanks a lot for this response. I've try it but each request hide all lines, so I've disable option "searching" in dataTable and script my personnal code:

    $(".globalSearch").on( 'keyup', function () {
                var rec = $(this).val();
                $.each($("#example tbody tr"), function()
                {
                    var ch0=$(this).children()[0];
                    var ch1=$(this).children()[1];
    
                    var colCode = $(ch0).html();
                    var colUsuel = $(ch1).html();
    
                    // si on trouve le mot recherché au début
                    if (colCode.substr(0, rec.length) == rec || colUsuel.substr(0, rec.length) == rec)
                    {
                        $(this).show();
                    }else
                    {
                        $(this).hide();
                    }
                });
            } );
    

    Now It's OK :)
    Have a nice day

    Arnaud

Sign In or Register to comment.