Programmatically-applied PowerTable Filters

Hey there everyone. :slight_smile:

I have a PowerTable on a Popup dialog. The following code runs on the PowerTable's initialize function:

import com.jidesoft.filter.AbstractFilter

Determine Laptop Name from LaptopID

newData = system.db.runPrepQuery("SELECT [Name] FROM [LaptopAsset] WHERE [id]=?", [self.parent.InputParameterLaptopID])
for row in newData:
sLaptopName = row["Name"]

Apply the filters

oTable = self.table
oTable.setAutoCreateRowSorter(1)

oTableHeader = oTable.getTableHeader()
oTableHeader.clearFilters()
oTableHeader.setShowFilterIcon(1)
oTableHeader.setShowSortArrow(1)
oTableHeader.setAutoFilterEnabled(1)

oActualModel = oTable.getRowSorter().getModel().getActualModel()
oActualModel.clearFilters()

oActualModel.addFilter(0, com.jidesoft.filter.EqualFilter("LaptopName", sLaptopName))
oActualModel.setFiltersApplied(1)

Specify the focus

self.requestFocusInWindow()

The code successfully filters the dataset... and displays the proper tableheader icons (because of setShowFilterIcon and SetShowSort Arrow)... sorting tableheader icon works great, but when I select "All" from the tableheader filter choices, it doesn't "unapply"/remove the programmatically applied filter... I have struggled for a couple of days with the Jtable docs and tried many things, but can't figure out what I'm doing wrong. Any help is appreciated. Thanks in advance!

Frank.

			model = self.getTable().getModel().getActualModel()
			from com.jidesoft.grid import SingleValueFilter
			class newFilter(SingleValueFilter):
				def isValueFiltered(self, value):
					return True if value == target.data.getValueAt(row, 4) else False
			model.addFilter(3, newFilter())
			model.setFiltersApplied(True)
			model.refresh()

I’ve used this code before to successfully apply a filter (shows the filter icon in the table header correctly), and it also allows it to be removed by selecting ‘All’ from the dropdown; I suspect that the EqualFilter you’re using doesn’t do everything necessary to interact with the table.

1 Like

That did it! Weird how we needed to derive our own filter… Thanks ever so much! Cheers! :smiley:

Just a note for future reference, you do need to have "Filterable" enabled to get the icon in the table header. Otherwise it just filters without icon.

But I'm wondering if there would be something similar for grouping. I've found documentation about grouping on Jide tables, but I can't get it to work in python.

https://www.jidesoft.com/products/JIDE_Grids_Developer_Guide.pdf#page=94

1 Like

Can you help to clarify how this works?
I can’t seem to get this run…
And how do we customize the filters?

Here’s an outline. Create one custom method for each filter configuration in the power table you wish to script.

def setFilter(self, col, target):
	model = self.getTable().getModel().getActualModel()
	model.clearFilters()
	from com.jidesoft.grid import SingleValueFilter
	class newFilter(SingleValueFilter):
		def isValueFiltered(self, value):
			return bool( value != target )
	model.addFilter(col, newFilter())
	model.setFiltersApplied(True)
	model.refresh()

For each column you need to tailor the SingleValueFilter return logic based on the column’s data type and values. In order to filter using two columns you need to declare two classes, and so forth. Note the inequality in the isValueFiltered() logic above i.e. (value != target). First I tried as @PGriffith indicated but that yields the logical complement of the desired filtration hence the negation.

Finally script the filters in the other component that serves as the GUI for setting the filters. In my case I have another power table that summarizes the main raw data set. In the summary table’s extension function it goes something like this.

def onMousePress(self, rowIndex, colIndex, colName, value, event):
	root = self.parent
	table = root.getComponent('Power Table Raw Data')
	if		colIndex == 0:		table.setFilter(colIndex,value)

documentation link: http://www.jidesoft.com/javadoc/com/jidesoft/grid/FilterableTableModel.html

3 Likes