Commit 2294006e by cwq0219

组件优化

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