SearchPanes - Custom Filtering Options (Partial String Filter)

SearchPanes - Custom Filtering Options (Partial String Filter)

zmdaviszmdavis Posts: 9Questions: 0Answers: 0

I'm trying to create a create a custom filter where I can filter by a partial string. To borrow the example here:
https://datatables.net/extensions/searchpanes/examples/customFiltering/customOptionConditions

Some row data that I have says, "Edinburgh". While other row data that I have says, "Edinburgh and Glasgow".

The following code will only filter data that say "Edinburgh" rather than also including "Edinburgh and Glasgow" as a partial string filter.

Is there a way I can adapt the below code to succeed in filtering both 'Edinburgh' and 'Edinburgh and Glasgow ' (without hard-coding it)?

            {
                searchPanes: {
                    options:[
                        {
                            label: 'Not Edinburgh',
                            value: function(rowData, rowIdx){
                                return rowData[3] !== 'Edinburgh';
                            }
                        },
                        {
                            label: 'Not London',
                            value: function(rowData, rowIdx){
                                return rowData[3] !== 'London';
                            }
                        }
                    ],
                    combiner: 'and'
                },
                targets: [3]
            }

Replies

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    See if the Srting includes() method will work for what you want.

    Kevin

  • zmdaviszmdavis Posts: 9Questions: 0Answers: 0

    I have it working here:
    http://live.datatables.net/nipucawa/1/edit

    However, my main issue is that on my end with my work, instead of:
    return rowData[3].includes('Edinburgh');

    I have:
    return rowData["Membrane_Attachment_Type"].includes('Edinburgh');

    SO I wasn't able to get it to work on my end using the data variable pulled from my server_processing.php file unless I used the name. I tried using the numeric method with my column number, but it doesn't work:
    return rowData[7].includes('Edinburgh');

    I thought maybe it was due to the top portion:

            "searchPanes": {
                "viewTotal": true,
                "columns": [7],
            },
    

    But that didn't work for me either... see anything wrong here?

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    Without knowing your data structure its hard to say specifically. Sounds like your data structure is object based so you would use object notation, something like rowData["Membrane_Attachment_Type"]. I used columns.data to define object for your example and it works with using return rowData.office.includes('Edinburgh');:
    http://live.datatables.net/poqafecu/1/edit

    You can use the browser's debugger or console.log statements to see what rowData is within the function. If you still need help then please provide a link to your page or a test case showing the problem you are having.

    Kevin

  • zmdaviszmdavis Posts: 9Questions: 0Answers: 0

    When I use this first code snippet, it pulls the data correctly:

            {
                "searchPanes":{
                "options":[
                {
                    "label": 'Water-Based Adhesive',
                    "value": function(rowData, rowIdx){
                        return rowData["Membrane_Attachment_Type"] == "WB II";
                    }
                }
                ],
                },
                "targets": [7]
            },
    

    This works fine pulling data and anything that starts with "WB II", but won't use WB II as a keyword search, so for instance, "WB II, SBIV, etc.." won't be included in the filter.

    When I try this:

            {
                "searchPanes":{
                "options":[
                {
                    "label": 'Water-Based Adhesive',
                    "value": function(rowData, rowIdx){
                        return rowData.Membrane_Attachment_Type.includes('WB II');
                    }
                }
                ],
                },
                "targets": [7]
            },
    

    and check the console, it gives me:
    "Uncaught TypeError: rowData.Membrane_Attachment_Type is null"
    and points to this line:

    return rowData.Membrane_Attachment_Type.includes('WB II');
    

    Even though it's not really null, so I'm not sure how to solve this one.

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    Uncaught TypeError: rowData.Membrane_Attachment_Type is null

    That suggests you have a value of null in that column. Is this the case? If so then you will need an additional check so you don't use includes() with null values or change your null values to empty strings "".

    Kevin

  • zmdaviszmdavis Posts: 9Questions: 0Answers: 0

    You're right, that was the issue. Works great now! You've been a huge help, thank you!

            {
                "searchPanes":{
                "options":[
                {
                    "label": 'Water-Based Adhesive',
                    "value": function(rowData, rowIdx){
                        if (rowData["Membrane_Attachment_Type"] != null) {
                            return rowData.Membrane_Attachment_Type.includes('WB II');
                        }
                        else{
                            return '';
                        }
                    }
                }
                ],
                },
                "targets": [7]
            },
    
This discussion has been closed.