Commit 2294006e by cwq0219

组件优化

1 parent 77e04395
...@@ -2836,6 +2836,15 @@ ...@@ -2836,6 +2836,15 @@
} }
} }
}, },
"glsx-table": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/glsx-table/-/glsx-table-1.0.6.tgz",
"integrity": "sha512-3EZJTCE6ZzusjkPZ5O1lUcEHeF2x+NPDpUAAGd9jp6jB6tKY1qLp33ILTYgEKuf6nsyAT7elE3B6+5cCd9nY4w==",
"requires": {
"element-ui": "^2.13.1",
"vue": "^2.6.12"
}
},
"graceful-fs": { "graceful-fs": {
"version": "4.2.6", "version": "4.2.6",
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.6.tgz", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.6.tgz",
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
"dependencies": { "dependencies": {
"axios": "^0.21.1", "axios": "^0.21.1",
"element-ui": "^2.15.1", "element-ui": "^2.15.1",
"glsx-table": "^1.0.6",
"vue": "^2.5.11" "vue": "^2.5.11"
}, },
"browserslist": [ "browserslist": [
......
<!-- 表格+分页 --> <!-- 表格+分页 -->
<template> <template>
<div class="d2-table"> <div class="glsx-table">
<el-table <el-table
:ref="tableParams.mutiSelectName + 'MutiTable' || 'mutiTable'" :ref="tableParams.mutiSelectName + 'MutiTable' || 'MutiTable'"
:data="getListApi ? data : tableData" :data="getListApi ? data : tableData"
:row-key="tableParams.mutiSelectUniqueKey || ''" :row-key="tableParams.mutiSelectUniqueKey || ''"
v-loading="loading" v-loading="loading"
border border
:highlight-current-row="true" :highlight-current-row="true"
:stripe="true"
@sort-change="sortChange" @sort-change="sortChange"
@selection-change="handleSelect" @selection-change="handleSelect"
:size="size"
> >
<el-table-column <el-table-column
v-if="tableParams.mutiSelectName && tableParams.mutiSelectUniqueKey" v-if="tableParams.mutiSelectUniqueKey && tableParams.mutiSelectName"
type="selection" type="selection"
width="55" width="55"
:reserve-selection="true" :reserve-selection="true"
...@@ -24,7 +26,7 @@ ...@@ -24,7 +26,7 @@
width="60px" width="60px"
label="序号" label="序号"
type='index' type='index'
:index="index=>index+(page.currentPage - 1) * page.pageSize + 1" :index="index=>index+(page[this.pageConf.pageNum] - 1) * page[this.pageConf.pageSize] + 1"
></el-table-column> ></el-table-column>
<el-table-column <el-table-column
v-for="(item, index) in tableParams.tableColumns" v-for="(item, index) in tableParams.tableColumns"
...@@ -45,14 +47,28 @@ ...@@ -45,14 +47,28 @@
:width="item.width" :width="item.width"
></slot> ></slot>
<template v-else-if="item.formatter"> <template v-else-if="item.formatter">
<span v-text="item.formatter(scope.row)"></span> <span
class="line-ellipsis"
:style="{'-webkit-line-clamp': item.line ? item.line : ''}"
:title="item.formatter(scope.row)"
v-text="item.formatter(scope.row)"
></span>
</template> </template>
<template v-else-if="getListApi"> <template v-else-if="getListApi">
<span v-if="item.parentProp">{{scope.row[item.parentProp][item.prop]}}</span> <span
<span v-else>{{scope.row[item.prop]}}</span> class="line-ellipsis"
:style="{'-webkit-line-clamp': item.line ? item.line : ''}"
:title="scope.row[item.prop]"
v-text="scope.row[item.prop]"
></span>
</template> </template>
<template v-else> <template v-else>
<span v-text="tableData[scope.$index][item.prop]"></span> <span
class="line-ellipsis"
:style="{'-webkit-line-clamp': item.line ? item.line : ''}"
:title="tableData[scope.$index][item.prop]"
v-text="tableData[scope.$index][item.prop]"
></span>
</template> </template>
<template v-if="item.addSymbol"> <template v-if="item.addSymbol">
<span v-text="item.addSymbol"></span> <span v-text="item.addSymbol"></span>
...@@ -62,9 +78,9 @@ ...@@ -62,9 +78,9 @@
<el-table-column <el-table-column
v-if="tableParams.tableOption" v-if="tableParams.tableOption"
label="操作" label="操作"
fixed="right"
:align="tableParams.tableOption.align||'center'" :align="tableParams.tableOption.align||'center'"
:width="tableParams.tableOption.width||'auto'" :width="tableParams.tableOption.width||'200'"
fixed="right"
> >
<template slot-scope="scope"> <template slot-scope="scope">
<slot <slot
...@@ -79,9 +95,9 @@ ...@@ -79,9 +95,9 @@
background background
@size-change="handleSizeChange" @size-change="handleSizeChange"
@current-change="handleCurrentChange" @current-change="handleCurrentChange"
:current-page="page.currentPage" :current-page="page[this.pageConf.pageNum]"
:page-sizes="[10,20,30,40]" :page-sizes="[10,20,30,40]"
:page-size="page.pageSize" :page-size="page[this.pageConf.pageSize]"
:total="getListApi ? total : tableTotal" :total="getListApi ? total : tableTotal"
:layout="layout" :layout="layout"
> >
...@@ -92,25 +108,23 @@ ...@@ -92,25 +108,23 @@
<script> <script>
export default { export default {
name: 'd2-table', name: 'd2-table',
data () { data() {
return { return {
loading: false, loading: false,
data: [], data: [],
// 页码 // 页码
page: { page: {},
pageSize: 10,
currentPage: 1
},
total: 10, total: 10,
// 排序
sortColum: '',
sort: '',
// 多选数组 // 多选数组
mutiSelectArr: {} mutiSelectArr: {}
} }
}, },
props: { props: {
size: {
type: String,
default: 'medium'
},
// 搜索条件 // 搜索条件
searchForm: { searchForm: {
type: Object, type: Object,
...@@ -144,6 +158,10 @@ export default { ...@@ -144,6 +158,10 @@ export default {
type: Boolean, type: Boolean,
default: true default: true
}, },
pageConf: {
type: Object,
default: () => ({ pageSize: 'pageSize', pageNum: 'pageNum', totalCount: 'totalCount' })
},
// 分页组件配置 // 分页组件配置
layout: { layout: {
type: String, type: String,
...@@ -168,11 +186,6 @@ export default { ...@@ -168,11 +186,6 @@ export default {
// type: String, // type: String,
// default: '' // default: ''
// }, // },
// 搜索按钮id;new Date().getTime() + parseInt(Math.random() * 1000)
// btnId: {
// type: String,
// default: ''
// },
// 后端返回列表参数名 // 后端返回列表参数名
listName: { listName: {
type: String, type: String,
...@@ -193,56 +206,44 @@ export default { ...@@ -193,56 +206,44 @@ export default {
} }
}, },
mounted () { mounted() {
const btn = document.getElementById(this.tableParams.btnId) this.page = {}
btn && btn.addEventListener('click', this.refreshEmit) this.page[this.pageConf.pageSize] = 10
this.page[this.pageConf.pageNum] = 1
this.getListApi && this.refreshTable() this.getListApi && this.refreshTable()
}, },
methods: { methods: {
// 排序 // 排序
sortChange ({ column, prop, order }) { sortChange({ column, prop, order }) {
this.sortColum = order ? prop : '' this.$emit('sortChange', { column, prop, order })
this.sort = order
this.refreshTable()
}, },
// 多选 // 多选
handleSelect (val) { handleSelect(val) {
this.mutiSelectArr[this.tableParams.mutiSelectName] = val this.mutiSelectArr[this.tableParams.mutiSelectName] = val
}, },
// 清空多选 // 清空多选
clearMutiSelect () { clearMutiSelect() {
this.mutiSelectArr[this.tableParams.mutiSelectName] = [] this.mutiSelectArr[this.tableParams.mutiSelectName] = []
this.$refs[this.tableParams.mutiSelectName + 'MutiTable'].clearSelection() this.$refs[this.tableParams.mutiSelectName + 'MutiTable'].clearSelection()
}, },
// 发布事件,以期查询条件改变后,页面图表需要刷新 refreshTable(resetPageNum = true, clearMuti = true) {
refreshEmit () {
this.$emit('searchChange')
this.refreshTable(true, true)
},
refreshTable (clearMuti = true, resetCurrentPage = false) {
this.beforeSearchFn().then(res => { this.beforeSearchFn().then(res => {
// console.log(res)
// 清空多选 // 清空多选
clearMuti && this.clearMutiSelect() clearMuti && this.clearMutiSelect()
this.loading = true this.loading = true
resetCurrentPage && (this.page.currentPage = 1) resetPageNum && (this.page[this.pageConf.pageNum] = 1)
var params = { var params = {
...this.page, ...this.page,
...this.searchForm ...this.searchForm
} }
if (this.sort) {
params.sortColum = this.sortColum
params.sort = this.sort === 'descending' ? 1 : 0
}
this.getListApi(params).then(res => { this.getListApi(params).then(res => {
this.data = res[this.tableParams.listName] this.data = this.findVal(res, this.tableParams.listName)
this.total = res.total this.total = this.findVal(res, this.pageConf.totalCount)
this.loading = false this.loading = false
}).catch(() => { }).catch(() => {
this.loading = false this.loading = false
...@@ -250,16 +251,25 @@ export default { ...@@ -250,16 +251,25 @@ export default {
}) })
}, },
// 找返回值
findVal(res, rankStr) {
var rankList = rankStr.split('.')
if (!rankStr) {
return res
}
return this.findVal(res[rankList[0]], rankList.splice(1).join('.'))
},
// 每页条数 // 每页条数
handleSizeChange (value) { handleSizeChange(value) {
this.page.pageSize = value this.page[this.pageConf.pageSize] = value
this.getListApi ? this.refreshTable(false) : this.$emit('refreshTable', this.page) this.getListApi ? this.refreshTable(false, false) : this.$emit('refreshTable', this.page)
}, },
// 页码 // 页码
handleCurrentChange (value) { handleCurrentChange(value) {
this.page.currentPage = value this.page[this.pageConf.pageNum] = value
this.getListApi ? this.refreshTable(false) : this.$emit('refreshTable', this.page) this.getListApi ? this.refreshTable(false, false) : this.$emit('refreshTable', this.page)
} }
} }
} }
...@@ -272,7 +282,15 @@ export default { ...@@ -272,7 +282,15 @@ export default {
.el-table .cell { .el-table .cell {
line-height: 14px; line-height: 14px;
} }
.d2-table p { .glsx-table p {
margin: 10px 0; margin: 10px 0;
} }
.glsx-table .line-ellipsis {
display: -webkit-box;
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
/* -webkit-line-clamp: 2; */
-webkit-box-orient: vertical;
}
</style> </style>
import Vue from 'vue' import Vue from 'vue'
import App from './App.vue' import App from './App.vue'
import ElementUI from 'element-ui' import ElementUI from 'element-ui'
import glsxTable from 'glsx-table'
import 'element-ui/lib/theme-chalk/index.css' import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI) Vue.use(ElementUI)
Vue.use(glsxTable)
new Vue({ new Vue({
el: '#app', el: '#app',
......
...@@ -10,21 +10,23 @@ ...@@ -10,21 +10,23 @@
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
<el-button <el-button
:id="tableParams.btnId"
size="small" size="small"
type="primary" type="primary"
@click="handleSearch"
>查询</el-button> >查询</el-button>
<el-button @click="handleOption"> <el-button @click="handleOption">
批量操作 批量操作
</el-button> </el-button>
</el-form-item> </el-form-item>
</el-form> </el-form>
<d2-table <glsx-table
ref="tableRef" ref="tableRef"
:searchForm="searchForm" :searchForm="searchForm"
:tableParams="tableParams" :tableParams="tableParams"
:getListApi="getListApi" :getListApi="getListApi"
:pageConf="pageConf"
:beforeSearchFn="beforeSearchFn" :beforeSearchFn="beforeSearchFn"
@sortChange="sortChange"
> >
<span <span
slot="showImg" slot="showImg"
...@@ -45,13 +47,12 @@ ...@@ -45,13 +47,12 @@
@click="handleAdd(scope.scope)" @click="handleAdd(scope.scope)"
>新增</el-button> >新增</el-button>
</span> </span>
</d2-table> </glsx-table>
</div> </div>
</template> </template>
<script> <script>
import { getListApi } from '../api/test' import { getListApi } from '../api/test'
import d2Table from '../components/d2-table'
export default { export default {
data() { data() {
return { return {
...@@ -59,9 +60,9 @@ export default { ...@@ -59,9 +60,9 @@ export default {
id: '' id: ''
}, },
getListApi: getListApi, getListApi: getListApi,
pageConf: { pageSize: 'pageSize', pageNum: 'pageNum', totalCount: 'total' },
// 表格参数 // 表格参数
tableParams: { tableParams: {
btnId: new Date().getTime() + parseInt(Math.random() * 1000),
listName: 'list', listName: 'list',
// 行数据的key // 行数据的key
mutiSelectUniqueKey: 'id', mutiSelectUniqueKey: 'id',
...@@ -75,6 +76,7 @@ export default { ...@@ -75,6 +76,7 @@ export default {
}, { }, {
label: '标题', label: '标题',
prop: 'title', prop: 'title',
line: 1,
sortable: true sortable: true
}, { }, {
label: '状态', label: '状态',
...@@ -95,10 +97,6 @@ export default { ...@@ -95,10 +97,6 @@ export default {
} }
}, },
components: {
d2Table
},
methods: { methods: {
// 刷新表格前的回调函数 // 刷新表格前的回调函数
beforeSearchFn() { beforeSearchFn() {
...@@ -107,14 +105,23 @@ export default { ...@@ -107,14 +105,23 @@ export default {
resolve() resolve()
}) })
}, },
// 刷新表格
handleSearch() {
this.$refs.tableRef.refreshTable()
},
sortChange({ column, prop, order }) {
// 添加-排序相关参数
this.searchForm.order = order
this.searchForm.sortAttr = order ? prop : null
this.handleSearch()
},
// 新增 // 新增
handleAdd(row) { handleAdd(row) {
this.$notify({ this.$notify({
title: '提示', title: '提示',
message: '点击了id为' + row.id + '的数据' message: '点击了id为' + row.id + '的数据'
}) })
// 刷新表格 this.handleSearch()
this.$refs.tableRef.refrshTable()
}, },
// 获取批量数据 // 获取批量数据
handleOption() { handleOption() {
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
>查询</el-button> >查询</el-button>
</el-form-item> </el-form-item>
</el-form> </el-form>
<d2-table <glsx-table
ref="tableRef2" ref="tableRef2"
:tableData="tableData" :tableData="tableData"
:tableTotal="tableTotal" :tableTotal="tableTotal"
...@@ -33,13 +33,12 @@ ...@@ -33,13 +33,12 @@
@click="handleAdd(scope.scope)" @click="handleAdd(scope.scope)"
>新增</el-button> >新增</el-button>
</span> </span>
</d2-table> </glsx-table>
</div> </div>
</template> </template>
<script> <script>
import { getListApi2 } from '../api/test' import { getListApi2 } from '../api/test'
import d2Table from '../components/d2-table'
export default { export default {
name: 'manualTable', name: 'manualTable',
data() { data() {
...@@ -49,6 +48,7 @@ export default { ...@@ -49,6 +48,7 @@ export default {
}, },
// 表格参数 // 表格参数
tableData: [], tableData: [],
tableTotal: 0,
tableParams: { tableParams: {
listName: 'list', listName: 'list',
tableOption: true, tableOption: true,
...@@ -64,15 +64,17 @@ export default { ...@@ -64,15 +64,17 @@ export default {
} }
}, },
components: {
d2Table
},
mounted() { mounted() {
this.handleSearch() this.handleSearch()
}, },
methods: { methods: {
handleAdd(row) {
this.$notify({
title: '提示',
message: row
})
},
handleSearch() { handleSearch() {
var params = { var params = {
...JSON.parse(JSON.stringify(this.searchForm)), ...JSON.parse(JSON.stringify(this.searchForm)),
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!