|
| 1 | +# Event |
| 2 | + |
| 3 | +All `el-table` table [events](http://element.eleme.io/#/en-US/component/table#table-events) have been proxied by `vue-data-tables` |
| 4 | + |
| 5 | +# row-click and cell-click |
| 6 | + |
| 7 | +`row-click` and `cell-click` are treated differently(not a simple proxy). |
| 8 | +SomeTimes, `row-click` and `cell-click` may not want to be triggered, when some columns are clicked. For example, In `table action column`, there may be lots of buttons. when you click these buttons, you often click the `td` element, which will trigger `row-click` and `cell-click` event. property `col-not-row-click` can be leveraged to indicate the columns, which you don't want them to trigger `row-click` and `cell-click`. |
| 9 | + |
| 10 | +> `table action column` is marked as `can not trigger click` by deault. |
| 11 | +
|
| 12 | +```html |
| 13 | +/*vue*/ |
| 14 | +<desc> |
| 15 | +`flow_no` and `table action column`(default) are marked as `can not trigger click` |
| 16 | +</desc> |
| 17 | +<template> |
| 18 | + <data-tables |
| 19 | + :data='data' |
| 20 | + :col-not-row-click="canNotClickList" |
| 21 | + :action-col-def="actionColDef" |
| 22 | + @row-click='handleRowClick'> |
| 23 | + <el-table-column v-for="title in titles" |
| 24 | + :prop="title.prop" |
| 25 | + :label="title.label" |
| 26 | + :key="title.label" |
| 27 | + sortable="custom"> |
| 28 | + </el-table-column> |
| 29 | + </data-tables> |
| 30 | +</template> |
| 31 | + |
| 32 | +<script> |
| 33 | +export default { |
| 34 | + data() { |
| 35 | + return { |
| 36 | + data, |
| 37 | + titles, |
| 38 | + canNotClickList: ['flow_no'], |
| 39 | + actionColDef: { |
| 40 | + label: 'Actions', |
| 41 | + def: [{ |
| 42 | + handler: row => { |
| 43 | + this.$message('Edit clicked') |
| 44 | + row.flow_no = "hello word" |
| 45 | + }, |
| 46 | + name: 'Edit' |
| 47 | + }] |
| 48 | + } |
| 49 | + } |
| 50 | + }, |
| 51 | + methods: { |
| 52 | + handleRowClick(row, event, column) { |
| 53 | + this.$message(`${row.flow_no} is clicked`) |
| 54 | + console.log(row, event, column) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +</script> |
| 59 | +``` |
| 60 | + |
| 61 | +# filtered-data |
| 62 | +trigger when any filter change(search box, checkbox, sort or customFilters), and pass filted data out. |
| 63 | + |
| 64 | +> Combined with 3rd-party library, such as: [json2csv](https://github.com/zemirco/json2csv) and [alasql](https://github.com/agershun/alasql), filtered data can be exported. |
| 65 | +
|
| 66 | +```html |
| 67 | +/*vue*/ |
| 68 | +<desc> |
| 69 | +export data to excel |
| 70 | +</desc> |
| 71 | +<template> |
| 72 | + <data-tables |
| 73 | + :data='data' |
| 74 | + :actions-def='actionsDef' |
| 75 | + @filtered-data='handleFilteredData'> |
| 76 | + <el-table-column v-for="title in titles" |
| 77 | + :prop="title.prop" |
| 78 | + :label="title.label" |
| 79 | + :key="title.label" |
| 80 | + sortable="custom"> |
| 81 | + </el-table-column> |
| 82 | + </data-tables> |
| 83 | +</template> |
| 84 | + |
| 85 | +<script> |
| 86 | +includeJs("//unpkg.com/[email protected]/dist/json2csv.js"); |
| 87 | +
|
| 88 | +let CsvExport = function(data, fields, fieldNames, fileName) { |
| 89 | + try { |
| 90 | + var result = json2csv({ |
| 91 | + data: data, |
| 92 | + fields: fields, |
| 93 | + fieldNames: fieldNames |
| 94 | + }) |
| 95 | + var csvContent = 'data:text/csvcharset=GBK,\uFEFF' + result |
| 96 | + var encodedUri = encodeURI(csvContent) |
| 97 | + var link = document.createElement('a') |
| 98 | + link.setAttribute('href', encodedUri) |
| 99 | + link.setAttribute('download', `${(fileName || 'file')}.csv`) |
| 100 | + document.body.appendChild(link) |
| 101 | + link.click() |
| 102 | + document.body.removeChild(link) |
| 103 | + } catch (err) { |
| 104 | + console.error(err) |
| 105 | + } |
| 106 | +} |
| 107 | +
|
| 108 | +export default { |
| 109 | + data() { |
| 110 | + return { |
| 111 | + data, |
| 112 | + titles, |
| 113 | + canNotClickList: ['flow_no'], |
| 114 | + actionsDef: [], |
| 115 | + filteredData: [] |
| 116 | + } |
| 117 | + }, |
| 118 | + created() { |
| 119 | + let columns = ['flow_no', 'content', 'flow_type'] |
| 120 | + let columnNames = ['Flow NO.', 'Content', 'type'] |
| 121 | + this.actionsDef = { |
| 122 | + colProps: { |
| 123 | + span: 19 |
| 124 | + }, |
| 125 | + def: [{ |
| 126 | + name: 'export all', |
| 127 | + handler: () => { |
| 128 | + CsvExport(this.data, columns, columnNames, 'all') |
| 129 | + }, |
| 130 | + icon: 'plus' |
| 131 | + }, { |
| 132 | + name: 'export filtered', |
| 133 | + handler: () => { |
| 134 | + CsvExport(this.filteredData, columns, columnNames, 'filtered') |
| 135 | + }, |
| 136 | + icon: 'upload' |
| 137 | + }] |
| 138 | + } |
| 139 | + }, |
| 140 | + methods: { |
| 141 | + handleFilteredData(filteredData) { |
| 142 | + this.filteredData = filteredData |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | +</script> |
| 147 | +``` |
0 commit comments