需求:點擊左邊的tab頁簽,請求右側表格數據;如果返回的接口數據存在taskuser字段并不為null,那么按照這個字段去回勾數據。如果存在數據,但與后面所勾選的數據項不同,按照后面勾選的為主。
<el-tabs tab-position="left" v-model="dialogInfo.activeTab" style="height: 350px" class="demo-tabs" @tab-change="tabChange"><el-tab-pane v-for="tab in dialogInfo.tabs" :key="tab.id" :label="tab.text" :name="tab.text"><div class="tableDiv"><el-tableref="refTableV":data="tableConfig.tableData":header-cell-style="{ background: '#F5F7FA', height: '30px' }"style="width: 100%; margin: 0 auto"height="100%"align="center"row-key="empName"stripeborder@select="select"><el-table-column type="selection" width="40" fixed label="操作" /><el-table-column fixed prop="userName" label="用戶編號" width="120" align="center" /><el-table-column fixed prop="empName" label="用戶姓名" min-width="150" align="center" /><el-table-column fixed prop="expiresdate" label="過期日期" min-width="150" align="center"><template v-slot="scope"><el-date-pickerv-if="dialogInfo.tabs[dialogInfo.activeTabIndex].allot?.empName === scope.row.empName"v-model="scope.row.expiresdate"type="date"placeholder="選擇日期"size="small"@change="handleDateChange(scope.row)"value-format="YYYY-MM-DD"/><span v-else @click="changeLeader(scope.row)">{{ scope.row.expiresdate || '' }}</span></template></el-table-column><el-table-column fixed prop="leaderNames" label="直接領導" min-width="150" align="center"><template v-slot="scope"><span v-if="scope.row.leaderNames && scope.row.leaderNames !== ''" size="small" @click="leaderSearch(scope.row)">{{ scope.row.leaderNames || '選擇領導' }}</span><span v-else-if="dialogInfo.tabs[dialogInfo.activeTabIndex].allot?.empName === scope.row.empName"><el-button size="small" @click="leaderSearch(scope.row)">{{ '選擇領導' }}</el-button></span></template></el-table-column></el-table></div></el-tab-pane></el-tabs>
const refTableV = ref(null) //表格
const refTree = ref(null)
const dialogInfo = reactive({tabs: [],dialogText: '任務分配',dialogFormVisible: false,activeTab: '',searchstrId: '',list: [],treeLearder: {},str: []
})const tableConfig = reactive({allot: {},tableData: [],loading: false,page: 1,selectedData: [], // 存儲當前勾選的數據項lastSelectedRow: {},paramsData: {},activeTabIndex: 0
})
?首先openInit 這個方法是我打開彈層的第一步,tabs數據是左側的角色信息;我會給每一項的tab數據添加selectedData和allot 這個在后面取值會用到。
const openInit = (list) => {dialogInfo.str = []list.forEach((item) => dialogInfo.str.push(item.factory + ',' + item.materialscode))tableConfig.selectedData = [] // 存儲當前勾選的數據項tableConfig.lastSelectedRow = {}tableConfig.allot = {}dialogInfo.list = JSON.parse(JSON.stringify(list))dialogInfo.treeLearder = {}getPropertyList({searchstr: {taskType: 'SPAREPARTSTASKROLE'}}).then((res) => {const tabs = res.data.rows || []dialogInfo.searchstrId = ''if (tabs.length > 0) {dialogInfo.searchstrId = tabs[0].iddialogInfo.activeTab = tabs[0].texttabs.forEach((row) => {row.selectedData = []row.allot = {}})dialogInfo.tabs = tabsdialogInfo.activeTabIndex = 0dialogInfo.dialogFormVisible = truegetTable() // 獲取表格數據}})
}const getTable = () => {preTaskAssign({searchstr: {id: dialogInfo.searchstrId,eventcodes: dialogInfo.str.join(';')}}).then((res) => {const tableData = res.data.row?.teapSysUserEntityList || []const taskuser = res.data.row?.taskuser || ''refTableV.value[dialogInfo.activeTabIndex].clearSelection()const strLength = dialogInfo.str.lengthif (strLength === 1) {if (tableData && tableData.length > 0) {tableData.forEach((newRow) => {if (newRow.userName === taskuser) {dialogInfo.tabs[dialogInfo.activeTabIndex].selectedData = [newRow]dialogInfo.tabs[dialogInfo.activeTabIndex].allot = newRownextTick(() => {refTableV.value[dialogInfo.activeTabIndex].toggleRowSelection(newRow, true)})}})}}tableConfig.tableData = tableData})
}
代碼主要就是針對勾選數據 保存數據,切換tab頁簽 再次回來后數據的勾選處理?
const select = (selectedRows) => {// 獲取當前 tab 的數據const currentTab = dialogInfo.tabs[dialogInfo.activeTabIndex]refTableV.value[dialogInfo.activeTabIndex].clearSelection()// 清空上一次選中的數據currentTab.selectedData = []currentTab.allot = {}// 更新當前 tab 的 selectedData 和 allotif (selectedRows.length === 1) {currentTab.selectedData = selectedRowscurrentTab.allot = selectedRows[0] // 如果有 allot 數據,更新為第一個選中的數據nextTick(() => {refTableV.value[dialogInfo.activeTabIndex].toggleRowSelection(currentTab.allot, true)})} else {ElMessage.warning('只能選擇一條數據,請取消已勾選項后再進行選擇')}// 更新當前 tab 的 selectedData 和 allotdialogInfo.tabs[dialogInfo.activeTabIndex] = currentTab
}// 選擇領導的彈層
const tabChange = (name) => {// 查找當前 tab 索引dialogInfo.activeTab = namedialogInfo.searchstrId = dialogInfo.tabs.find((item) => item.text === name).idlet index = nulldialogInfo.tabs.some((item, ind) => {if (item.text === name) {index = indreturn true // 找到就停止遍歷}return false})dialogInfo.activeTabIndex = indexconst currentTab = dialogInfo.tabs[index]// 保存當前勾選狀態const { selectedData, allot } = currentTab// 在切換 tab 時,將勾選狀態存儲到 dialogInfo 中dialogInfo.tabs[index].selectedData = selectedData || []dialogInfo.tabs[index].allot = allot || {}// 重新加載當前 tab 的數據,保持選中狀態preTaskAssign({searchstr: {id: dialogInfo.searchstrId,eventcodes: dialogInfo.str.join(';') // 外面行勾選的數據 工廠加物料編號傳字符串}}).then((res) => {// 獲取從接口返回的表格數據const tableDataList = res.data.row?.teapSysUserEntityList || []const taskuser = res.data.row?.taskuser || ''// 更新表格數據tableConfig.tableData = tableDataListrefTableV.value[dialogInfo.activeTabIndex].clearSelection()// 使用一些邏輯優化遍歷,找到合適的行就停止let isFound = falsetableDataList.forEach((row) => {if (isFound) return // 如果已經找到匹配項,直接跳過后續行const selectedUserName = selectedData[0]?.userNameif (taskuser) {if (selectedData.length === 0) {// 1. 如果有taskuser但是selectedData為空,依照taskuser去篩選數據if (row.userName === taskuser) {dialogInfo.tabs[dialogInfo.activeTabIndex].selectedData = [row]dialogInfo.tabs[dialogInfo.activeTabIndex].allot = rownextTick(() => {refTableV.value[dialogInfo.activeTabIndex].toggleRowSelection(row, true)})isFound = true // 標記為已找到匹配項,跳出循環}} else if (selectedUserName !== taskuser) {// 2. 如果同時存在,但是匹配不上,那么按照selectedData中的數據去篩選if (row.userName === selectedUserName) {dialogInfo.tabs[dialogInfo.activeTabIndex].selectedData = [row]dialogInfo.tabs[dialogInfo.activeTabIndex].allot = rownextTick(() => {refTableV.value[dialogInfo.activeTabIndex].toggleRowSelection(row, true)})isFound = true // 標記為已找到匹配項,跳出循環}} else if (selectedUserName === taskuser) {// 3. 如果同時存在,匹配的上,按照taskuser判斷nextTick(() => {const selectedRow = dialogInfo.tabs[dialogInfo.activeTabIndex].selectedData[0]refTableV.value[dialogInfo.activeTabIndex].toggleRowSelection(selectedRow, true)})isFound = true // 標記為已找到匹配項,跳出循環}} else {// 4. taskuser不存在,按照selectedData去篩選if (row.userName === selectedUserName) {dialogInfo.tabs[dialogInfo.activeTabIndex].selectedData = [row]dialogInfo.tabs[dialogInfo.activeTabIndex].allot = rownextTick(() => {refTableV.value[dialogInfo.activeTabIndex].toggleRowSelection(row, true)})isFound = true // 標記為已找到匹配項,跳出循環}}})})
}const leaderSearch = (row) => {// 查找與勾選行對應的數據const exObj = tableConfig.tableData.find((item) => item.userId === dialogInfo.tabs[dialogInfo.activeTabIndex].allot.userId)// 檢查過期日期是否為空if (exObj.expiresdate === '' || exObj.expiresdate == null) return ElMessage.warning('請先設置時間')const str = dialogInfo.tabs[dialogInfo.activeTabIndex].id // 這個就是左邊角色的idrefTree.value.openInit(JSON.parse(JSON.stringify(row)), str)
}