├── .browserslistrc ├── babel.config.js ├── postcss.config.js ├── public ├── example.gif ├── favicon.ico └── index.html ├── .editorconfig ├── vue.config.js ├── .gitignore ├── .npmignore ├── .eslintrc.js ├── src ├── index.js ├── easyGoTopMixin.js ├── EleTablePagination.vue ├── EleTableToolbar.vue └── EleTable.vue ├── example ├── main.js └── App.vue ├── LICENSE ├── package.json └── README.md /.browserslistrc: -------------------------------------------------------------------------------- 1 | > 1% 2 | last 2 versions 3 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: [ 3 | '@vue/app' 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | autoprefixer: {} 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /public/example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dream2023/vue-ele-table/HEAD/public/example.gif -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dream2023/vue-ele-table/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | [*.{js,jsx,ts,tsx,vue}] 2 | indent_style = space 3 | indent_size = 2 4 | trim_trailing_whitespace = true 5 | insert_final_newline = true 6 | -------------------------------------------------------------------------------- /vue.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | css: { extract: false }, 3 | configureWebpack: { 4 | entry: './example/main.js', 5 | output: { 6 | libraryExport: 'default' 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /dist 4 | 5 | # local env files 6 | .env.local 7 | .env.*.local 8 | 9 | # Log files 10 | npm-debug.log* 11 | yarn-debug.log* 12 | yarn-error.log* 13 | 14 | # Editor directories and files 15 | .idea 16 | .vscode 17 | *.suo 18 | *.ntvs* 19 | *.njsproj 20 | *.sln 21 | *.sw? 22 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | tests 3 | docs 4 | documentation 5 | public 6 | vue.config.js 7 | coverage 8 | example 9 | jest.config.js 10 | .eslintrc.js 11 | cypress.json 12 | .browserslistrc 13 | postcss.config.js 14 | babel.config.js 15 | .editorconfig 16 | .cypress.json 17 | dist/demo.html 18 | .env.dev-doc 19 | .env.dev-example 20 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | env: { 4 | node: true 5 | }, 6 | 'extends': [ 7 | 'plugin:vue/essential', 8 | '@vue/standard' 9 | ], 10 | rules: { 11 | 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off', 12 | 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off' 13 | }, 14 | parserOptions: { 15 | parser: 'babel-eslint' 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import EleTable from './EleTable' 2 | 3 | const Plugin = {} 4 | 5 | Plugin.install = function (Vue, params = {}) { 6 | Vue.prototype.$EleTableParams = params 7 | 8 | // vue-ele-editable 的全局配置 9 | if (params && params.editable) { 10 | Vue.prototype.$EleEditableParams = params.editable 11 | } 12 | Vue.component(EleTable.name, EleTable) 13 | } 14 | 15 | if (typeof window !== 'undefined' && window.Vue) { 16 | Plugin.install(window.Vue) 17 | } 18 | 19 | export default Plugin 20 | -------------------------------------------------------------------------------- /example/main.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import App from './App.vue' 3 | import EleTable from '../src/index' 4 | import ElementUI from 'element-ui' 5 | import 'element-ui/lib/theme-chalk/index.css' 6 | 7 | Vue.config.productionTip = false 8 | Vue.use(ElementUI) 9 | Vue.use(EleTable, { 10 | // 默认每页显示个数 11 | defaultSize: 5, 12 | // editable相关 13 | editable: { 14 | image: { 15 | lazy: true 16 | }, 17 | 'upload-image': { 18 | lazy: true 19 | } 20 | } 21 | }) 22 | 23 | new Vue({ 24 | render: h => h(App) 25 | }).$mount('#app') 26 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | vue-ele-table 9 | 10 | 11 | 14 |
15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/easyGoTopMixin.js: -------------------------------------------------------------------------------- 1 | export default { 2 | data () { 3 | return { 4 | easyGoTopIsMoving: false 5 | } 6 | }, 7 | methods: { 8 | $goTop (backPosition = 0) { 9 | if (this.easyGoTopIsMoving) return 10 | const start = window.pageYOffset 11 | let i = 0 12 | this.easyGoTopIsMoving = true 13 | this.interval = setInterval(() => { 14 | const next = Math.floor( 15 | this.easyGoTopEaseInOutQuad(10 * i, start, -start, 500) 16 | ) 17 | if (next <= backPosition) { 18 | window.scrollTo(0, backPosition) 19 | clearInterval(this.interval) 20 | this.easyGoTopIsMoving = false 21 | } else { 22 | window.scrollTo(0, next) 23 | } 24 | i++ 25 | }, 16.7) 26 | }, 27 | easyGoTopEaseInOutQuad (t, b, c, d) { 28 | if ((t /= d / 2) < 1) return (c / 2) * t * t + b 29 | return (-c / 2) * (--t * (t - 2) - 1) + b 30 | } 31 | }, 32 | beforeDestroy () { 33 | clearInterval(this.interval) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/EleTablePagination.vue: -------------------------------------------------------------------------------- 1 | 15 | 16 | 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 二当家的 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE.m -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vue-ele-table", 3 | "version": "0.1.5", 4 | "description": "基于 element-ui 功能强大的表格组件", 5 | "license": "MIT", 6 | "private": false, 7 | "scripts": { 8 | "serve": "vue-cli-service serve", 9 | "build:lib": "vue build -t lib --name vue-ele-table -d ./dist/ ./src/index.js", 10 | "build": "npm run lint & npm run build:lib", 11 | "lint": "vue-cli-service lint" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/dream2023/vue-ele-table" 16 | }, 17 | "main": "dist/vue-ele-table.umd.min.js", 18 | "homepage": "https://github.com/dream2023/vue-ele-table", 19 | "keywords": [ 20 | "vue-ele-table", 21 | "vue table", 22 | "element table", 23 | "element", 24 | "element-ui table", 25 | "element table", 26 | "el-editable", 27 | "datatable" 28 | ], 29 | "dependencies": { 30 | "vue-ele-editable": "^1.0.3" 31 | }, 32 | "devDependencies": { 33 | "@vue/cli-plugin-babel": "^3.8.0", 34 | "@vue/cli-plugin-eslint": "^3.8.0", 35 | "@vue/cli-service": "^3.8.0", 36 | "@vue/eslint-config-standard": "^4.0.0", 37 | "axios": "^0.19.0", 38 | "babel-eslint": "^10.0.1", 39 | "core-js": "^2.6.5", 40 | "element-ui": "^2.9.2", 41 | "eslint": "^5.16.0", 42 | "eslint-plugin-vue": "^5.0.0", 43 | "lint-staged": "^8.1.5", 44 | "mockjs": "^1.0.1-beta3", 45 | "vue": "^2.6.10", 46 | "vue-template-compiler": "^2.6.10" 47 | }, 48 | "gitHooks": { 49 | "pre-commit": "lint-staged" 50 | }, 51 | "lint-staged": { 52 | "*.{js,vue}": [ 53 | "vue-cli-service lint", 54 | "git add" 55 | ] 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/EleTableToolbar.vue: -------------------------------------------------------------------------------- 1 | 85 | 86 | 226 | 227 | 269 | -------------------------------------------------------------------------------- /example/App.vue: -------------------------------------------------------------------------------- 1 | 24 | 25 | 261 | 262 | 267 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vue-ele-table | 基于 element-ui 的表格二次封装 2 | 3 | [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg)](https://opensource.org/licenses/mit-license.php) 4 | [![npm](https://img.shields.io/npm/v/vue-ele-table.svg)](https://www.npmjs.com/package/vue-ele-table) 5 | [![download](https://img.shields.io/npm/dw/vue-ele-table.svg)](https://npmcharts.com/compare/vue-ele-editable?minimal=true) 6 | 7 | ## 说明 8 | 9 | vue-ele-table 是基于[element-ui table](https://element.eleme.cn/#/zh-CN/component/table) 和 [vue-ele-editable](https://github.com/dream2023/vue-ele-editable) 的进一步封装, 内置了 搜索、排序、筛选、过滤、分页、显示隐藏列、行内编辑 功能, 隐藏了细节, 目的是为了保证开发的质量, 加快开发的速度 😁。 10 | 11 | ⚠️ 但是, 由于在接口的设计上, 每个项目千差万别, 本来想将本组件变得更加通用和适应, 但是发现越改操作越复杂, 违背了当初的意愿。所以您如果想要应用到自己的项目中, 一种方法是可以参考本项目的 API 设计规范, 另一种方法是 clone/download 后, 放到自己项目中进行修改, 另外如果您有好的建议, 尽管提 issue, 一定第一时间响应(PS: 本项目代码注释相当详细, 相信你能看懂) 12 | 13 | > 如果 star 超过 100, 有视频详细源码讲解 14 | 15 | ## 演示 16 | 17 | [![演示图](./public/example.gif)](https://codepen.io/dream2023/pen/agwMpY) 18 | 19 | ## 在线示例 20 | 21 | [https://codepen.io/dream2023/pen/agwMpY](https://codepen.io/dream2023/pen/agwMpY) 22 | 23 | ## 安装 24 | 25 | ```bash 26 | npm install vue-ele-table --save 27 | ``` 28 | 29 | ## 使用 30 | 31 | ```js 32 | import EleTable from 'vue-ele-table' 33 | Vue.use(EleTable) 34 | 35 | // 在引入 EleTable 时,可以传入一个全局配置对象, 例如: 36 | Vue.use(EleTable, { 37 | // 每页显示的数量 38 | defaultSize: 10, 39 | // 用于改变请求的key, 40 | // 例如: 原 { page: 1, size: 20 }, 经过下面设置后, 就会变为: { page: 1, per_page: 20 } 41 | paramsKey: { 42 | size: 'per_page' 43 | }, 44 | // editable 相关的配置, 具体参考: 45 | // https://github.com/dream2023/vue-ele-editable 46 | editable: { 47 | image: { 48 | lazy: true 49 | } 50 | // ... 51 | } 52 | }) 53 | ``` 54 | 55 | ## API 规范 56 | 57 | ### 返回数据格式 58 | 59 | ```js 60 | { 61 | "total": Number, // 总条数 62 | "list": Array // 数据列表 63 | }, 64 | // 或者 直接数组 65 | [ 66 | { }, 67 | { } 68 | ] 69 | 70 | 71 | // 例如: 72 | // 默认分页 73 | { 74 | "total": 300, 75 | "list": [ 76 | { id: 1, age: 19, name: '张三' }, 77 | { id: 2, age: 33, name: '李四' }, 78 | { id: 3, age: 22, name: '王五' }, 79 | ] 80 | } 81 | 82 | // 直接返回数组, 则表明无需分页 83 | [ 84 | { id: 1, age: 19, name: '张三' }, 85 | { id: 2, age: 33, name: '李四' }, 86 | { id: 3, age: 22, name: '王五' }, 87 | ] 88 | ``` 89 | 90 | ### 分页控制 91 | 92 | ```js 93 | { 94 | "page": Number, // 页码, 默认为 1 95 | "size": Number, // 每页显示条数,默认值 20 96 | }, 97 | 98 | // 例如: 99 | // 也就是 当前是 第2页, 每页显示 30 条 100 | { 101 | "page": 2, 102 | "size": 30 103 | } 104 | ``` 105 | 106 | ### 排序控制 107 | 108 | ```js 109 | // 请求参数 110 | { 111 | "_order_field": String, // 排序字段名 112 | "_order_direction": "asc" || "desc", // 正序 或 倒序 113 | }, 114 | 115 | // 例如: 116 | // 含义: 排序字段是 id, 正序 asc 117 | { 118 | "_order_field": 'id', 119 | "_order_direction": "asc" 120 | } 121 | ``` 122 | 123 | ### 筛选控制 124 | 125 | ```js 126 | { 127 | "_filter": String, // 支持多筛选, 用 | 分割不同字段, 用 : 分割值和字段, 用 , 分割值 128 | 129 | // 例如: 130 | // 含义: (type=1 或 type = 2 或 type=3) 且 name = 'abc' 131 | "_filter": "type:1,2,3|name:abc" 132 | } 133 | 134 | 135 | ``` 136 | 137 | ### 时间筛选控制 138 | 139 | ```js 140 | { 141 | "_filter_time" : String, // 分割原理同上 142 | 143 | // 例如: 144 | // 含义: (create_time > 12121212 且 create_time < 13131313) 且 (update_time > 14141414 且 update_time < 15151515) 145 | "_filter_time" = "create_time:12121212,13131313|update_time:14141414,15151515", 146 | } 147 | ``` 148 | 149 | ### 搜索控制 150 | 151 | ```js 152 | { 153 | "_search_field": "字段|字段|字段", 154 | "_search_keyword": "搜索内容" 155 | }, 156 | 157 | // 例如: 158 | // 含义: (name 或者 title 或者 article) 包含 学习vue 的记录 159 | { 160 | "_search_field": "name|title|article", 161 | "_search_keyword": "学习vue" 162 | } 163 | ``` 164 | 165 | ## Props 参数 166 | 167 | ```js 168 | props: { 169 | // 是否显示顶部删除按钮 170 | isShowTopDelete: { 171 | type: Boolean, 172 | default: true 173 | }, 174 | // 是否显示右侧删除按钮 175 | isShowRightDelete: { 176 | type: Boolean, 177 | default: true 178 | }, 179 | // 是否显示多选框 180 | isShowSelection: { 181 | type: Boolean, 182 | default: true 183 | }, 184 | // 是否显示index列 185 | isShowIndex: { 186 | type: Boolean, 187 | default: true 188 | }, 189 | // 是否显示分页 190 | isShowPagination: { 191 | type: Boolean, 192 | default: true 193 | }, 194 | // 主键 195 | // 用途: 删除, 更新等按钮操作 196 | primaryKey: { 197 | type: String, 198 | default: 'id' 199 | }, 200 | // 请求函数 201 | // 用途: 所有请求都有内部完成, 所以需要一个请求函数, 改函数的返回值必须是一个Promise实例 202 | requestFn: { 203 | type: Function, 204 | required: true 205 | }, 206 | // 更新函数 207 | // 用途: 用于行内编辑时, 更新数据, 具体参考: https://github.com/dream2023/vue-ele-editable 208 | updateFn: Function, 209 | // 批量删除函数 210 | deletesFn: Function, 211 | // 删除函数 212 | deleteFn: Function, 213 | // 表格描述, 下面会详细论述 214 | tableDesc: { 215 | type: Object, 216 | default () { 217 | return {} 218 | } 219 | }, 220 | // 列描述, 下面会详细论述 221 | columnsDesc: { 222 | type: Object, 223 | required: true 224 | }, 225 | // 顶部按钮, 下面会详细论述 226 | topButtons: Array, 227 | // 顶部时间过滤字段 228 | // 这个比较简单, 只需要传递字段就行了, 例如 ['create_time', 'update_time'] 或者 'create_time' 229 | topTime: [String, Array], 230 | // 顶部搜索字段, 下面会详细论述 231 | topSearch: [Object, Array], 232 | // 右侧按钮, 下面会详细论述 233 | rightButtons: { 234 | type: Array, 235 | default () { 236 | return [] 237 | } 238 | } 239 | } 240 | ``` 241 | 242 | ### columnsDesc 243 | 244 | ```html 245 | 246 | 255 | 274 | 275 | ``` 276 | 277 | ```js 278 | columnsDesc: { 279 | // 字段 280 | status: { 281 | text: '状态', // 表头名字 282 | sortable: true, // 该列是否需要排序, 283 | width: 100, // 列宽 284 | options: [ // 用于筛选 285 | { text: '正常', value: true }, // text 用来显示, value 是 text 对应的值 286 | { text: '禁用', value: false } 287 | ], 288 | columnAttrs: { // 其他关于 column 的属性, 都使用 columnAttrs 289 | resizable: false, // 禁止拖动 290 | }, 291 | 292 | // 下面的是关于 editable 相关的属性 293 | // https://github.com/dream2023/vue-ele-editable 294 | type: 'status', // 指定 列 显示的类型, 295 | defaultValue: 1, // 默认值, 296 | // ... 297 | }, 298 | name: { 299 | type: 'input', 300 | text: '姓名' 301 | }, 302 | // ... 303 | } 304 | ``` 305 | 306 | ### tableDesc 307 | 308 | ```html 309 | 310 | 316 | ``` 317 | 318 | ```js 319 | tableDesc: { 320 | class: {}, // 添加的类 321 | style: {}, // 添加的自定义样式 322 | attrs: {}, // 添加的自定义属性 323 | on: {}, // 添加的自定义事件 324 | } 325 | ``` 326 | 327 | ```js 328 | // 例子: 指定默认排序字段 329 | tableDesc: { 330 | attrs: { 331 | 'default-sort': {prop: 'date', order: 'descending'} 332 | } 333 | } 334 | ``` 335 | 336 | ### topButtons 337 | 338 | ```js 339 | { 340 | primaryKey: 'id', // 假定主键字段为 id 341 | topButtons: [ 342 | { 343 | text: '新增', // 文字 344 | attrs: { // btn相关属性 345 | type: 'success', 346 | }, 347 | click: (ids, rows) => { 348 | // 点击事件 349 | console.log(ids, rows) // 传递过来勾选中的列的 主键集合 和 列的集合 350 | } 351 | }, 352 | { 353 | // ... 354 | } 355 | ] 356 | } 357 | ``` 358 | 359 | ### rightButtons 360 | 361 | ```js 362 | { 363 | primaryKey: 'id', // 假定主键字段为 id 364 | rightButtons: [ 365 | { 366 | text: '编辑', // 显示的文字 367 | attrs: { // btn相关属性 368 | type: 'success', 369 | }, 370 | click: (id, index, row) => { 371 | // 点击事件 372 | console.log(id, index, row) // 传递过来 主键值, 行的index值, 和 行值 373 | } 374 | } 375 | ] 376 | } 377 | ``` 378 | 379 | ### topSearch 380 | 381 | ```js 382 | topSearch: [ 383 | { text: '用户名', value: 'name' }, // text: 显示在提示框的内容, value: 字段 384 | { text: '省份', value: 'address' } 385 | ] 386 | ``` 387 | 388 | ### 综合案例 389 | 390 | ```js 391 | data () { 392 | return { 393 | rightButtons: [ 394 | { 395 | text: '编辑', 396 | click: (id, index, data) => { 397 | console.log(id, index, data) 398 | } 399 | } 400 | ], 401 | topButtons: [ 402 | { 403 | text: '新增', 404 | click: (ids) => { 405 | console.log(ids) 406 | } 407 | }, 408 | { 409 | text: '启用', 410 | attrs: { 411 | type: 'success' 412 | }, 413 | click: (ids) => { 414 | console.log(ids) 415 | } 416 | }, 417 | { 418 | text: '禁用', 419 | attrs: { 420 | type: 'warning' 421 | }, 422 | click: (ids) => { 423 | console.log(ids) 424 | } 425 | } 426 | ], 427 | topTime: 'create_time', 428 | topSearch: [ 429 | { text: '用户名', value: 'name' }, 430 | { text: '省份', value: 'address' } 431 | ], 432 | columnsDesc: { 433 | id: { 434 | text: 'ID', 435 | sortable: true, 436 | width: 80 437 | }, 438 | avatar: { 439 | text: '头像', 440 | type: 'image' 441 | }, 442 | name: { 443 | text: '名字', 444 | type: 'input' 445 | }, 446 | type: { 447 | text: '类型', 448 | options: ['VIP1', 'VIP2', 'VIP3', 'SVIP'] 449 | }, 450 | address: { 451 | text: '省份' 452 | }, 453 | birthday: { 454 | text: '出生日期' 455 | }, 456 | status: { 457 | text: '状态', 458 | type: 'status', 459 | width: 100, 460 | options: [ 461 | { text: '正常', type: 'success', value: 1 }, 462 | { text: '禁用', type: 'danger', value: 0 } 463 | ] 464 | }, 465 | create_time: { 466 | text: '创建时间', 467 | type: 'datetime-text' 468 | } 469 | } 470 | } 471 | } 472 | ``` 473 | 474 | ## 相关链接 475 | 476 | - [vue-ele-editable](https://github.com/dream2023/vue-ele-editable) 477 | - [element table](https://element.eleme.cn/#/zh-CN/component/table) 478 | -------------------------------------------------------------------------------- /src/EleTable.vue: -------------------------------------------------------------------------------- 1 | 159 | 160 | 496 | --------------------------------------------------------------------------------