最近做一个换班的需求,用自己的班次跟别人的班次交换,如图显示
需求描述:
1.显示文字的格子可以点击,选中添加高亮状态,当前格子高亮时其他格子取消选中(已实现)
2.图中每一行表示一个人的班次,选中自己班次的格子和要替换班次的格子保持高亮状态(已实现)
3.仅可选择两个班次(一个是自己的,另外一个是要替换的班次)
代码如下
data: {
tableData: [
[
{
shiftName: '早1',
status: 1,
checked: false
},
{
shiftName: '早2',
status: 1,
checked: false
},
{
shiftName: '早3',
status: 2,
checked: false
},
{
shiftName: '早4',
status: 2,
checked: false
},
{
shiftName: '早5',
status: 1,
checked: false
}
],
[
{
shiftName: '晚1',
status: 2,
checked: false
},
{
shiftName: '晚2',
status: 2,
checked: false
},
{
shiftName: '晚3',
status: 1,
checked: false
},
{
shiftName: '晚4',
status: 1,
checked: false
},
{
shiftName: '晚5',
status: 1,
checked: false
}
],
[
{
shiftName: '夜1',
status: 1,
checked: false
},
{
shiftName: '夜2',
status: 2,
checked: false
},
{
shiftName: '夜3',
status: 2,
checked: false
},
{
shiftName: '夜4',
status: 2,
checked: false
},
{
shiftName: '夜5',
status: 1,
checked: false
}
]
]
},
methods: {
select(column, rowIndex, columnIndex) {
this.tableData.forEach((item, index) => {
item.forEach((item1, index1) => {
if (rowIndex === index) {
item1.checked = false
}
})
})
column.checked = !column.checked
}
}
<table>
<tr v-for="(row, rowIndex) in tableData" :key="rowIndex">
<td v-for="(column, columnIndex) in row" :key="columnIndex">
<a href="javascript:;" class="cell"
v-if="column.status === 1"
:class="{active: column.checked}"
@click="select(column, rowIndex, columnIndex)">{{column.shiftName}}</a>
</td>
</tr>
</table>
<style>
table {
width: 500px;
margin: 0 auto;
border-collapse: collapse;
}
table tr td {
text-align: center;
padding: 15px 5px;
border: 1px solid #eee;
}
table tr td .cell {
display: block;
color: #666;
text-decoration: none;
}
table tr td .cell.active {
background-color: #f00;
}
table tr td .cell:hover {
text-decoration: underline;
}
</style>
我的问题:
1.每一行的格子除了选中当前格子取消其他格子的高亮状态,当前格子选中之后也要能取消选中(类似于jquery的toggle效果),现在是当前选中的格子不能取消选中
2.限制只能选中两个格子,并且将这两个选中格子的数据存起来(要传给后端)
烦请大佬指导,感谢~