|
| 1 | + |
| 2 | +--- |
| 3 | +title: Filter select options |
| 4 | +description: Enter text in a select menu to filter the available options. |
| 5 | +--- |
| 6 | + |
| 7 | +## Filter select options |
| 8 | + |
| 9 | +**Enter text in a select menu to filter the available options.** |
| 10 | + |
| 11 | +To filter options in a select menu, you'll need to add some client-side Javascript to perform the filtering whenever the user types a new letter into the text field. Things to note in this example: |
| 12 | + |
| 13 | +- The option filtering is performed in the browser using the `filterFn` method we've defined with the @methods macro. This macro injects Javascript code into the browser |
| 14 | +- Reactive variables tagged with @in @out are mirrored in the browser as Javascript variables. In the browser, they are contained within the `Main_App_varMain_App_ReactiveModel` structure. You can examine this structure in the browser tools in the console |
| 15 | + |
| 16 | +```julia |
| 17 | +module App |
| 18 | +using GenieFramework |
| 19 | + |
| 20 | +@genietools |
| 21 | + |
| 22 | +@app begin |
| 23 | + @out full_listofnames = ["Henry", "HenryJames", "Mark", "Jenny"] |
| 24 | + @out listofnames = ["Henry", "HenryJames", "Mark", "Jenny"] |
| 25 | + @in selected_name = "" |
| 26 | + @in startlist_button = false |
| 27 | + |
| 28 | + @onbutton startlist_button begin |
| 29 | + listofnames = vcat(listofnames, ["Henry", "Mark", "Jenny"]) |
| 30 | + end |
| 31 | +end |
| 32 | + |
| 33 | +@methods begin |
| 34 | + """ |
| 35 | +filterFn: function(val, update, abort) { |
| 36 | + if (val === '') { |
| 37 | + update(() => { |
| 38 | + Main_App_varMain_App_ReactiveModel.listofnames = Main_App_varMain_App_ReactiveModel.full_listofnames |
| 39 | +
|
| 40 | + }) |
| 41 | + return |
| 42 | + } |
| 43 | + update(() => { |
| 44 | + const needle = val.toLowerCase() |
| 45 | + Main_App_varMain_App_ReactiveModel.listofnames = Main_App_varMain_App_ReactiveModel.full_listofnames.filter(v => v.toLowerCase().indexOf(needle) > -1) |
| 46 | + }) |
| 47 | + } |
| 48 | +""" |
| 49 | +end |
| 50 | + |
| 51 | +function ui() |
| 52 | + [ |
| 53 | + column([ |
| 54 | + btn("Start list", @click(:startlist_button), size = "20px"), |
| 55 | + select(:selected_name, options=:listofnames, emitvalue=true, clearable=true, useinput=true, counter = true, fillinput=true, filled = true, label="TraceID", var"@filter"="filterFn") |
| 56 | + ]) |
| 57 | + ] |
| 58 | +end |
| 59 | +@page("/",ui) |
| 60 | +end |
| 61 | +``` |
| 62 | +``` |
0 commit comments