|
1 | 1 | # Making Dynamic Dataview Queries via Meta Bind |
2 | | -A practical example showing how to dynamically populate MetaBind input fields using Dataview queries. This addresses the common use case of wanting to filter suggester options based on note properties, in ways that isn't possible with normal optionQuery. Will require adjustment based on your particular use case, but it's a place to start. |
3 | 2 |
|
4 | | -The code block here is the equivalent of making the query `WHERE {property} = "{value}"` |
| 3 | +A practical example showing how to dynamically populate MetaBind input fields using Dataview queries. This addresses the common use case of wanting to filter suggester options based on note properties in ways that aren't possible with normal optionQuery. This approach lets you filter by any frontmatter property, not just tags, to create more targeted suggestion lists. |
| 4 | + |
| 5 | +Use Case: You're editing a character note and want to populate a "friends" field with only other character notes, excluding non-character content like locations, objects, etc. |
5 | 6 |
|
6 | 7 | ```js-engine |
7 | 8 | const dv = engine.getPlugin('dataview').api; |
8 | 9 |
|
9 | | -const property = "categories"; // frontmatter property to filter on |
10 | | -const value = "character"; // value to match |
11 | | -// Equivalent to the Dataview query WHERE categories = "character" |
| 10 | +const filterProperty = "categories"; // frontmatter property to filter on |
| 11 | +const filterValue = "character"; // value to match - only character notes |
| 12 | +const fieldName = "friends"; // the actual field we're populating |
12 | 13 |
|
13 | | -// Query notes where the property contains the value |
| 14 | +// Query: WHERE categories contains "character" |
| 15 | +// This finds all notes matching the filter criteria. |
14 | 16 | const pages = dv.pages() |
15 | | - .where(p => p[property] && p[property].includes(value)); |
| 17 | + .where(p => p[filterProperty] && p[filterProperty].includes(filterValue)); |
16 | 18 |
|
17 | 19 | // Turn into Meta Bind options |
18 | 20 | const options = pages.map(p => `option(${p.file.name})`).join(", "); |
19 | 21 |
|
20 | 22 | // Build the Meta Bind input string |
21 | | -const inputField = `\`INPUT[listSuggester(${options}):${value}]\``; |
| 23 | +const inputField = `\`INPUT[listSuggester(${options}):${fieldName}]\``; |
22 | 24 |
|
23 | 25 | return engine.markdown.create(inputField); |
24 | 26 | ``` |
0 commit comments