|
| 1 | +--- |
| 2 | +id: advanced-plugins |
| 3 | +title: Advanced Plugin |
| 4 | +--- |
| 5 | + |
| 6 | +import { LiveExample } from "../../lib/liveExample.js"; |
| 7 | + |
| 8 | +Now that we know how to write a basic plugin, let's take a look at some complex examples. |
| 9 | + |
| 10 | +## Using the pipeline |
| 11 | + |
| 12 | +Grid.js has an internal pipeline which is the brain of Grid.js and takes care of processing, filter and refining the data. |
| 13 | +You can always get access to the current pipeline by using `this.config.pipeline` in your plugin component (make sure you have extended `BaseComponent`). |
| 14 | + |
| 15 | +In this example, we have a table of Name (string) and Salary (double) and our custom plugin is in charge of summing salaries |
| 16 | +and displaying the total in the footer. |
| 17 | + |
| 18 | +<LiveExample children={ |
| 19 | +` |
| 20 | +class TotalSalaryPlugin extends BaseComponent { |
| 21 | + constructor(...props) { |
| 22 | + super(...props); |
| 23 | + |
| 24 | + this.state = { |
| 25 | + total: 0 |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + setTotal() { |
| 30 | + this.config.pipeline.process().then(data => { |
| 31 | + this.setState({ |
| 32 | + total: data.toArray().reduce((prev, row) => prev + row[1], 0) |
| 33 | + }); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + componentDidMount() { |
| 38 | + // initial setState |
| 39 | + this.setTotal(); |
| 40 | + this.config.pipeline.on('updated', this.setTotal.bind(this)); |
| 41 | + } |
| 42 | + |
| 43 | + render() { |
| 44 | + return h('b', {}, \`Total: $\${this.state.total.toLocaleString()}\`); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +const grid = new Grid({ |
| 49 | + search: true, |
| 50 | + sort: true, |
| 51 | + columns: ['Name', 'Salary'], |
| 52 | + data: [ |
| 53 | + ['John', Math.round(Math.random() * 100000)], |
| 54 | + ['Mark', Math.round(Math.random() * 100000)], |
| 55 | + ['Josh', Math.round(Math.random() * 100000)], |
| 56 | + ['Sara', Math.round(Math.random() * 100000)], |
| 57 | + ['Maria', Math.round(Math.random() * 100000)], |
| 58 | + ] |
| 59 | +}); |
| 60 | + |
| 61 | +grid.plugin.add({ |
| 62 | + id: 'salaryplugin', |
| 63 | + component: h(TotalSalaryPlugin, {}), |
| 64 | + position: PluginPosition.Footer, |
| 65 | +}); |
| 66 | +` |
| 67 | +} /> |
| 68 | + |
| 69 | +## Using the translation |
| 70 | + |
| 71 | +Likewise, you can get access to the Translator object and localize strings in your custom plugin: |
| 72 | + |
| 73 | +<LiveExample children={ |
| 74 | +` |
| 75 | +class HelloPlugin extends BaseComponent { |
| 76 | + render() { |
| 77 | + return h('b', {}, this._('hello')); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +const grid = new Grid({ |
| 82 | + columns: ['Name', 'Salary'], |
| 83 | + data: [ |
| 84 | + ['John', Math.round(Math.random() * 100000)], |
| 85 | + ['Mark', Math.round(Math.random() * 100000)], |
| 86 | + ['Josh', Math.round(Math.random() * 100000)], |
| 87 | + ], |
| 88 | + language: { |
| 89 | + 'hello': 'bonjour!!' |
| 90 | + } |
| 91 | +}); |
| 92 | + |
| 93 | +grid.plugin.add({ |
| 94 | + id: 'bonjour', |
| 95 | + component: h(HelloPlugin, {}), |
| 96 | + position: PluginPosition.Header, |
| 97 | +}); |
| 98 | +` |
| 99 | +} /> |
0 commit comments