vue2中通过tab切换表格el-table-column使用if判断为什么切换会出现问题
时间: 2024-01-11 22:02:54 浏览: 132
在Vue2中使用if判断切换表格el-table-column可能会出现问题是因为Vue2中使用v-if指令进行条件渲染时,如果条件为false,则会将对应的DOM元素从DOM树中移除,如果条件为true,则会将对应的DOM元素添加到DOM树中。而在el-table-column中,如果某一列被隐藏掉,则该列的宽度也会被隐藏掉,从而导致表格的列宽出现问题。
如果要实现表格列的动态切换,建议使用Vue2中的v-show指令,该指令将会将对应的DOM元素隐藏,但是不会将其从DOM树中移除,从而保证了表格列的宽度不会出现问题。
相关问题
将以下vue+element ui代码转换成纯html+element ui代码:<template> <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick"> <el-tab-pane label="User" name="first"> <el-table :data="filterTableData" style="width: 100%"> <el-table-column label="Date" prop="date" /> <el-table-column label="Name" prop="name" /> <el-table-column align="right"> <template #header> <el-input v-model="search" size="small" placeholder="Type to search" /> </template> <template #default="scope"> <el-button size="small" @click="handleEdit(scope.$index, scope.row)" >Edit</el-button > <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)" >Delete</el-button > </template> </el-table-column> </el-table> </el-tab-pane> <el-tab-pane label="Config" name="second"> <el-table :data="filterTableData" style="width: 100%"> <el-table-column label="Date" prop="date" /> <el-table-column label="Name" prop="name" /> <el-table-column align="right"> <template #header> <el-input v-model="search" size="small" placeholder="Type to search" /> </template> <template #default="scope"> <el-button size="small" @click="handleEdit(scope.$index, scope.row)" >Edit</el-button > <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)" >Delete</el-button > </template> </el-table-column> </el-table></el-tab-pane> <el-tab-pane label="Role" name="third"> <el-table :data="filterTableData" style="width: 100%"> <el-table-column label="Date" prop="date" /> <el-table-column label="Name" prop="name" /> <el-table-column align="right"> <template #header> <el-input v-model="search" size="small" placeholder="Type to search" /> </template> <template #default="scope"> <el-button size="small" @click="handleEdit(scope.$index, scope.row)" >Edit</el-button > <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)" >Delete</el-button > </template> </el-table-column> </el-table></el-tab-pane> <el-tab-pane label="Task" name="fourth"> <el-table :data="filterTableData" style="width: 100%"> <el-table-column label="Date" prop="date" /> <el-table-column label="Name" prop="name" /> <el-table-column align="right"> <template #header> <el-input v-model="search" size="small" placeholder="Type to search" /> </template> <template #default="scope"> <el-button size="small" @click="handleEdit(scope.$index, scope.row)" >Edit</el-button > <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)" >Delete</el-button > </template> </el-table-column> </el-table></el-tab-pane> </el-tabs> </template> <script lang="ts" setup> import { computed, ref } from 'vue' interface User { date: string name: string address: string } const search = ref('') const filterTableData = computed(() => tableData.filter( (data) => !search.value || data.name.toLowerCase().includes(search.value.toLowerCase()) ) ) const handleEdit = (index: number, row: User) => { console.log(index, row) } const handleDelete = (index: number, row: User) => { console.log(index, row) } const tableData: User[] = [ { date: '2016-05-03', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', }, { date: '2016-05-02', name: 'John', address: 'No. 189, Grove St, Los Angeles', }, { date: '2016-05-04', name: 'Morgan', address: 'No. 189, Grove St, Los Angeles', }, { date: '2016-05-01', name: 'Jessy', address: 'No. 189, Grove St, Los Angeles', }, ] </script> <style> .demo-tabs > .el-tabs__content { padding: 32px; color: #6b778c; font-size: 32px; font-weight: 600; } </style>
以下是转换后的纯HTML+Element UI代码:
```html
<div class="demo-tabs el-tabs">
<div class="el-tabs__header">
<div class="el-tabs__nav-wrap">
<div role="tab" aria-controls="pane-first" :aria-selected="activeName==='first' ? 'true' : 'false'" class="el-tabs__item" :class="{ 'is-active': activeName==='first' }" @click="activeName='first'">User</div>
<div role="tab" aria-controls="pane-second" :aria-selected="activeName==='second' ? 'true' : 'false'" class="el-tabs__item" :class="{ 'is-active': activeName==='second' }" @click="activeName='second'">Config</div>
<div role="tab" aria-controls="pane-third" :aria-selected="activeName==='third' ? 'true' : 'false'" class="el-tabs__item" :class="{ 'is-active': activeName==='third' }" @click="activeName='third'">Role</div>
<div role="tab" aria-controls="pane-fourth" :aria-selected="activeName==='fourth' ? 'true' : 'false'" class="el-tabs__item" :class="{ 'is-active': activeName==='fourth' }" @click="activeName='fourth'">Task</div>
</div>
</div>
<div class="el-tabs__content">
<div role="tabpanel" aria-hidden="false" id="pane-first" class="el-tab-pane" :class="{ 'is-active': activeName==='first' }">
<table class="el-table" style="width: 100%">
<thead class="el-table__header">
<tr>
<th class="el-table_1_column_1">Date</th>
<th class="el-table_1_column_2">Name</th>
<th class="el-table_1_column_3">Operation</th>
</tr>
</thead>
<tbody class="el-table__body">
<tr v-for="(item, index) in filterTableData" :key="index">
<td>{{ item.date }}</td>
<td>{{ item.name }}</td>
<td class="el-table_1_column_3">
<button class="el-button el-button--small" @click="handleEdit(index, item)">Edit</button>
<button class="el-button el-button--small el-button--danger" @click="handleDelete(index, item)">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>
<div role="tabpanel" aria-hidden="true" id="pane-second" class="el-tab-pane" :class="{ 'is-active': activeName==='second' }">
<table class="el-table" style="width: 100%">
<thead class="el-table__header">
<tr>
<th class="el-table_2_column_1">Date</th>
<th class="el-table_2_column_2">Name</th>
<th class="el-table_2_column_3">Operation</th>
</tr>
</thead>
<tbody class="el-table__body">
<tr v-for="(item, index) in filterTableData" :key="index">
<td>{{ item.date }}</td>
<td>{{ item.name }}</td>
<td class="el-table_2_column_3">
<button class="el-button el-button--small" @click="handleEdit(index, item)">Edit</button>
<button class="el-button el-button--small el-button--danger" @click="handleDelete(index, item)">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>
<div role="tabpanel" aria-hidden="true" id="pane-third" class="el-tab-pane" :class="{ 'is-active': activeName==='third' }">
<table class="el-table" style="width: 100%">
<thead class="el-table__header">
<tr>
<th class="el-table_3_column_1">Date</th>
<th class="el-table_3_column_2">Name</th>
<th class="el-table_3_column_3">Operation</th>
</tr>
</thead>
<tbody class="el-table__body">
<tr v-for="(item, index) in filterTableData" :key="index">
<td>{{ item.date }}</td>
<td>{{ item.name }}</td>
<td class="el-table_3_column_3">
<button class="el-button el-button--small" @click="handleEdit(index, item)">Edit</button>
<button class="el-button el-button--small el-button--danger" @click="handleDelete(index, item)">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>
<div role="tabpanel" aria-hidden="true" id="pane-fourth" class="el-tab-pane" :class="{ 'is-active': activeName==='fourth' }">
<table class="el-table" style="width: 100%">
<thead class="el-table__header">
<tr>
<th class="el-table_4_column_1">Date</th>
<th class="el-table_4_column_2">Name</th>
<th class="el-table_4_column_3">Operation</th>
</tr>
</thead>
<tbody class="el-table__body">
<tr v-for="(item, index) in filterTableData" :key="index">
<td>{{ item.date }}</td>
<td>{{ item.name }}</td>
<td class="el-table_4_column_3">
<button class="el-button el-button--small" @click="handleEdit(index, item)">Edit</button>
<button class="el-button el-button--small el-button--danger" @click="handleDelete(index, item)">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
```
需要注意的是,Element UI的CSS样式需要在HTML文件中引入。此外,上述代码中的`activeName`、`search`、`filterTableData`、`handleEdit`和`handleDelete`需要在JavaScript中定义和实现。
<el-tabs v-model="activeName" class="demo-tab"> <el-tab-pane label="检测到的目标" name="first" class="item"> <!-- 表格存放特征值 --> <el-table :data="feature_list" height="390" border style="width: 750px; text-align: center" v-loading="loading" element-loading-text="数据正在处理中,请耐心等待" element-loading-spinner="el-icon-loading" lazy > <el-table-column label="目标类别" width="250px"> <template slot-scope="scope"> <span>{{ scope.row[2] }}</span> </template> </el-table-column>
这段代码是一个 Vue 组件,使用了 Element UI 的 Tabs、TabPane 和 Table 组件来显示检测到的目标的特征值。
el-tabs 组件用来创建一个选项卡,可以在不同的选项卡之间切换,其中 v-model="activeName" 表示当前选中的选项卡的名称。
el-tab-pane 组件用来创建一个选项卡面板,每个面板可以包含一个 Table 组件,用来显示目标的特征值。
el-table 组件用来创建一个表格,其中 :data="feature_list" 表示表格数据来自 Vue 实例中的 feature_list 属性,height="390" 表示表格高度,border 表示表格有边框,style="width: 750px; text-align: center" 表示表格样式,v-loading="loading" 表示表格数据正在加载中,element-loading-text="数据正在处理中,请耐心等待" 表示加载时的提示文本,element-loading-spinner="el-icon-loading" 表示加载时的图标,lazy 表示表格启用懒加载。而 el-table-column 组件用来创建表格列,其中 label="目标类别" 表示列标题,width="250px" 表示列宽度,slot-scope="scope" 表示列插槽的作用域,scope.row[2] 表示当前行的第 3 列数据(下标从 0 开始),因此是目标类别的值。
阅读全文