Search code examples
pythonwxpythonfilteringobjectlistviewobjectlistview-python

python ObjectListView negate filter


I need some help with negate this filter for the ObjectListView.

def addFilter(self, text):
    # OLV.Filter.Predicate()
    meter_flt = OLV.Filter.TextSearch(self, text=text)
    self.SetFilter(meter_flt)

This works great, but if i try to filter like "chicken" then it's only show chickens. I want it to be reversed, so if i type chicken, everything apart from chicken should be displayed.

Thanks for your help!


Solution

  • You can use Filter.Predicate

    Filter.Predicate(booleanCallable) Show only the model objects for which the given callable returns true. The callable must accept a single parameter, which is the model object to be considered.

    Following is a code snippet for handling multiple text to be excluded from the list of items.

    def __init__(self):
        self.text_list = [] # list of text to be excluded
        self.SetFilter(Filter.Predicate(self.filterMethod))
    
    def addFilter(self, text):
        self.text_list.append(text)
        self.RepopulateList() # so that our filter_method is applied again
    
    def filterMethod(self,obj):
        for text in self.text_list:
            if {YOUR EXCLUSION LOGIC HERE}:
                return False
        return True