Filter Data"

From Documentation
(Created page with "<!-- how to implement * toggle auto filter * clear * reapply --> Filter is a useful feature, when you want to focus on data.. <source lang='java'> public boolean isAutoFilt...")
 
m (correct highlight (via JWB))
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{ZKSpreadsheetEssentials3PageHeader}}
 +
 +
 
<!--
 
<!--
  
how to implement
+
how to perform following task via API:
 
* toggle auto filter
 
* toggle auto filter
 
* clear
 
* clear
 
* reapply
 
* reapply
 +
* apply criteria
 +
-->
 +
 +
Filter is a useful feature especially when you want to focus on a subset of data. It can filter out of those data you don't want displaying without removing them. Spreadsheet allows you to enable/disable filter and apply/reset criteria via API. You can use both <javadoc directory="zss">org.zkoss.zss.api.Range</javadoc> or <javadoc directory="zss">org.zkoss.zss.api.SheetOperationUtil</javadoc> to achieve these functions, but <code>SheetOperationUtil</code> checks sheet protection for you.
 +
 +
We'll use a simple application to demonstrate filter API. Its screenshot is as below:
 +
 +
[[File:zss-essentials-filter-example.png | center]]
  
-->
+
In this application, through those buttons on the right hand side we can toggle, clear, and reapply filters and filter data by "Type" column.
  
Filter is a useful feature, when you want to focus on data..
+
Here is the source code to implement it:
 +
<source lang='java' highlight='12,13, 18,20,26,28,34,36,43,44'>
 +
public class AutoFilterComposer extends SelectorComposer<Component> {
  
<source lang='java'>
+
@Wire
 +
private Spreadsheet ss;
  
public boolean isAutoFilterEnabled();
+
@Wire
 +
private Combobox typeBox;
 
 
public Range findAutoFilterRange();
+
@Listen("onClick = button[label='Toogle Filter']")
 +
public void toggle() {
 +
Range selection = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
 +
Range filteringRange = selection.findAutoFilterRange();
 +
if(!selection.isAutoFilterEnabled() &&  filteringRange == null) {
 +
Messagebox.show("nothing to filter");
 +
return;
 +
}
 +
 +
selection.enableAutoFilter(!selection.isAutoFilterEnabled());
 +
 +
// SheetOperationUtil.toggleAutoFilter(selection);
 +
}
 
 
 +
@Listen("onClick = button[label='Clear Filter']")
 +
public void clear() {
 +
Range sheetRange = Ranges.range(ss.getSelectedSheet());
 +
sheetRange.resetAutoFilter();
 +
 +
// SheetOperationUtil.resetAutoFilter(selection);
 +
}
 
 
public void enableAutoFilter(boolean enable);
+
@Listen("onClick = button[label='Reapply Filter']")
 +
public void reapply() {
 +
Range sheetRange = Ranges.range(ss.getSelectedSheet());
 +
sheetRange.applyAutoFilter();
 +
 +
// SheetOperationUtil.applyAutoFilter(selection);
 +
}
 
 
 
+
@Listen("onClick = button[label='Apply']")
public void applyAutoFilter();
+
public void apply() {
+
Range sheetRange = Ranges.range(ss.getSelectedSheet());
public void resetAutoFilter();
+
if (sheetRange.isAutoFilterEnabled()){
 
+
String[] criteria = {typeBox.getValue()};
 +
sheetRange.enableAutoFilter(1, AutoFilterOperation.VALUES, criteria, null,
 +
true);
 +
}
 +
}
 +
}
 
</source>
 
</source>
'''applyAutoFilter doesn't work'''
+
* Line 12: The method <code>findAutoFilterRange()</code> will return a non-blank area based on your selection if Spreadsheet can find it. We should use it to ensure not to enable filter if there is no data.
 
+
* Line 13: Use <code>isAutoFilterEnabled()</code> to get enabled status of the auto filter.
[[File:zss-essentials-filter.png | center]]
+
* Line 18: Enable auto filter with <code>enableAutoFilter(true)</code> or disable it with <code>enableAutoFilter(false)</code>.
 +
* LIne 20: You can also enable / disable auto filter with <code> SheetOperationUtil.toggleAutoFilter()</code>.
 +
* Line 26, 28: The method <code>resetAutoFilter()</code> clears applied criteria and all data will be shown and <code>SheetOperationUtil.resetAutoFilter()</code> has the same function.
 +
