Welcome to the final part of this series. In the first part, we talked about connecting our query to a table widget and displaying our data. In the second part, we went further into implementing server-side pagination into this table, and in the third part, we added searching to it.
In this part, we’ll be adding server-side filtering into the application. Let’s get into it!!!
Implementing server-side filtering
Adding server-side filtering to our existing application is rather very simple. However, we have to understand what needs to be done in order to be able to do it.
- We need to provide a way for our users to select specific fields, for this purpose, we’ll be making use of the select widget.
- For the field selected, we need to provide our users with a way of providing a filter value. For this, we’ll be using the input widget
- Lastly, we need to update our query to use both widgets as specified above.
Let’s dive right in!
-
Drag a select widget into your canvas and position it wherever you want, update the data to display the columns in your table like so:
In my case, I only want my users to be able to filter by the Title and Body columns -
Set the onOptionChange property of the select widget and the onTextChanged property of the input widget to re-run the Query_getPosts query.
-
In the Table widget settings, disable the Allow Filtering option to remove the default filtering functionality.
-
Update your query to use the values from our select widget and the input widget like so:
SELECT * FROM posts WHERE {{W_tableFilter.selectedOptionValue ?? 'title'}} ILIKE '{{'%' + `${W_tableFilter.selectedOptionValue ? W_tableFilterInput.text : Table1.searchText}` + '%'}}' LIMIT {{Table1.pageSize}} OFFSET {{(Table1.pageNo - 1) * Table1.pageSize}};
W_tableFilter.selectedOptionValue refers to the currently selected field of our select widget and W_tableFilterInput.text refers to the input of the input widget.
With this update, you should be able to filter by title and body without any issues.
That’s it for server-side filtering using PostgreSQL!
You can find the completed application here - Appsmith