Skip to content

Commit b52e1d0

Browse files
recipe: cs, 02
1 parent 727f097 commit b52e1d0

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed
Loading
Loading
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: Filtering Link Fields
3+
description: Using the `set_query` and `get_query` API to filter your link fields.
4+
---
5+
6+
## Use Case
7+
8+
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:
9+
10+
![Screenshot showing filter getting applied](../../../../assets/images/cookbook/client-scripts/filtered_district_field.png)
11+
12+
The district doctype has a field which stores which State it belongs to:
13+
14+
![District DocType Form View](../../../../assets/images/cookbook/client-scripts/district_doctype.png)
15+
16+
## The Script
17+
18+
```js {3-9}
19+
frappe.ui.form.on('Location', {
20+
refresh(frm) {
21+
frm.set_query("district", (doc) => {
22+
return {
23+
filters: {
24+
"state": doc.state // whatever state is selected
25+
}
26+
}
27+
});
28+
}
29+
})
30+
```
31+
32+
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`.
33+
34+
## Learn More
35+
36+
You can read more about `set_query` API [here](https://frappeframework.com/docs/user/en/guides/app-development/overriding-link-query-by-custom-script).

0 commit comments

Comments
 (0)