Set Filters Programatically (from a text input field)

* You have an input field in a text area (colname document property)
* You can pass multiple values (space separated) to the filter from the text area input field.
* You want to take the value from this text field and pass the value to a filter named 'Site Name' 
* The filter in the filter palnel is a ListBoxFilter 













import Spotfire.Dxp.Application.Filters as filters
import Spotfire.Dxp.Application.Filters.ListBoxFilter
from Spotfire.Dxp.Application.Filters import FilterTypeIdentifiers
from Spotfire.Dxp.Data import DataPropertyClass
from System import String
myPanel = Document.ActivePageReference.FilterPanel
myFilter= myPanel.TableGroups[0].GetFilter("Site Name")
lbFilter = myFilter.FilterReference.As[filters.ListBoxFilter]()
lbFilter.IncludeAllValues=False
strVals = Document.Properties["colname"]
if strVals!=String.Empty:
  lbFilter.SetSelection(strVals.split())
else:
  lbFilter.Reset()



6 comments:

sql sean said...

How do i modify this script to use comma(,) as a separator instead of space?

Jose Leviaguirre said...

Hello Sql Sean,

Just pass the spearator you are using, in this case, a coma, as your argument to the split function.

The split() function is converting the colname string to list of items a,b and c. The default separator is a space. If your original colname string is spearated by comas (e.g. "a,b,c"), then do:

...strVals.split(","))

Bharath Abhishek said...

Hello Jose,

Can you provide te code if I have to make use of two list box filters instetad of a text filter and list box filter. I mean depending upon the selections made in the first list box filters, same should be filtered out in the second list box.

Thanks
Bharath

Jose Leviaguirre said...

Hello Bharath,

Check out this post

Hope this helps.

Viz Developer said...

This does not function if a value from the list pf specified values doe not exist in the drop down. Is there a way to resolve this? Appreciate your help.

Jose Leviaguirre said...

Hello VD, you can select unique values from the filter into a list and do an intersection between the strVals from the doc prop and feed your setSelection with it:

# Select unique values from the filter into a list
# dt is a script param to your talbe as well as columnName (string)
cursor = DataValueCursor.Create[str](dt.Columns[columnName])
distinctRows = dt.GetDistinctRows(None,cursor)

c1 = []
distinctRows.Reset()
while distinctRows.MoveNext():c1.append(cursor.CurrentValue)

# Do an intersection between the strVals
# splits "a, b, c" by comma and remove spaces)
c2 = map(str.strip,strVals.split(","))
c3 = c1 & c2

# Feed your setSelection with it
if c3.Count>0:
lbFilter.SetSelection(c3)
else:
lbFilter.Reset()