* Line 34,36: The method <code>applyAutoFilter()</code> will re-apply current criteria to modified or newly added data.
 +
* Line 43: The argument for criteria is a String array, and you can have multiple values in it, e.g. <code>{"Beverages", "Meat", "Tools"}</code>
 +
* Line 44: Apply criteria on "Type" column which is the first column, so the first argument is <code>1</code>. The second argument is the filter operation the filter performs, and we currently only support filtering values. The fourth argument determines drop-down arrow's visibility which is usually <code>true</code>.
  
<source lang='java'>
 
SheetOperationUtil.toggleAutoFilter(range);
 
  
SheetOperationUtil.resetAutoFilter(range);
 
  
SheetOperationUtil.applyAutoFilter(range);
 
</source>
 
  
 
+
{{ZKSpreadsheetEssentialsPageFooter}}
Filter command calls it.
 
<source>
 
public void enableAutoFilter(int field, AutoFilterOperation filterOp, Object criteria1, Object criteria2, Boolean visibleDropDown);
 
 
 
</source>
 

Latest revision as of 12:52, 19 January 2022



Filter is a useful feature especially when you want to focus on a subset of data. It can filter out of those data you don't want displaying without removing them. Spreadsheet allows you to enable/disable filter and apply/reset criteria via API. You can use both Range or SheetOperationUtil to achieve these functions, but SheetOperationUtil checks sheet protection for you.

We'll use a simple application to demonstrate filter API. Its screenshot is as below:

Zss-essentials-filter-example.png

In this application, through those buttons on the right hand side we can toggle, clear, and reapply filters and filter data by "Type" column.

Here is the source code to implement it:

public class AutoFilterComposer extends SelectorComposer<Component> {

	@Wire
	private Spreadsheet ss;

	@Wire
	private Combobox typeBox;
	
	@Listen("onClick = button[label='Toogle Filter']")
	public void toggle() {
		Range selection = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
		Range filteringRange = selection.findAutoFilterRange();
		if(!selection.isAutoFilterEnabled() &&  filteringRange == null) { 
			Messagebox.show("nothing to filter");
			return;
		}
		
		selection.enableAutoFilter(!selection.isAutoFilterEnabled());
		
//		SheetOperationUtil.toggleAutoFilter(selection);
	}
	
	@Listen("onClick = button[label='Clear Filter']")
	public void clear() {
		Range sheetRange = Ranges.range(ss.getSelectedSheet());
		sheetRange.resetAutoFilter();
		
//		SheetOperationUtil.resetAutoFilter(selection);
	}
	
	@Listen("onClick = button[label='Reapply Filter']")
	public void reapply() {
		Range sheetRange = Ranges.range(ss.getSelectedSheet());
		sheetRange.applyAutoFilter();
		
//		SheetOperationUtil.applyAutoFilter(selection);
	}
	
	@Listen("onClick = button[label='Apply']")
	public void apply() {
		Range sheetRange = Ranges.range(ss.getSelectedSheet());
		if (sheetRange.isAutoFilterEnabled()){
			String[] criteria = {typeBox.getValue()};
			sheetRange.enableAutoFilter(1, AutoFilterOperation.VALUES, criteria, null,
					true);
		}
	}
}
  • Line 12: The method findAutoFilterRange() will return a non-blank area based on your selection if Spreadsheet can find it. We should use it to ensure not to enable filter if there is no data.
  • Line 13: Use isAutoFilterEnabled() to get enabled status of the auto filter.
  • Line 18: Enable auto filter with enableAutoFilter(true) or disable it with enableAutoFilter(false).
  • LIne 20: You can also enable / disable auto filter with SheetOperationUtil.toggleAutoFilter().
  • Line 26, 28: The method resetAutoFilter() clears applied criteria and all data will be shown and SheetOperationUtil.resetAutoFilter() has the same function.
  • Line 34,36: The method applyAutoFilter() will re-apply current criteria to modified or newly added data.
  • Line 43: The argument for criteria is a String array, and you can have multiple values in it, e.g. {"Beverages", "Meat", "Tools"}
  • Line 44: Apply criteria on "Type" column which is the first column, so the first argument is 1. The second argument is the filter operation the filter performs, and we currently only support filtering values. The fourth argument determines drop-down arrow's visibility which is usually true.



All source code listed in this book is at Github.


Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.