下面是关于“vue+elementUI配置表格的列显示与隐藏”的完整攻略。
步骤一:安装必要的依赖
首先,需要安装element-ui和vue-resize组件依赖,可以使用npm安装,具体命令如下:
npm install element-ui vue-resize --save
其中,element-ui
用于构建表格组件,vue-resize
用于监听窗口变化以便及时进行表格列的自适应调整。
步骤二:初始化表格
在Vue组件中,先引入所需的依赖和表格需要的数据,然后初始化表格,示例代码如下:
<template>
<div>
<el-table
ref="table"
:data="tableData"
v-resize="resizeTable"
resizable
:column-width-draggable="true"
:default-sort="{prop: 'date', order: 'descending'}"
:highlight-current-row="true">
<el-table-column
prop="date"
label="Date"
width="180">
</el-table-column>
<el-table-column
prop="name"
label="Name"
width="180">
</el-table-column>
<el-table-column
prop="address"
label="Address">
</el-table-column>
<el-table-column
:key="'action'"
label="Action"
width="150"
align="center"
fixed="right">
<template slot-scope="scope">
<el-button type="text" size="small" @click="handleEdit(scope.row)">Edit</el-button>
<el-button type="text" size="small" @click="handleDelete(scope.row)">Delete</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
import { mapState, mapGetters } from 'vuex'
import { ResizeObserver } from 'vue-resize'
import { Table } from 'element-ui'
export default {
name: 'MyTable',
data() {
return {
tableData: [
{
date: '2016-05-02',
name: 'John Smith',
address: 'No.1518 N. Point, USA'
},
{
date: '2016-05-04',
name: 'Tony Green',
address: 'No.1688 W. Point, USA'
},
{
date: '2016-05-01',
name: 'Ann Brown',
address: 'No.9818 E. Point, USA'
},
{
date: '2016-05-03',
name: 'Gina Black',
address: 'No.2822 S. Point, USA'
}
]
}
},
methods: {
handleEdit(row) {
console.log('Edit clicked', row)
},
handleDelete(row) {
console.log('Delete clicked', row)
},
getTableColumns() {
return this.$refs.table.columns
},
resizeTable() {
const dynamicColumns = this.getTableColumns().filter(column => !column.isColumnHidden)
const fixedColumns = this.getTableColumns().filter(column => column.isColumnHidden)
this.$nextTick(() => {
dynamicColumns.forEach(({ $el }) => {
const { clientWidth } = $el
fixedColumns.forEach(({ columnKey, $el }) => {
const $fixedColumn = $el.querySelector(`.cell.${columnKey}`)
$fixedColumn.style.width = `${clientWidth}px`
})
})
})
}
},
mounted() {
this.resizeTable()
},
components: {
ResizeObserver,
ElTable: Table
}
}
</script>
上面的示例代码中,我们使用了element-ui提供的el-table
和el-table-column
组件实现一个简单的表格,并提供了编辑和删除操作按钮。此示例中表格为静态数据,后续我们将添加动态数据及列的隐藏功能。
其中重要的一点是,在el-table
组件中我们使用了v-resize
指令,并引入vue-resize
组件作为支持该指令的必要依赖。v-resize
指令会在窗口大小发生变化时自动调用resizeTable
方法,做到实时自适应表格列的宽度。
步骤三:添加列隐藏功能
接下来,我们将添加一个按钮,通过点击按钮来显示或隐藏指定列,示例代码如下:
<template>
<div>
<button type="button" @click="toggleColumns">Toggle columns visibility</button>
<el-table
ref="table"
:data="tableData"
v-resize="resizeTable"
resizable
:column-width-draggable="true"
:default-sort="{prop: 'date', order: 'descending'}"
:highlight-current-row="true">
<el-table-column
prop="date"
ref="dateColumn"
label="Date"
width="180">
</el-table-column>
<el-table-column
prop="name"
ref="nameColumn"
label="Name"
width="180">
</el-table-column>
<el-table-column
prop="address"
ref="addressColumn"
label="Address">
</el-table-column>
<el-table-column
:key="'action'"
label="Action"
width="150"
align="center"
fixed="right">
<template slot-scope="scope">
<el-button type="text" size="small" @click="handleEdit(scope.row)">Edit</el-button>
<el-button type="text" size="small" @click="handleDelete(scope.row)">Delete</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
import { mapState, mapGetters } from 'vuex'
import { ResizeObserver } from 'vue-resize'
import { Table } from 'element-ui'
export default {
name: 'MyTable',
data() {
return {
tableData: [
{
date: '2016-05-02',
name: 'John Smith',
address: 'No.1518 N. Point, USA'
},
{
date: '2016-05-04',
name: 'Tony Green',
address: 'No.1688 W. Point, USA'
},
{
date: '2016-05-01',
name: 'Ann Brown',
address: 'No.9818 E. Point, USA'
},
{
date: '2016-05-03',
name: 'Gina Black',
address: 'No.2822 S. Point, USA'
}
]
}
},
methods: {
handleEdit(row) {
console.log('Edit clicked', row)
},
handleDelete(row) {
console.log('Delete clicked', row)
},
getTableColumns() {
return this.$refs.table.columns
},
toggleColumns() {
const columnsToToggle = [this.$refs.nameColumn, this.$refs.addressColumn]
columnsToToggle.forEach(column => {
column.visible = !column.visible
column.isColumnHidden = !column.visible
})
this.resizeTable()
},
resizeTable() {
const dynamicColumns = this.getTableColumns().filter(column => !column.isColumnHidden)
const fixedColumns = this.getTableColumns().filter(column => column.isColumnHidden)
this.$nextTick(() => {
dynamicColumns.forEach(({ $el }) => {
const { clientWidth } = $el
fixedColumns.forEach(({ columnKey, $el }) => {
const $fixedColumn = $el.querySelector(`.cell.${columnKey}`)
$fixedColumn.style.width = `${clientWidth}px`
})
})
})
}
},
mounted() {
this.resizeTable()
},
components: {
ResizeObserver,
ElTable: Table
}
}
</script>
在上面的示例代码中,我们添加了一个名为toggleColumns
的方法,该方法会在点击按钮时调用。该方法会通过this.$refs
获取需要隐藏的列对象,然后将列的visible
属性取反,表现为显示或隐藏列。通过设置isColumnHidden
属性来判断当前列是否应该隐藏,并调用resizeTable
方法重新计算表格列的宽度。
示例一:支持动态数据
上述示例中我们提供了一个静态的数组tableData
,在Vue实际应用中,我们通常需要根据后台返回的数据来动态渲染表格,下面给出一个简单的示例如下:
<template>
<div>
<button type="button" @click="toggleColumns">Toggle columns visibility</button>
<el-table
ref="table"
:data="tableData"
v-resize="resizeTable"
resizable
:column-width-draggable="true"
:default-sort="{prop: 'date', order: 'descending'}"
:highlight-current-row="true">
<el-table-column
prop="id"
ref="idColumn"
label="ID"
width="100">
</el-table-column>
<el-table-column
prop="name"
ref="nameColumn"
label="Name"
width="180">
</el-table-column>
<el-table-column
prop="address"
ref="addressColumn"
label="Address">
</el-table-column>
<el-table-column
:key="'action'"
label="Action"
width="150"
align="center"
fixed="right">
<template slot-scope="scope">
<el-button type="text" size="small" @click="handleEdit(scope.row)">Edit</el-button>
<el-button type="text" size="small" @click="handleDelete(scope.row)">Delete</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
import { mapState, mapGetters } from 'vuex'
import { ResizeObserver } from 'vue-resize'
import { Table } from 'element-ui'
import api from '@/api';
export default {
name: 'MyTable',
data() {
return {
tableData: []
}
},
methods: {
handleEdit(row) {
console.log('Edit clicked', row)
},
handleDelete(row) {
console.log('Delete clicked', row)
},
getTableColumns() {
return this.$refs.table.columns
},
async fetchTableData() {
const data = await api.getTableData()
this.tableData = data
},
toggleColumns() {
const columnsToToggle = [this.$refs.nameColumn, this.$refs.addressColumn]
columnsToToggle.forEach(column => {
column.visible = !column.visible
column.isColumnHidden = !column.visible
})
this.resizeTable()
},
resizeTable() {
const dynamicColumns = this.getTableColumns().filter(column => !column.isColumnHidden)
const fixedColumns = this.getTableColumns().filter(column => column.isColumnHidden)
this.$nextTick(() => {
dynamicColumns.forEach(({ $el }) => {
const { clientWidth } = $el
fixedColumns.forEach(({ columnKey, $el }) => {
const $fixedColumn = $el.querySelector(`.cell.${columnKey}`)
$fixedColumn.style.width = `${clientWidth}px`
})
})
})
}
},
mounted() {
this.fetchTableData()
this.resizeTable()
},
components: {
ResizeObserver,
ElTable: Table
}
}
</script>
在上面这个示例中,我们引入了一个名为api
的接口模块,用于获取表格数据。在fetchTableData
方法中,我们通过await
和api.getTableData()
获取后台数据,并将其保存至tableData
中。这个过程中,需要注意的一点是,要在获取到数据之后再调用resizeTable
方法,否则可能导致错误或计算不准确。
示例二:支持多个表格
上述示例中我们只提供了单个表格的示例,如果需要支持多个表格,并对每个表格都实现列的显示与隐藏功能,可以将示例代码封装为可重用的Vue组件。下面给出一个类似的封装示例:
<!-- MyDynamicTable.vue -->
<template>
<div>
<button type="button" @click="toggleColumns">Toggle columns visibility</button>
<el-table
ref="table"
:data="tableData"
v-resize="resizeTable"
resizable
:column-width-draggable="true"
:default-sort="{prop: 'date', order: 'descending'}"
:highlight-current-row="true">
<el-table-column
prop="id"
ref="idColumn"
label="ID"
width="100">
</el-table-column>
<el-table-column
prop="name"
ref="nameColumn"
label="Name"
width="180">
</el-table-column>
<el-table-column
prop="address"
ref="addressColumn"
label="Address">
</el-table-column>
<el-table-column
:key="'action'"
label="Action"
width="150"
align="center"
fixed="right">
<template slot-scope="scope">
<el-button type="text" size="small" @click="handleEdit(scope.row)">Edit</el-button>
<el-button type="text" size="small" @click="handleDelete(scope.row)">Delete</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
import { mapState, mapGetters } from 'vuex'
import { ResizeObserver } from 'vue-resize'
import { Table } from 'element-ui'
import api from '@/api';
export default {
name: 'MyDynamicTable',
props: ['apiEndpoint'],
data() {
return {
tableData: []
}
},
methods: {
handleEdit(row) {
console.log('Edit clicked', row)
},
handleDelete(row) {
console.log('Delete clicked', row)
},
getTableColumns() {
return this.$refs.table.columns
},
async fetchTableData() {
const data = await api.getTableData(this.apiEndpoint)
this.tableData = data
},
toggleColumns() {
const columnsToToggle = [this.$refs.nameColumn, this.$refs.addressColumn]
columnsToToggle.forEach(column => {
column.visible = !column.visible
column.isColumnHidden = !column.visible
})
this.resizeTable()
},
resizeTable() {
const dynamicColumns = this.getTableColumns().filter(column => !column.isColumnHidden)
const fixedColumns = this.getTableColumns().filter(column => column.isColumnHidden)
this.$nextTick(() => {
dynamicColumns.forEach(({ $el }) => {
const { clientWidth } = $el
fixedColumns.forEach(({ columnKey, $el }) => {
const $fixedColumn = $el.querySelector(`.cell.${columnKey}`)
$fixedColumn.style.width = `${clientWidth}px`
})
})
})
}
},
mounted() {
this.fetchTableData()
this.resizeTable()
},
components: {
ResizeObserver,
ElTable: Table
}
}
</script>
以上代码中,我们将封装后的表格组件命名为MyDynamicTable
,并通过props
中的apiEndpoint
属性接收一个用于获取数据的API地址。如此封装之后,我们只需按如下方式在父组件中使用即可使用多个表格:
<!-- MyPage.vue -->
<template>
<div>
<my-dynamic-table api-endpoint="/api/table1" />
<my-dynamic-table api-endpoint="/api/table2" />
<my-dynamic-table api-endpoint="/api/table3" />
</div>
</template>
<script>
import MyDynamicTable from '@/components/MyDynamicTable'
export default {
name: 'MyPage',
components: {
MyDynamicTable
}
}
</script>
以上就是“vue+elementUI配置表格的列显示与隐藏”的完整攻略,希望能对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:vue+elementUI配置表格的列显示与隐藏 - Python技术站