|
| 1 | +:::anchor Customize contextmenu |
| 2 | + |
| 3 | +:::demo |
| 4 | + |
| 5 | +```html |
| 6 | +<template> |
| 7 | + <div> |
| 8 | + <ve-table |
| 9 | + row-key-field-name="rowKey" |
| 10 | + :fixed-header="true" |
| 11 | + :columns="columns" |
| 12 | + :table-data="tableData" |
| 13 | + :row-style-option="rowStyleOption" |
| 14 | + border-y |
| 15 | + :contextmenu-body-option="contextmenuBodyOption" |
| 16 | + /> |
| 17 | + </div> |
| 18 | +</template> |
| 19 | + |
| 20 | +<script> |
| 21 | + export default { |
| 22 | + data() { |
| 23 | + return { |
| 24 | + // contextmenu body option |
| 25 | + contextmenuBodyOption: { |
| 26 | + // callback for all options |
| 27 | + callback: ({ type, selection }) => { |
| 28 | + const { rowKey, colKey } = selection; |
| 29 | +
|
| 30 | + const rowIndex = this.tableData.findIndex((x) => x.rowKey === rowKey); |
| 31 | +
|
| 32 | + // custom empty row |
| 33 | + if (type === "custom-empty-row") { |
| 34 | + this.tableData = this.tableData.map((rowData) => { |
| 35 | + // empty current row |
| 36 | + if (rowData.rowKey === rowKey) { |
| 37 | + this.columns.forEach((column) => { |
| 38 | + rowData[column.field] = ""; |
| 39 | + }); |
| 40 | + } |
| 41 | + return rowData; |
| 42 | + }); |
| 43 | + } |
| 44 | +
|
| 45 | + console.log("type::", type); |
| 46 | + console.log("selection::", selection); |
| 47 | + }, |
| 48 | +
|
| 49 | + // contextmenus |
| 50 | + contextmenus: [ |
| 51 | + { |
| 52 | + type: "INSERT_ROW_ABOVE", |
| 53 | + }, |
| 54 | + { |
| 55 | + type: "INSERT_ROW_BELOW", |
| 56 | + }, |
| 57 | + { |
| 58 | + type: "SEPARATOR", |
| 59 | + }, |
| 60 | + { |
| 61 | + type: "custom-empty-row", |
| 62 | + label: "empty row(custom)", |
| 63 | + }, |
| 64 | + { |
| 65 | + type: "customType1", |
| 66 | + label: "custom menu", |
| 67 | + children: [ |
| 68 | + { |
| 69 | + label: "menu5-1", |
| 70 | + type: "menu5-1-type", |
| 71 | + children: [ |
| 72 | + { |
| 73 | + label: "menu5-1-1", |
| 74 | + type: "menu5-1-1-type", |
| 75 | + }, |
| 76 | + { |
| 77 | + label: "menu5-2-2", |
| 78 | + type: "menu5-2-2-type", |
| 79 | + }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + { |
| 83 | + label: "menu5-2", |
| 84 | + disabled: true, |
| 85 | + }, |
| 86 | + { |
| 87 | + type: "SEPARATOR", |
| 88 | + }, |
| 89 | + { |
| 90 | + label: "menu5-3", |
| 91 | + type: "menu5-3-type", |
| 92 | + }, |
| 93 | + ], |
| 94 | + }, |
| 95 | + ], |
| 96 | + }, |
| 97 | +
|
| 98 | + rowStyleOption: { |
| 99 | + clickHighlight: false, |
| 100 | + }, |
| 101 | + columns: [ |
| 102 | + { |
| 103 | + field: "", |
| 104 | + key: "a", |
| 105 | + title: "", |
| 106 | + width: 50, |
| 107 | + align: "center", |
| 108 | + renderBodyCell: ({ row, column, rowIndex }, h) => { |
| 109 | + return ++rowIndex; |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + field: "name", |
| 114 | + key: "name", |
| 115 | + title: "Name", |
| 116 | + align: "left", |
| 117 | + width: "15%", |
| 118 | + }, |
| 119 | + { |
| 120 | + field: "date", |
| 121 | + key: "date", |
| 122 | + title: "Date", |
| 123 | + align: "left", |
| 124 | + width: "15%", |
| 125 | + }, |
| 126 | + { |
| 127 | + field: "number", |
| 128 | + key: "number", |
| 129 | + title: "Number", |
| 130 | + align: "right", |
| 131 | + width: "30%", |
| 132 | + }, |
| 133 | + { |
| 134 | + field: "address", |
| 135 | + key: "address", |
| 136 | + title: "Address", |
| 137 | + align: "left", |
| 138 | + width: "40%", |
| 139 | + }, |
| 140 | + ], |
| 141 | + // table data |
| 142 | + tableData: [ |
| 143 | + { |
| 144 | + name: "John", |
| 145 | + date: "1900-05-20", |
| 146 | + number: "32", |
| 147 | + address: "No.1 Century Avenue, Shanghai", |
| 148 | + rowKey: 0, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "Dickerson", |
| 152 | + date: "1910-06-20", |
| 153 | + number: "676", |
| 154 | + address: "No.1 Century Avenue, Beijing", |
| 155 | + rowKey: 1, |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "Larsen", |
| 159 | + date: "2000-07-20", |
| 160 | + number: "76", |
| 161 | + address: "No.1 Century Avenue, Chongqing", |
| 162 | + rowKey: 2, |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "Geneva", |
| 166 | + date: "2010-08-20", |
| 167 | + number: "7797", |
| 168 | + address: "No.1 Century Avenue, Xiamen", |
| 169 | + rowKey: 3, |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "Jami", |
| 173 | + date: "2020-09-20", |
| 174 | + number: "8978", |
| 175 | + address: "No.1 Century Avenue, Shenzhen", |
| 176 | + rowKey: 4, |
| 177 | + }, |
| 178 | + ], |
| 179 | + }; |
| 180 | + }, |
| 181 | + }; |
| 182 | +</script> |
| 183 | +``` |
| 184 | + |
| 185 | +::: |
0 commit comments