1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
class BasicTableData {
constructor({ form = {}, columns = [], formShow = false, page = new Page(), selectedIdList = [], fileSelectorShow = false, } = {}) { this.form = form this.columns = columns this.formShow = formShow this.page = page this.selectedIdList = selectedIdList this.fileSelectorShow = fileSelectorShow } }
class BasicTableMethods {
constructor({ createForm = function () { throw new Error('如果需要搜索条件,请重写 initForm() 方法') }, getPage = async function (page, entity) { throw new Error('如果需要自动分页,请重写 getPage() 方法') }, exportFile = async function () { throw new Error('如果需要导出数据,请重写 exportFile() 方法') }, importFile = function () { throw new Error('如果需要导入数据,请重写 importFile() 方法') }, deleteData = async function (idList) { throw new Error('如果需要删除数据,请重写 deleteData 方法') }, init = async function () {}, resetFile = function () { const $el = this.$refs['fileInput'] if (!$el) { throw new Error( '如果需要清空选择文件,请为 input:file 绑定属性 ref 的值为 fileInput', ) } $el.value = '' }, searchPage = async function () { try { this.page = await this.getPage(this.page, this.form) } catch (e) { console.error(e) await rxPrompt.dangerMsg('查询数据失败,请刷新页面') } }, resetPage = async function () { this.form = this.createForm() await this.searchPage() }, toggle = function () { this.formShow = !this.formShow }, selection = function (data) { this.selectedIdList = data.map(({ id }) => id) }, changeSize = function (size) { this.page.current = 1 this.page.size = size this.searchPage() }, goto = function (current) { if (!current) { current = this.page.current } if (current < 1) { return } if (current > this.page.pages) { return } this.page.current = current this.searchPage() }, deleteSelected = async function () { const result = await this.deleteData(this.selectedIdList) if (result.code !== 200 || !result.data) { await rxPrompt.msg('') return } // noinspection JSIgnoredPromiseFromCall rxPrompt.msg('删除成功') this.page.current = 1 await this.searchPage() }, showFileSelector = function () { this.fileSelectorShow = !this.fileSelectorShow }, initCommon = async function () { this.form = this.createForm() this.searchPage() }, } = {}) { this.createForm = createForm this.getPage = getPage this.searchPage = searchPage this.resetPage = resetPage this.toggle = toggle this.selection = selection this.changeSize = changeSize this.goto = goto this.exportFile = exportFile this.importFile = importFile this.resetFile = resetFile this.deleteData = deleteData this.init = init this.deleteSelected = deleteSelected this.showFileSelector = showFileSelector this.initCommon = initCommon } }
class BasicTableOption {
constructor({ el = '#app', data = new BasicTableData(), methods = new BasicTableMethods(), mounted = async function () { await this.initCommon() await this.init() }, } = {}) { this.el = el this.data = data this.methods = methods this.mounted = mounted } }
class BasicTableVue extends Vue {
constructor({ data, methods, mounted, ...args } = {}) { super( _.merge(new BasicTableOption(), { data: function () { return _.merge( new BasicTableData(), typeof data === 'function' ? data.call(this) : data, ) }, methods, mounted, ...args, }), ) } }
|