|
| 1 | +# AgGrid Table |
| 2 | + |
| 3 | +This is a basic introduction on how to use [AgGrid Table](https://www.ag-grid.com/) together with Windmill. |
| 4 | +It assumes little to no knowledge about AgGrid. |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +## What is AgGrid Table. |
| 9 | +The AgGrid Table component (called AgGrid from here) is a small wrapper around a fantstic libary called [AgGrid](https://www.ag-grid.com/). |
| 10 | +It provides you with a lot of advanced features. It comes in a free and a Enterprise (paid) version. |
| 11 | +All features below are part of the free version of AgGrid. |
| 12 | + |
| 13 | +:::tip Enterprise |
| 14 | +If you need the enterprise version of AgGrid, please [contact us](../../6_getting_help/index.md). |
| 15 | +::: |
| 16 | + |
| 17 | +## AgGrid vs Table component |
| 18 | + |
| 19 | +In Windmill there are 2 table components: one simply called [Table](../../../apps/4_app_component_library.md#table) and AgGrid. |
| 20 | + |
| 21 | +The Table component is for basic usecases. It takes an array of objects as input, and uses the key of the object as the header of the table. It also provides you with one or more action buttons to trigger an action for the row. |
| 22 | + |
| 23 | +But if you want the user to be able to sort the table, edit a value, adjust width and more you want to reach for AgGrid. |
| 24 | + |
| 25 | +:::info Transformer |
| 26 | + If you want to do basic sorting, or edit the column header name from the script you can also use a Transformer script. |
| 27 | + See the [documentation](../../../apps/6_app_settings.md#tranformers) for more information. |
| 28 | +::: |
| 29 | + |
| 30 | +## Column Definition |
| 31 | + |
| 32 | +AgGrid needs two inputs, rowdata and column definitions. |
| 33 | +By default AgGrid does not show the rowdata. You need to specify the properties of each column. |
| 34 | +This is done in the Configuration on the right side. |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +You can staticly set the properties of each column and many properties are available. |
| 39 | +Let's walk trough the one you may be most likley to use: |
| 40 | + |
| 41 | +* field (string) - which field to use from the rowdata | [documentation](https://www.ag-grid.com/javascript-data-grid/column-properties/#reference-columns-field) |
| 42 | +* headerName (string) - rename the column header to something other than the field name | [documentation](https://www.ag-grid.com/javascript-data-grid/column-properties/#reference-header-headerName) |
| 43 | +* sortable (boolean)- should the column be sortable by the user | [documentation](https://www.ag-grid.com/javascript-data-grid/column-properties/#reference-sort-sortable) |
| 44 | +* sort (asc|desc) - which order to sort the column | [documentation](https://www.ag-grid.com/javascript-data-grid/column-properties/#reference-sort-sort) |
| 45 | +* resizable (boolean) - should the column be resizable | [documentation](https://www.ag-grid.com/javascript-data-grid/column-properties/#reference-width-resizable) |
| 46 | +* rowDrag (boolean) - should the user be able to drag the column | [documentation](https://www.ag-grid.com/javascript-data-grid/column-properties/#reference-row%20dragging-rowDrag) |
| 47 | + |
| 48 | +There is *a lot* more properties, this is just the first few. |
| 49 | +See [Column Properties](https://www.ag-grid.com/javascript-data-grid/column-properties/) for the complete list of properties. |
| 50 | + |
| 51 | +### Dynamicly configure the column definition |
| 52 | + |
| 53 | +As with most things, Windmill lets you **dropdown to code** when you want to do more advanced stuff, where the gui is more in the way then helping. |
| 54 | + |
| 55 | +The Windmill way wil be to first create a background script and then connecting it with the col def |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +Here you can provide your own column definition that is more then a JSON but also includes classes and functions. |
| 60 | + |
| 61 | +Below is a series of snippest that will help you get started. |
| 62 | + |
| 63 | +#### Provide default values for all columns |
| 64 | + |
| 65 | +Create a background script in deno with the following content: |
| 66 | +```js |
| 67 | + export async function main() { |
| 68 | + const columnDef = [ |
| 69 | + { |
| 70 | + field: "name", |
| 71 | + headerName: "Full name", |
| 72 | + }, |
| 73 | + { |
| 74 | + field: "age", |
| 75 | + sortable: false, |
| 76 | + }, |
| 77 | + ]; |
| 78 | + |
| 79 | + const defaultColumnProperties = { |
| 80 | + sortable: true, |
| 81 | + }; |
| 82 | + |
| 83 | + return columnDef.map((col) => ({ ...defaultColumnProperties, ...col })); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +#### Provide default values for all columns |
| 88 | + |
| 89 | +Create a background script in deno with the following content: |
| 90 | +```ts |
| 91 | + export async function main() { |
| 92 | + const columnDef = [ |
| 93 | + { |
| 94 | + field: "name", |
| 95 | + headerName: "Full name", |
| 96 | + }, |
| 97 | + { |
| 98 | + field: "age", |
| 99 | + sortable: false, |
| 100 | + }, |
| 101 | + ]; |
| 102 | + |
| 103 | + const defaultColumnProperties = { |
| 104 | + sortable: true, |
| 105 | + }; |
| 106 | + |
| 107 | + return columnDef.map((col) => ({ ...defaultColumnProperties, ...col })); |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +#### Create a select button |
| 112 | + |
| 113 | +Here we are using a AgGrid Component called `agSelectCellEditor`. There [exist more predefined cell components](https://www.ag-grid.com/javascript-data-grid/provided-cell-editors/#select-cell-editor) like this. |
| 114 | + |
| 115 | + |
| 116 | +Create a frontend script in JS with the following content: |
| 117 | +```js |
| 118 | +return [ |
| 119 | + { |
| 120 | + field: "name", |
| 121 | + headerName: "Full name", |
| 122 | + }, |
| 123 | + { |
| 124 | + field: "age", |
| 125 | + cellEditorParams: function(params) { |
| 126 | + return { values: [1, params.data.age, 100 ] } |
| 127 | + }, |
| 128 | + cellEditor: 'agSelectCellEditor', |
| 129 | + editable: true, |
| 130 | + useFormatter: true |
| 131 | + |
| 132 | + } |
| 133 | +] |
| 134 | +``` |
| 135 | +and connect it to the column definition. |
| 136 | + |
| 137 | +If you want to act on changes in the select dropdown, you may use the components states `newChanges`, or `selectedRow`. |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +### Create a button (custom component) |
| 143 | +Create a frontend script in JS with the following content: |
| 144 | +```js |
| 145 | + |
| 146 | +class BtnCellRenderer { |
| 147 | + constructor() { } |
| 148 | + init(params) { |
| 149 | + this.params = params; |
| 150 | + this.eGui = document.createElement('button'); |
| 151 | + this.eGui.innerHTML = 'Push me!'; |
| 152 | + this.btnClickedHandler = this.btnClickedHandler.bind(this); |
| 153 | + this.eGui.addEventListener('click', this.btnClickedHandler); |
| 154 | + } |
| 155 | + getGui() { |
| 156 | + return this.eGui; |
| 157 | + } |
| 158 | + destroy() { |
| 159 | + this.eGui.removeEventListener('click', this.btnClickedHandler); |
| 160 | + } |
| 161 | +} |
| 162 | +BtnCellRenderer.prototype.btnClickedHandler = function () { |
| 163 | + if(!state.logs) { |
| 164 | + state.logs = [] |
| 165 | + } |
| 166 | + state.logs.push({ |
| 167 | + rowDataAfterChange: JSON.stringify(this.params.data) |
| 168 | + }) |
| 169 | +} |
| 170 | + |
| 171 | +return [ |
| 172 | + { field: 'name' }, |
| 173 | + { field: 'age' }, |
| 174 | + { |
| 175 | + "headerName": "Send row data to state", |
| 176 | + "cellRenderer": BtnCellRenderer |
| 177 | + } |
| 178 | +] |
| 179 | + |
| 180 | +``` |
| 181 | +and connect it to the column definition. |
| 182 | + |
| 183 | +This example demonstrates how to create a custom cell renderer component. Where we put the data from the row into |
| 184 | +our state. from here we can use other scripts to act on the row data as we like. |
| 185 | + |
| 186 | + |
| 187 | +You can create a lot of components, not only for cell rendering, to understand how check out [AgGrid's documentation for components](https://www.ag-grid.com/javascript-data-grid/components/). |
0 commit comments