【发布时间】:2020-08-07 00:08:00
【问题描述】:
我正在创建一个 Table 组件并为所有逻辑使用工厂函数。在v-for 中,我为每行的每个项目创建一个单元格。
工厂
这是我在需要它的 vue 页面中导入的实际工厂。我这里只添加了相关代码。
const TableData = (data) => {
const methods = {
'getRows': () => {
const result = []
for(let i = 0, end = data.length; i < end; i++) {
result.push(TableRow(methods, i))
}
return result
}
}
return methods
}
const TableRow = (parent, rowIndex) => {
const methods = {
'getCells': () => {
const result = []
for(let colIndex = 0, end = parent.getColumnCount(); colIndex < end; colIndex++) {
result.push(TableCell(parent, rowIndex, colIndex))
}
return result
}
}
return methods
}
const TableCell = (parent, rowIndex, columnIndex) => {
let active = false
const methods = {
'hover': () => {
active = !active
},
'isActive': () => {
return active
}
}
return methods
}
组件
所以在组件下面
<template>
<div class="table-container">
<table class="table" v-if="table">
<thead>
<tr>
<th class="index-col"></ths>
<th v-for="(col, index) in columns">{{col}}</th>
</tr>
</thead>
<tbody>
<tr v-for="row in rows">
<td class="cell" v-for="cell in row.getCells()" @mouseenter="cell.hover" @mouseleave="cell.hover" :class="{active: cell.isActive()}">{{cell.getValue()}}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import { mapActions, mapGetters } from "vuex";
/* Table Data Factory */
import TableData from "~/plugins/library/table/data_new.js";
export default {
data() {
return {
table: null
};
},
methods: {
async fetch() {
/* Here I fetch data from API (fetchedData is an array) */
this.data = fetchedData
if(this.data) {
this.table = TableData(this.data)
} else {
console.error('Got no data')
}
}
},
computed: {
columns() {
return this.table.getColumns()
},
rows() {
return this.table.getRows()
}
},
mounted() {
this.fetch()
}
};
</script>
我想要发生的是,当我将一个单元格悬停在表格中时(将单元格的活动状态设置为 true),该类也会切换。
:class="{active: cell.isActive()"
类 prop 不会监视单元工厂中的更改。我明白,但我不知道如何让它反应。我已经尝试并搜索了一段时间以找到解决方案,但没有成功。
希望有人可以进一步帮助我,在此先感谢!
【问题讨论】:
标签: javascript vue.js factory v-for