You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this example we show two approaches to sort and filter data:
236
+
1. Using SQL-like operations for database-backed models (simulated)
237
+
2. Using Python operations for in-memory data
238
+
239
+
Both approaches use the same UI components: `rx.select` for sorting and `rx.input` for filtering.
240
+
241
+
### Approach 1: Database Filtering and Sorting
242
+
243
+
For database-backed models, we typically use SQL queries with `select`, `where`, and `order_by`. In this example, we'll simulate this behavior with mock data.
The state variable `_people` is set to be a [backend-only variable]({vars.base_vars.path}). This is done in case the variable is very large in order to reduce network traffic and improve performance.
333
+
### Approach 2: In-Memory Filtering and Sorting
239
334
240
-
For sorting the `rx.select` component is used. The data is sorted based on the attributes of the `Person` class. When a `select` item is selected, as the `on_change` event trigger is hooked up to the `set_sort_value` event handler, the data is sorted based on the state variable `sort_value` attribute selected. (Every base var has a [built-in event handler to set]({events.setters.path}) it's value for convenience, called `set_VARNAME`.)
335
+
For in-memory data, we use Python operations like `sorted()` and list comprehensions.
241
336
242
-
For filtering the `rx.input` component is used. The data is filtered based on the search query entered into the `rx.input` component. When a search query is entered, as the `on_change` event trigger is hooked up to the `set_search_value` event handler, the data is filtered based on if the state variable `search_value`is present in any of the data in that specific `Person`.
337
+
The state variable `_people`is set to be a backend-only variable. This is done in case the variable is very large in order to reduce network traffic and improve performance.
243
338
244
-
`current_people` is an [`rx.var(cache=True)`]({vars.computed_vars.path}). It is a var that is only recomputed when the other state vars it depends on change. This is to ensure that the `People` shown in the table are always up to date whenever they are searched or sorted.
339
+
When a `select` item is selected, the `on_change` event trigger is hooked up to the `set_sort_value` event handler. Every base var has a built-in event handler to set its value for convenience, called `set_VARNAME`.
340
+
341
+
`current_people` is an `rx.var(cache=True)`. It is a var that is only recomputed when the other state vars it depends on change. This ensures that the `People` shown in the table are always up to date whenever they are searched or sorted.
Pagination is an important part of database management, especially when working with large datasets. It helps in enabling efficient data retrieval by breaking down results into manageable loads.
@@ -541,7 +647,7 @@ class DatabaseTableState3(rx.State):
The real power of the `rx.table` comes where you are able to visualise, add and edit data live in your app. Check out these apps and code to see how this is done: app: https://customer-data-app.reflex.run code: https://github.com/reflex-dev/reflex-examples/tree/main/customer_data_app and code: https://github.com/reflex-dev/data-viewer.
@@ -620,7 +725,7 @@ class TableDownloadState(rx.State):
0 commit comments