diff --git a/src/components/TableModel2/index.vue b/src/components/TableModel2/index.vue index c4be7ba..fb41d49 100644 --- a/src/components/TableModel2/index.vue +++ b/src/components/TableModel2/index.vue @@ -78,7 +78,11 @@ v-if="selectionShow && isSelectShow" /> @@ -194,6 +198,11 @@ export default { type: Boolean, default: false, }, + // 单选框唯一标识的字段名(如 'id', 'proId', 'bidId' 等) + radioKey: { + type: String, + default: 'id', + }, // 测试时使用的数据源 testTableList: { type: Array, @@ -429,10 +438,17 @@ export default { this.total = res.total // 数据刷新后,如果之前选中的行不在当前列表中,清空选中状态 if (this.isRadioShow && this.currentRowId) { - const exists = this.tableList.some( - (row) => - this.getRowUniqueId(row) === this.currentRowId, - ) + let exists = false + for (let i = 0; i < this.tableList.length; i++) { + const row = this.tableList[i] + const uniqueId = this.getRowUniqueId(row, i) + if (uniqueId === this.currentRowId) { + exists = true + // 更新当前行数据引用 + this.currentRowData = row + break + } + } if (!exists) { this.currentRowId = null this.currentRowData = null @@ -546,16 +562,62 @@ export default { this.selectedData = e }, - // 获取行的唯一标识(优先使用id,否则使用索引) - getRowUniqueId(row) { - return row.id || row.proId || row.bidId || JSON.stringify(row) + // 获取行的唯一标识(优先使用 radioKey 指定的字段) + getRowUniqueId(row, index) { + // 优先使用 radioKey 指定的字段作为唯一标识 + if (this.radioKey && row[this.radioKey] !== undefined && row[this.radioKey] !== null && row[this.radioKey] !== '') { + return String(row[this.radioKey]) + } + // 如果 radioKey 指定的字段不存在,尝试使用常见的ID字段 + if (row.id !== undefined && row.id !== null && row.id !== '') { + return String(row.id) + } + if (row.proId !== undefined && row.proId !== null && row.proId !== '') { + return String(row.proId) + } + if (row.bidId !== undefined && row.bidId !== null && row.bidId !== '') { + return String(row.bidId) + } + // 如果都没有,使用索引作为后备(确保唯一性) + // 注意:如果数据没有唯一ID,使用索引可能导致分页切换时选中状态丢失 + const rowIndex = index !== undefined ? index : this.tableList.findIndex(r => r === row) + // 使用行的内容生成一个稳定的哈希值作为唯一标识 + try { + const rowStr = JSON.stringify(row) + // 简单的哈希函数,确保相同内容返回相同值 + let hash = 0 + for (let i = 0; i < rowStr.length; i++) { + const char = rowStr.charCodeAt(i) + hash = ((hash << 5) - hash) + char + hash = hash & hash // Convert to 32bit integer + } + return `hash_${Math.abs(hash)}_${rowIndex}` + } catch (e) { + // 如果序列化失败,使用索引 + return `index_${rowIndex}` + } }, + // 处理单选框变化事件 + handleRadioChange(row, index) { + if (this.isRadioShow && row) { + const uniqueId = this.getRowUniqueId(row, index) + // 只有当值真正改变时才更新,避免重复触发 + if (this.currentRowId !== uniqueId) { + this.currentRowId = uniqueId + this.currentRowData = row + // 注意:这里不直接 emit,让 watch 中的监听器来处理,避免重复触发 + } + } + }, // 处理表格当前行变化(用于高亮) handleCurrentChange(currentRow) { if (this.isRadioShow && currentRow) { - this.currentRowId = this.getRowUniqueId(currentRow) + const index = this.tableList.findIndex(r => r === currentRow) + const uniqueId = this.getRowUniqueId(currentRow, index) + this.currentRowId = uniqueId + this.currentRowData = currentRow } }, @@ -613,12 +675,21 @@ export default { // 监听单选框选中值的变化 currentRowId(newVal, oldVal) { if (this.isRadioShow && newVal && newVal !== oldVal) { - // 找到对应的行数据 - const row = this.tableList.find(r => this.getRowUniqueId(r) === newVal) - if (row) { - const index = this.tableList.findIndex(r => this.getRowUniqueId(r) === newVal) - this.currentRowData = row - this.$emit('radio-change', row, index) + // 找到对应的行数据和索引 + let foundRow = null + let foundIndex = -1 + for (let i = 0; i < this.tableList.length; i++) { + const row = this.tableList[i] + const uniqueId = this.getRowUniqueId(row, i) + if (uniqueId === newVal) { + foundRow = row + foundIndex = i + break + } + } + if (foundRow) { + this.currentRowData = foundRow + this.$emit('radio-change', foundRow, foundIndex) } } }, diff --git a/src/views/analysis/components/AnalysisBidDetail.vue b/src/views/analysis/components/AnalysisBidDetail.vue index feafdec..e5b4752 100644 --- a/src/views/analysis/components/AnalysisBidDetail.vue +++ b/src/views/analysis/components/AnalysisBidDetail.vue @@ -78,7 +78,7 @@ + @radio-change="handleRadioChange" :indexNumShow="false" :isShowtableCardStyle="false" :radioKey="radioKey">