# Programmatically-applied PowerTable Filters

**URL:** <https://forum.inductiveautomation.com/t/programmatically-applied-powertable-filters/15495>\
**Category:** Ignition\
**Tags:** sdk, scripting, ignition79\
**Created:** [September 28, 2017, 5:17pm UTC](https://forum.inductiveautomation.com/t/programmatically-applied-powertable-filters/15495 "2017-09-28T17:17:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MindMusic](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@MindMusic](https://forum.inductiveautomation.com/u/MindMusic)\
**Post date:** [September 28, 2017, 5:17pm UTC](https://forum.inductiveautomation.com/t/programmatically-applied-powertable-filters/15495/1 "2017-09-28T17:17:34Z")

</div>

Hey there everyone. 🙂

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.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 28, 2017, 5:35pm UTC](https://forum.inductiveautomation.com/t/programmatically-applied-powertable-filters/15495/2 "2017-09-28T17:35:12Z")

</div>

```auto
			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.

---

<div class="post-metadata">

**Author:** ![MindMusic](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@MindMusic](https://forum.inductiveautomation.com/u/MindMusic)\
**Post date:** [September 28, 2017, 6:03pm UTC](https://forum.inductiveautomation.com/t/programmatically-applied-powertable-filters/15495/3 "2017-09-28T18:03:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [September 29, 2017, 11:01am UTC](https://forum.inductiveautomation.com/t/programmatically-applied-powertable-filters/15495/4 "2017-09-29T11:01:03Z")

</div>

> [@paul-griffith](#):
>
> (shows the filter icon in the table header correctly)

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.  
 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/3ae06538ae9085e62fbe2e3b9cba4f92d360ff94.png)

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](https://www.jidesoft.com/products/JIDE_Grids_Developer_Guide.pdf#page=94)

---

<div class="post-metadata">

**Author:** ![phong.n.pham](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@phong.n.pham](https://forum.inductiveautomation.com/u/phong.n.pham)\
**Post date:** [August 24, 2018, 12:13am UTC](https://forum.inductiveautomation.com/t/programmatically-applied-powertable-filters/15495/6 "2018-08-24T00:13:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jsorlie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jsorlie/32/3645_2.png) [@jsorlie](https://forum.inductiveautomation.com/u/jsorlie)\
**Post date:** [December 7, 2018, 8:42pm UTC](https://forum.inductiveautomation.com/t/programmatically-applied-powertable-filters/15495/7 "2018-12-07T20:42:01Z")

</div>

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

```auto
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 @paul-griffith 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.

```auto
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](http://www.jidesoft.com/javadoc/com/jidesoft/grid/FilterableTableModel.html)
