Files
pqs-9100_client/frontend/src/views/machine/testSource/components/parameterTable.vue

227 lines
6.5 KiB
Vue
Raw Normal View History

2024-11-28 14:30:49 +08:00
<template>
2024-12-05 13:51:47 +08:00
<div class='table-box' >
2024-11-28 14:30:49 +08:00
<ProTable
2024-12-05 13:51:47 +08:00
ref='proTable'
2024-12-02 08:50:21 +08:00
:columns='columns'
:pagination="false"
:toolButton="false"
:data="tableData"
:row-key="id"
2024-12-03 09:49:07 +08:00
:style="{ height: '250px',maxHeight: '400px',overflow:'hidden'}"
2024-11-28 14:30:49 +08:00
>
<!-- 表格 header 按钮 -->
<template #tableHeader='scope'>
2024-12-05 13:51:47 +08:00
<el-button :disabled="isDisable" v-auth.testSource="'add'" type='primary' :icon='CirclePlus' @click="openDialog('add')">新增
2024-12-02 08:50:21 +08:00
</el-button>
2024-12-04 19:54:56 +08:00
<el-button v-auth.testSource="'delete'" type='danger' :icon='Delete'
2024-12-05 13:51:47 +08:00
plain :disabled='isDisable || !scope.isSelected' @click='batchDelete(scope.selectedListIds)'>
2024-12-04 20:00:04 +08:00
删除
2024-11-28 14:30:49 +08:00
</el-button>
</template>
<!-- 表格操作 -->
<template #operation='scope'>
2024-12-05 13:51:47 +08:00
<!-- <el-button type="primary" link :icon='CopyDocument' @click="copyRow(scope.row)">复制</el-button> -->
<el-button :disabled="isDisable" type='primary' link :icon='EditPen' @click="openDialog('edit', scope.row)">编辑</el-button>
<el-button :disabled="isDisable" type='primary' link :icon='Delete' @click='handleDelete(scope.row.id)'>删除</el-button>
2024-11-28 14:30:49 +08:00
</template>
</ProTable>
2024-12-02 08:50:21 +08:00
2024-11-28 14:30:49 +08:00
</div>
2024-12-02 08:50:21 +08:00
<ParameterPopup @getParameter="getParameter" ref='parameterPopup' :tableData="tableData"/>
2024-11-28 14:30:49 +08:00
</template>
2024-12-02 08:50:21 +08:00
<script setup lang='tsx' name='useRole'>
import {type TestSource} from '@/api/device/interface/testSource'
import ProTable from '@/components/ProTable/index.vue'
import type {ColumnProps, ProTableInstance} from '@/components/ProTable/interface'
import {CirclePlus, CopyDocument, Delete, EditPen} from '@element-plus/icons-vue'
import {useDictStore} from '@/stores/modules/dict'
import ParameterPopup from '@/views/machine/testSource/components/parameterPopup.vue';
import {reactive, ref, watch} from 'vue'
import {generateUUID} from "@/styles";
import {defineEmits} from "vue/dist/vue";
2024-12-05 13:51:47 +08:00
import { ElMessage } from 'element-plus'
2024-12-02 08:50:21 +08:00
const parameterPopup = ref()
const dictStore = useDictStore()
// ProTable 实例
const proTable = ref<ProTableInstance>()
const tableData = ref<any[]>([])
2024-12-05 13:51:47 +08:00
const props = defineProps({
parameterStr: {
type: String,
required: true,
},
isDisable: {
type: Boolean,
default: false,
},
});
2024-11-29 16:29:26 +08:00
2024-12-02 08:50:21 +08:00
const emit = defineEmits(['change-parameter'])
let originalParameterArr=reactive<TestSource.ParameterType[]>([])
2024-11-28 14:30:49 +08:00
2024-11-29 16:29:26 +08:00
watch(() => props.parameterStr, (newData) => {
2024-11-28 14:30:49 +08:00
if (newData) {
2024-12-02 08:50:21 +08:00
getTableList()
2024-11-28 14:30:49 +08:00
}
})
2024-12-02 08:50:21 +08:00
const getTableList = () => {
if (props.parameterStr) {
originalParameterArr = JSON.parse(props.parameterStr)
tableData.value = getTreeData(originalParameterArr)
}
2024-11-28 14:30:49 +08:00
};
2024-12-02 08:50:21 +08:00
const getTreeData = (data: TestSource.ParameterType[]): TestSource.ParameterType[] => {
const result: TestSource.ParameterType[] = []// 最终返回的树形结构数据
//不能修改原数组,所以需要深拷贝
const copyData = JSON.parse(JSON.stringify(data))
const map = new Map<string, TestSource.ParameterType>();
copyData.forEach(item => {
map.set(item.id, item)
});
for (const item of copyData) {
let parent = map.get(item.pId);
if (parent) {
if (!parent.children) {
parent.children = [];
2024-11-29 16:29:26 +08:00
}
2024-12-02 08:50:21 +08:00
parent.children.push(item);
parent.children.sort((a, b) => {
if (a.sort && b.sort) {
return a.sort - b.sort
} else {
return 0
}
})
} else {
result.push(item)
result.sort((a, b) => {
if (a.sort && b.sort) {
return a.sort - b.sort
} else {
return 0
}
})
2024-11-29 16:29:26 +08:00
}
}
2024-12-02 08:50:21 +08:00
return result
}
2024-11-29 16:29:26 +08:00
2024-11-28 14:30:49 +08:00
const columns = reactive<ColumnProps<any>[]>([
2024-12-05 13:51:47 +08:00
{type: 'selection', fixed: 'left', width: 70,
selectable(row, index) {
if (props.isDisable) {
return false;
}
return true;
},
},
2024-12-02 08:50:21 +08:00
{type: 'index', fixed: 'left', width: 70, label: '序号'},
2024-11-28 14:30:49 +08:00
{
prop: 'sourceParamType',
label: '参数类型',
minWidth: 180,
},
{
prop: 'sourceParamDesc',
label: '参数描述',
minWidth: 220,
},
{
2024-12-02 08:50:21 +08:00
prop: 'sourceParamValue',
2024-11-28 14:30:49 +08:00
label: '值',
minWidth: 150,
},
2024-12-02 08:50:21 +08:00
{prop: 'operation', label: '操作', fixed: 'right', width: 250},
2024-11-28 14:30:49 +08:00
])
2024-12-02 08:50:21 +08:00
2024-11-28 14:30:49 +08:00
// 打开 drawer(新增、编辑)
2024-12-02 08:50:21 +08:00
const openDialog = (titleType: string, row: Partial<TestSource.ParameterType> = {}) => {
parameterPopup.value?.open(titleType, row)
2024-11-28 14:30:49 +08:00
}
2024-12-02 08:50:21 +08:00
// 批量删除源参数
const batchDelete = async (ids: string[]) => {
let parentIds = originalParameterArr.map(item => item.pId)
if (parentIds.some(item => ids.includes(item))) {
ElMessage.error('不能删除父节点');
return;
}
for (const id of ids) {
handleDelete(id)
}
2024-11-28 14:30:49 +08:00
proTable.value?.clearSelection()
}
2024-12-02 08:50:21 +08:00
// 删除源参数
const handleDelete = (id: string) => {
let parentIds = originalParameterArr.map(item => item.pId)
if (parentIds.includes(id)) {
ElMessage.error('不能删除父节点');
return;
}
originalParameterArr = originalParameterArr.filter(item => item.id !== id);
emit('change-parameter', originalParameterArr)
2024-11-29 16:29:26 +08:00
tableData.value = getTreeData(originalParameterArr)
2024-12-02 08:50:21 +08:00
}
// 复制源参数
const copyRow = (row) => {
delete row.children
let parentIds = originalParameterArr.map(item => item.pId)
2024-11-29 16:29:26 +08:00
2024-12-02 08:50:21 +08:00
if (parentIds.includes(row.id)) {
let newParameterId = generateUUID()
let children = originalParameterArr.filter(item => item.pId == row.id).map(item => ({
...item,
id: generateUUID(),
pId: newParameterId
}))
originalParameterArr.push({...row, id: newParameterId})
originalParameterArr.push(...children)
} else {
originalParameterArr.push({...row, id: generateUUID(), pId: row.pId})
}
emit('change-parameter', originalParameterArr)
tableData.value = getTreeData(originalParameterArr)
2024-11-28 14:30:49 +08:00
}
2024-12-02 08:50:21 +08:00
const getParameter = (data: TestSource.ParameterType) => {
2024-12-05 13:43:05 +08:00
if (originalParameterArr.some(item => item.sourceParamType == data.sourceParamType)) {
ElMessage.error({message: '参数类型已存在!'})
return;
}
2024-12-02 08:50:21 +08:00
let index = originalParameterArr.findIndex(item => item.id === data.id)
2024-12-05 13:51:47 +08:00
2024-12-02 08:50:21 +08:00
if (index === -1) {
data.id = generateUUID()
// 新增
originalParameterArr.push(data)
2024-12-05 13:43:05 +08:00
ElMessage.success({message: '新增成功!'})
2024-12-02 08:50:21 +08:00
} else {
// 编辑
originalParameterArr[index] = data
2024-12-05 13:43:05 +08:00
ElMessage.success({message: '编辑成功!'})
2024-12-02 08:50:21 +08:00
}
emit('change-parameter', originalParameterArr)
tableData.value = getTreeData(originalParameterArr)
}
//清空数据
const clearData = () => {
originalParameterArr = []
tableData.value = []
}
defineExpose({
clearData
})
</script>
2024-11-28 14:30:49 +08:00