Commit 77e04395 by cwq0219

提交

0 parents
{
"presets": [
["env", { "modules": false }],
"stage-3"
]
}
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
.DS_Store
node_modules/
dist/
npm-debug.log
yarn-error.log
# Editor directories and files
.idea
*.suo
*.ntvs*
*.njsproj
*.sln
# d2-table-demo
> A Vue.js project
## Build Setup
``` bash
# install dependencies
npm install
# serve with hot reload at localhost:8080
npm run dev
# build for production with minification
npm run build
```
For detailed explanation on how things work, consult the [docs for vue-loader](http://vuejs.github.io/vue-loader).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>d2-table-demo</title>
</head>
<body>
<div id="app"></div>
<script src="/dist/build.js"></script>
</body>
</html>
This diff could not be displayed because it is too large.
{
"name": "d2-table-demo",
"description": "A Vue.js project",
"version": "1.0.0",
"author": "cwq0219 <cwqnn2015@163.com>",
"license": "MIT",
"private": true,
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"dependencies": {
"axios": "^0.21.1",
"element-ui": "^2.15.1",
"vue": "^2.5.11"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
],
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"file-loader": "^1.1.4",
"vue-loader": "^13.0.5",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1"
}
}
<template>
<div id="app">
<el-tabs v-model="activeName">
<el-tab-pane
label="传入接口(getListApi)"
name="autoTabel"
>
<h1>自动调用接口,并刷新表格</h1>
<auto-table></auto-table>
</el-tab-pane>
<el-tab-pane
label="不传入接口(getListApi)"
name="table"
>
<h1>手动调用接口,并刷新表格</h1>
<manual-table></manual-table>
</el-tab-pane>
</el-tabs>
</div>
</template>
<script>
import autoTable from './view/autoTable'
import manualTable from './view/manualTable'
export default {
name: 'app',
data() {
return {
activeName: 'autoTabel'
}
},
components: {
autoTable,
manualTable
}
}
</script>
<style>
#app {
font-family: "Avenir", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
/* margin-top: 60px; */
}
h1,
h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
import axios from 'axios'
function getListApi(params) {
return new Promise(resolve => {
axios.get('https://oss-config.test.glsx.com.cn/mock/606e66698f245f5d1a51566a/tempDemo/autoTableList', {
params: params
}).then(res => {
resolve(res.data.data)
})
})
}
function getListApi2(params) {
return new Promise(resolve => {
axios.get('https://oss-config.test.glsx.com.cn/mock/606e66698f245f5d1a51566a/tempDemo/tableList', {
params: params
}).then(res => {
resolve(res.data.data)
})
})
}
export {
getListApi,
getListApi2
}
<!-- 表格+分页 -->
<template>
<div class="d2-table">
<el-table
:ref="tableParams.mutiSelectName + 'MutiTable' || 'mutiTable'"
:data="getListApi ? data : tableData"
:row-key="tableParams.mutiSelectUniqueKey || ''"
v-loading="loading"
border
:highlight-current-row="true"
@sort-change="sortChange"
@selection-change="handleSelect"
>
<el-table-column
v-if="tableParams.mutiSelectName && tableParams.mutiSelectUniqueKey"
type="selection"
width="55"
:reserve-selection="true"
>
</el-table-column>
<el-table-column
v-if="hasNum"
align="center"
width="60px"
label="序号"
type='index'
:index="index=>index+(page.currentPage - 1) * page.pageSize + 1"
></el-table-column>
<el-table-column
v-for="(item, index) in tableParams.tableColumns"
:key="index"
:label="item.label"
:prop="item.prop"
:width="item.width"
:min-width="item.minWidth"
:align="item.align||'center'"
:sortable="item.sortable"
>
<template slot-scope="scope">
<slot
v-if="item.addHtml"
:name="item.addHtml"
:scope="scope.row"
:prop="item.prop"
:width="item.width"
></slot>
<template v-else-if="item.formatter">
<span v-text="item.formatter(scope.row)"></span>
</template>
<template v-else-if="getListApi">
<span v-if="item.parentProp">{{scope.row[item.parentProp][item.prop]}}</span>
<span v-else>{{scope.row[item.prop]}}</span>
</template>
<template v-else>
<span v-text="tableData[scope.$index][item.prop]"></span>
</template>
<template v-if="item.addSymbol">
<span v-text="item.addSymbol"></span>
</template>
</template>
</el-table-column>
<el-table-column
v-if="tableParams.tableOption"
label="操作"
fixed="right"
:align="tableParams.tableOption.align||'center'"
:width="tableParams.tableOption.width||'auto'"
>
<template slot-scope="scope">
<slot
name="auth-button"
:scope="scope.row"
></slot>
</template>
</el-table-column>
</el-table>
<el-pagination
v-if="hasPage"
background
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="page.currentPage"
:page-sizes="[10,20,30,40]"
:page-size="page.pageSize"
:total="getListApi ? total : tableTotal"
:layout="layout"
>
</el-pagination>
</div>
</template>
<script>
export default {
name: 'd2-table',
data () {
return {
loading: false,
data: [],
// 页码
page: {
pageSize: 10,
currentPage: 1
},
total: 10,
// 排序
sortColum: '',
sort: '',
// 多选数组
mutiSelectArr: {}
}
},
props: {
// 搜索条件
searchForm: {
type: Object,
default: () => { }
},
// 表格数据-外部传入
tableData: {
type: Array,
default: () => []
},
tableTotal: {
type: Number,
default: 0
},
// 获取列表接口
getListApi: {
type: Function,
default: null
},
// 搜索前操作
beforeSearchFn: {
type: Function,
default: () => {
return new Promise((resolve, reject) => {
resolve()
})
}
},
// 分页,默认有
hasPage: {
type: Boolean,
default: true
},
// 分页组件配置
layout: {
type: String,
default: 'sizes,total,->,prev,pager,next,jumper'
},
// 序号,默认有
hasNum: {
type: Boolean,
default: true
},
tableParams: {
type: Object,
default: () => {
return {
// 多选-判断来自哪个页面
// mutiSelectName: {
// type: String,
// default: ''
// },
// 多选-唯一标识-行数据的key
// mutiSelectUniqueKey: {
// type: String,
// default: ''
// },
// 搜索按钮id;new Date().getTime() + parseInt(Math.random() * 1000)
// btnId: {
// type: String,
// default: ''
// },
// 后端返回列表参数名
listName: {
type: String,
default: ''
},
// 表格操作,默认没有
// tableOption: {
// type: Object,
// default: null
// },
// 表格栏
tableColumns: {
type: Array,
default: () => []
}
}
}
}
},
mounted () {
const btn = document.getElementById(this.tableParams.btnId)
btn && btn.addEventListener('click', this.refreshEmit)
this.getListApi && this.refreshTable()
},
methods: {
// 排序
sortChange ({ column, prop, order }) {
this.sortColum = order ? prop : ''
this.sort = order
this.refreshTable()
},
// 多选
handleSelect (val) {
this.mutiSelectArr[this.tableParams.mutiSelectName] = val
},
// 清空多选
clearMutiSelect () {
this.mutiSelectArr[this.tableParams.mutiSelectName] = []
this.$refs[this.tableParams.mutiSelectName + 'MutiTable'].clearSelection()
},
// 发布事件,以期查询条件改变后,页面图表需要刷新
refreshEmit () {
this.$emit('searchChange')
this.refreshTable(true, true)
},
refreshTable (clearMuti = true, resetCurrentPage = false) {
this.beforeSearchFn().then(res => {
// console.log(res)
// 清空多选
clearMuti && this.clearMutiSelect()
this.loading = true
resetCurrentPage && (this.page.currentPage = 1)
var params = {
...this.page,
...this.searchForm
}
if (this.sort) {
params.sortColum = this.sortColum
params.sort = this.sort === 'descending' ? 1 : 0
}
this.getListApi(params).then(res => {
this.data = res[this.tableParams.listName]
this.total = res.total
this.loading = false
}).catch(() => {
this.loading = false
})
})
},
// 每页条数
handleSizeChange (value) {
this.page.pageSize = value
this.getListApi ? this.refreshTable(false) : this.$emit('refreshTable', this.page)
},
// 页码
handleCurrentChange (value) {
this.page.currentPage = value
this.getListApi ? this.refreshTable(false) : this.$emit('refreshTable', this.page)
}
}
}
</script>
<style>
.a-click {
cursor: pointer;
}
.el-table .cell {
line-height: 14px;
}
.d2-table p {
margin: 10px 0;
}
</style>
import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
new Vue({
el: '#app',
render: h => h(App)
})
<!-- 传入接口名称,自动调用接口,并更新表格数据 -->
<template>
<div>
<el-form
:inline="true"
:model="searchForm"
>
<el-form-item label="id:">
<el-input v-model="searchForm.id"></el-input>
</el-form-item>
<el-form-item>
<el-button
:id="tableParams.btnId"
size="small"
type="primary"
>查询</el-button>
<el-button @click="handleOption">
批量操作
</el-button>
</el-form-item>
</el-form>
<d2-table
ref="tableRef"
:searchForm="searchForm"
:tableParams="tableParams"
:getListApi="getListApi"
:beforeSearchFn="beforeSearchFn"
>
<span
slot="showImg"
slot-scope="scope"
>
<img
style="width: 100px; height: 100px;"
:src="scope.scope.coverPicture"
>
</span>
<span
slot="auth-button"
slot-scope="scope"
:data="scope.scope"
>
<el-button
type="text"
@click="handleAdd(scope.scope)"
>新增</el-button>
</span>
</d2-table>
</div>
</template>
<script>
import { getListApi } from '../api/test'
import d2Table from '../components/d2-table'
export default {
data() {
return {
searchForm: {
id: ''
},
getListApi: getListApi,
// 表格参数
tableParams: {
btnId: new Date().getTime() + parseInt(Math.random() * 1000),
listName: 'list',
// 行数据的key
mutiSelectUniqueKey: 'id',
// 多选ref的名称
mutiSelectName: 'list',
tableOption: true,
tableColumns: [{
label: 'id',
prop: 'id',
width: '80px'
}, {
label: '标题',
prop: 'title',
sortable: true
}, {
label: '状态',
prop: 'status',
formatter: cellValue => {
return {
0: '待审核',
1: '通过',
2: '失败'
}[cellValue.status]
}
}, {
label: '图片',
prop: 'coverPicture',
addHtml: 'showImg'
}]
}
}
},
components: {
d2Table
},
methods: {
// 刷新表格前的回调函数
beforeSearchFn() {
return new Promise(resolve => {
this.$message('点击查询,可在此格式化搜索项数据')
resolve()
})
},
// 新增
handleAdd(row) {
this.$notify({
title: '提示',
message: '点击了id为' + row.id + '的数据'
})
// 刷新表格
this.$refs.tableRef.refrshTable()
},
// 获取批量数据
handleOption() {
this.$notify({
title: '提示',
message: '当前选中的数据的id有' + this.$refs.tableRef.mutiSelectArr[this.tableParams.mutiSelectName].map(item => item.id).join(',')
})
}
}
}
</script>
<style scoped>
</style>
\ No newline at end of file \ No newline at end of file
<!-- 不传入接口名称,手动调用接口,并更新表格数据 -->
<template>
<div>
<el-form
:inline="true"
:model="searchForm"
>
<el-form-item label="name:">
<el-input v-model="searchForm.name"></el-input>
</el-form-item>
<el-form-item>
<el-button
size="small"
type="primary"
@click="handleSearch"
>查询</el-button>
</el-form-item>
</el-form>
<d2-table
ref="tableRef2"
:tableData="tableData"
:tableTotal="tableTotal"
:tableParams="tableParams"
@refreshTable="handleSearch"
>
<span
slot="auth-button"
slot-scope="scope"
:data="scope.scope"
>
<el-button
type="text"
@click="handleAdd(scope.scope)"
>新增</el-button>
</span>
</d2-table>
</div>
</template>
<script>
import { getListApi2 } from '../api/test'
import d2Table from '../components/d2-table'
export default {
name: 'manualTable',
data() {
return {
searchForm: {
name: ''
},
// 表格参数
tableData: [],
tableParams: {
listName: 'list',
tableOption: true,
tableColumns: [{
label: 'id',
prop: 'id',
width: '80px'
}, {
label: '姓名',
prop: 'name'
}]
}
}
},
components: {
d2Table
},
mounted() {
this.handleSearch()
},
methods: {
handleSearch() {
var params = {
...JSON.parse(JSON.stringify(this.searchForm)),
pageSize: this.$refs.tableRef2.page.pageSize,
currentPage: this.$refs.tableRef2.page.currentPage
}
getListApi2(params).then(res => {
this.tableData = res.list
this.tableTotal= res.total
})
}
}
}
</script>
<style scoped>
</style>
\ No newline at end of file \ No newline at end of file
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
},
{
test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
loader: 'file-loader'
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!