Search Column Condition

Search Column Condition

cybercyclonecybercyclone Posts: 2Questions: 1Answers: 0

Is there a way to do a search but for each column decided on whether to include it or transform it?

E.g, I want to be able to do a search across columns, but if it's for a certain column, only do the search if the search value is a specific string length.

Thanks!

Answers

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,736

    I don't understand what you are describing but you can use column().search() to search a specific column. You can use columns().search() to perform an AND search across all the columns specified using column-selector. If neither of these work for your case you can create a search plugin to perform the search the way you want.

    If you want help with this please provide a simple test case with an example set of data and a detailed description of what you are trying to achieve.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • cybercyclonecybercyclone Posts: 2Questions: 1Answers: 0

    Thanks @kthorngren .

    What I'm trying to do specifically is for each column, do a check first before assuming that the search is relevant. E.g, in the below column handler, I've added a "search" function that does what I'm imagining it would if it were implemented. In the scenario I was after, I don't want the search to apply to that column because I only want it handled if the search value is a specific length.

    {
        data: "transactionHash",
        search: function(value){
            if(value.length == 12){
                return value;
            } else {
                return null;
            }
        },
        render: function(data, type, row, meta) {
            if(data){
                if(type === "sort" || type === "filter"){
                    return data;
                }
                if(data.length > 64){
                    return data.substr(0, 64) + "...";
                } else {
                    return data;
                }
            } else {
                return "";
            }
        }
    }
    

    Using the columns().search() seems very hacky for what I'm trying to do and would be hard to keep track of if many tables were used.

    Would the above be possible to create as a plugin and if so, where would I start as it doesn't directly look like a "search plugin" would be the place.

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,736

    Here is an example showing both options:
    http://live.datatables.net/lewodihu/1/edit

    I set the length to 6. For the Office search use London for an exact match in the search plugin. Of course you can change the match to be a partial match or whatever you need. For the search plugin you can use the technique in the thread to control the table that is searched.

    Does this help?

    Kevin

Sign In or Register to comment.