-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
727f097
commit b52e1d0
Showing
3 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
title: Filtering Link Fields | ||
description: Using the `set_query` and `get_query` API to filter your link fields. | ||
--- | ||
|
||
## Use Case | ||
|
||
In India, there are states and each state has several districts. Suppose, we want to implement a **Location** form for India which asks the user to select a **State** and then a **District**. But we have to make sure the District field only shows districts belonging to the selected state: | ||
|
||
![Screenshot showing filter getting applied](../../../../assets/images/cookbook/client-scripts/filtered_district_field.png) | ||
|
||
The district doctype has a field which stores which State it belongs to: | ||
|
||
![District DocType Form View](../../../../assets/images/cookbook/client-scripts/district_doctype.png) | ||
|
||
## The Script | ||
|
||
```js {3-9} | ||
frappe.ui.form.on('Location', { | ||
refresh(frm) { | ||
frm.set_query("district", (doc) => { | ||
return { | ||
filters: { | ||
"state": doc.state // whatever state is selected | ||
} | ||
} | ||
}); | ||
} | ||
}) | ||
``` | ||
|
||
The JS API that let's you apply filters to link fields is highlighted above. As you can see above, `doc` (in this case, the **Location** document) is provided for you. Here, `doc` is equivalent to `frm.doc`. | ||
|
||
## Learn More | ||
|
||
You can read more about `set_query` API [here](https://frappeframework.com/docs/user/en/guides/app-development/overriding-link-query-by-custom-script). |