Column filter

Column filter

rosario.martonerosario.martone Posts: 6Questions: 0Answers: 0
edited 9:10AM in Free community support

Description of problem:
Hi everyone!
I am trying to find a way to customise the search into one of the column in my grid.
Values for this column can be dynamic: for example 100,101,102,1000.
I would like to limit the result to a very specific customisation. In the above values for example, I would like to search for 100,102 and only having as result rows where either 100 or 102 are present (not 1000 or 10201 for example).
Is that possible?
I am trying using the following syntax:

this.filter = (value) => {
    this.tableRef.column(`${this.columnName}:name`).search(value).draw();

    let searchPattern = value
        .split(',')
        .map(item => $.fn.dataTable.util.escapeRegex(item.trim()))
        .join('|');

    this.tableRef.column(`${this.columnName}:name`).search(searchPattern, true, false).draw();
}

but the result is coming with values containing for example 10050, 100321.

Thanks for help!
Ross

Replies

  • allanallan Posts: 65,887Questions: 1Answers: 10,960 Site admin

    Yes, you could build up a more complex regex expression that matches a number at the start, in the middle (with , on either side), or at the end, but I think I'd be tempted to just use a function to do it:

    this.filter = (value) => {
        let searchParts = value.split(',');
    
        this.tableRef
            .column(`${this.columnName}:name`)
            .search((data) => {
                let dataParts = data.split(',');
    
                for (let i = 0; i < searchParts.length; i++) {
                    if (dataParts.includes(searchParts[i])) {
                        return true;
                    }
                }
    
                return false;
            })
            .draw();
    };
    

    I'm sure it is possible to code-golf that to be smaller, but hopefully in this form it is clear what is going on.

    I'll confess I've not tested the code, so it might have a daft error in it somewhere, but again, hopefully it is clear what is happening - but let me know how you get on with it.

    Allan

  • rosario.martonerosario.martone Posts: 6Questions: 0Answers: 0
    edited 9:33AM

    Hi Allan!
    I had to change a bit the code but it perfectly worked, thanks for help!
    Leaving here for anyone who might have the same request:

    this.filter = (value) => {
        if (value != "") {
            let searchParts = value.split(',');
    
            this.tableRef
                .column(`${this.columnName}:name`)
                .search((value) => {
                    let dataParts = value.split(',');
    
                    for (let i = 0; i < searchParts.length; i++) {
                        if (dataParts.includes(searchParts[i])) {
                            return true;
                        }
                    }
    
                    return false;
                })
                .draw();
        }
    };
    

    Ross

  • allanallan Posts: 65,887Questions: 1Answers: 10,960 Site admin

    Awesome - thanks for confirming. Good to hear that did the job for you.

    Allan

Sign In or Register to comment.