|
| 1 | +<!-- search-meta |
| 2 | +tags: [filters, parameters, runtime-filters, runtime-parameters, host-events, useHostEventsV2, subscribed-event, timing, UpdateFilters, UpdateRuntimeFilters, UpdateParameters, React, TypeScript] |
| 3 | +apis: [runtimeFilters, runtimeParameters, HostEvent, UpdateRuntimeFilters, UpdateFilters, UpdateParameters, RuntimeFilterOp, RuntimeParameter, subscribedEvent, useHostEventsV2, LiveboardEmbed, SearchEmbed, useEmbedRef, trigger, on] |
| 4 | +questions: |
| 5 | + - How do I apply filters programmatically on an embedded Liveboard or Search? |
| 6 | + - How do I update filters on a ThoughtSpot embed after it has loaded? |
| 7 | + - How do I pass runtime parameters when embedding a Liveboard? |
| 8 | + - How do I update runtime parameters on an embedded Liveboard from my app? |
| 9 | + - What payload does HostEvent.UpdateParameters expect? |
| 10 | + - Should I use runtimeFilters or HostEvent.UpdateFilters to set filters? |
| 11 | + - What is the difference between UpdateRuntimeFilters and UpdateFilters? |
| 12 | + - Why is HostEvent.UpdateFilters not firing or not working in my embed? |
| 13 | + - When is it safe to trigger a host event after the embed loads? |
| 14 | + - How do I use subscribedEvent to know a host event is ready? |
| 15 | + - Do I need useHostEventsV2 for the Subscribed embed event to fire? |
| 16 | + - How do I update runtime filters from a button in my app? |
| 17 | +--> |
| 18 | + |
| 19 | +# update-filters-and-params |
| 20 | + |
| 21 | +Working patterns and a decision guide for applying **filters and parameters** to embedded ThoughtSpot content programmatically — at init via `runtimeFilters` / `runtimeParameters`, and after load via the `UpdateRuntimeFilters`, `UpdateFilters`, and `UpdateParameters` host events, including the `subscribedEvent` readiness pattern (requires `useHostEventsV2: true`). |
| 22 | + |
| 23 | +## Which mechanism to use |
| 24 | + |
| 25 | +| Your situation | Use | Since | |
| 26 | +|---|---|---| |
| 27 | +| Filters/parameters are known when you render the embed | **`runtimeFilters`** / **`runtimeParameters`** view-config options — simplest, no events, no timing concerns | SDK 1.9.0 | |
| 28 | +| Filters change after load (button, external UI) | **`HostEvent.UpdateRuntimeFilters`** — array of `{ columnName, operator, values }` | SDK 1.9.0 / TS 8.1.0.cl | |
| 29 | +| Updating values of a filter that already exists on the Liveboard | **`HostEvent.UpdateFilters`** — `{ filters: [{ column, oper, values }] }` | SDK 1.23.0 / TS 9.4.0.cl | |
| 30 | +| Parameter values change after load | **`HostEvent.UpdateParameters`** — array of `{ name, value, isVisibleToUser? }` | SDK 1.29.0 / TS 10.1.0.cl | |
| 31 | +| You need the exact moment a host event is safe to trigger | **`embed.subscribedEvent(HostEvent.X)`** listener — **only fires when `useHostEventsV2: true` is set in the embed view config** | SDK 1.45.2+ / TS 26.3.0.cl+ | |
| 32 | + |
| 33 | +Good to know: |
| 34 | + |
| 35 | +- **`UpdateRuntimeFilters` resets the embedded object to its original state** before applying the new conditions — user changes such as drill-downs are cleared. |
| 36 | +- If multiple columns share a name, disambiguate with the `WORKSHEET_NAME::COLUMN_NAME` format in `UpdateFilters` (e.g. `"(Sample) Retail - Apparel::city"`). |
| 37 | +- **Common pitfall:** wiring the `Subscribed` readiness listener **without** enabling `useHostEventsV2` — the listener silently never fires. On clusters older than 26.3.0.cl, trigger from `EmbedEvent.Load`/`EmbedEvent.Data` instead, guarded so it runs once. |
| 38 | + |
| 39 | +## Key Usage |
| 40 | + |
| 41 | +```typescript |
| 42 | +import { |
| 43 | + AuthType, |
| 44 | + HostEvent, |
| 45 | + RuntimeFilterOp, |
| 46 | + init, |
| 47 | + type EmbedEvent, |
| 48 | +} from "@thoughtspot/visual-embed-sdk"; |
| 49 | +import { LiveboardEmbed, useEmbedRef } from "@thoughtspot/visual-embed-sdk/react"; |
| 50 | +import { useEffect } from "react"; |
| 51 | + |
| 52 | +// init() must run BEFORE any embed component mounts — call it at module scope. |
| 53 | +// (The useInit hook runs in a parent effect, which fires AFTER child effects, |
| 54 | +// so the embed can construct before init and fail with "Please provide a valid URL".) |
| 55 | +init({ |
| 56 | + thoughtSpotHost: "https://your-instance.thoughtspot.cloud", |
| 57 | + authType: AuthType.None, |
| 58 | +}); |
| 59 | + |
| 60 | +const App = () => { |
| 61 | + const embedRef = useEmbedRef<typeof LiveboardEmbed>(); |
| 62 | + |
| 63 | + // AFTER LOAD — update runtime filters from your app's UI. |
| 64 | + // Note: resets the object to its original state (drills are cleared) before applying. |
| 65 | + const updateRuntimeFilters = () => { |
| 66 | + embedRef.current?.trigger(HostEvent.UpdateRuntimeFilters, [ |
| 67 | + { columnName: "state", operator: RuntimeFilterOp.EQ, values: ["michigan"] }, |
| 68 | + ]); |
| 69 | + }; |
| 70 | + |
| 71 | + // AFTER LOAD — update values of filters that already exist on the Liveboard. |
| 72 | + // Disambiguate duplicate column names with "WORKSHEET_NAME::COLUMN_NAME". |
| 73 | + const updateExistingFilters = () => { |
| 74 | + embedRef.current?.trigger(HostEvent.UpdateFilters, { |
| 75 | + filters: [{ column: "item type", oper: "IN", values: ["shoes", "boots"] }], |
| 76 | + }); |
| 77 | + }; |
| 78 | + |
| 79 | + // AFTER LOAD — update Parameter values (SDK 1.29.0+ / TS 10.1.0.cl+). |
| 80 | + const updateParameters = () => { |
| 81 | + embedRef.current?.trigger(HostEvent.UpdateParameters, [ |
| 82 | + { name: "Discount", value: 0.25, isVisibleToUser: true }, |
| 83 | + ]); |
| 84 | + }; |
| 85 | + |
| 86 | + // READINESS — fires the moment the embedded app has registered its handler for |
| 87 | + // the host event, so triggering is guaranteed safe. |
| 88 | + // REQUIRES useHostEventsV2: true on the embed (see props below) — without the |
| 89 | + // flag this "<Event> Subscribed" signal NEVER fires. |
| 90 | + useEffect(() => { |
| 91 | + const embed = embedRef.current; |
| 92 | + if (!embed) return; |
| 93 | + const subscribed = embed.subscribedEvent(HostEvent.UpdateFilters) as EmbedEvent; |
| 94 | + const onReady = () => { |
| 95 | + embed.trigger(HostEvent.UpdateFilters, { |
| 96 | + filters: [{ column: "date", oper: "EQ", values: ["JULY", "2023"], type: "MONTH_YEAR" }], |
| 97 | + }); |
| 98 | + }; |
| 99 | + embed.on(subscribed, onReady); |
| 100 | + return () => { |
| 101 | + embed.off(subscribed, onReady); |
| 102 | + }; |
| 103 | + }, []); |
| 104 | + |
| 105 | + return ( |
| 106 | + <> |
| 107 | + <button onClick={updateRuntimeFilters}>Update runtime filters</button> |
| 108 | + <button onClick={updateExistingFilters}>Update existing filters</button> |
| 109 | + <button onClick={updateParameters}>Update parameters</button> |
| 110 | + <LiveboardEmbed |
| 111 | + ref={embedRef} |
| 112 | + liveboardId="your-liveboard-id" |
| 113 | + // AT INIT — filters/parameters known upfront: pass them in the view config. |
| 114 | + // Prefer this whenever possible; no events or timing involved. |
| 115 | + runtimeFilters={[ |
| 116 | + { columnName: "state", operator: RuntimeFilterOp.EQ, values: ["california"] }, |
| 117 | + ]} |
| 118 | + runtimeParameters={[{ name: "Discount", value: 0.1 }]} |
| 119 | + // Required for the subscribedEvent readiness pattern (SDK 1.45.2+ / TS 26.3.0.cl+). |
| 120 | + useHostEventsV2 |
| 121 | + /> |
| 122 | + </> |
| 123 | + ); |
| 124 | +}; |
| 125 | +``` |
| 126 | + |
| 127 | +## Documentation |
| 128 | + |
| 129 | +- [Runtime filters](https://developers.thoughtspot.com/docs/runtime-filters) |
| 130 | +- [Runtime parameters](https://developers.thoughtspot.com/docs/runtime-parameters) |
| 131 | +- [Filter types and application layers](https://developers.thoughtspot.com/docs/filters-overview) |
| 132 | +- [Using host events](https://developers.thoughtspot.com/docs/host-events) |
| 133 | +- [HostEvent reference](https://developers.thoughtspot.com/docs/Enumeration_HostEvent) |
| 134 | + |
| 135 | +## Demo |
| 136 | + |
| 137 | +Open in [StackBlitz](https://stackblitz.com/github/thoughtspot/developer-examples/tree/main/visual-embed/update-filters-and-params) |
| 138 | + |
| 139 | +## Run locally |
| 140 | + |
| 141 | +``` |
| 142 | +$ git clone https://github.com/thoughtspot/developer-examples |
| 143 | +$ cd visual-embed/update-filters-and-params |
| 144 | +``` |
| 145 | +``` |
| 146 | +$ npm i |
| 147 | +``` |
| 148 | +``` |
| 149 | +$ npm run dev |
| 150 | +``` |
| 151 | + |
| 152 | +### Technology labels |
| 153 | + |
| 154 | +- React |
| 155 | +- Typescript |
| 156 | +- Web |
0 commit comments