123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <template>
- <div class="vent-table">
- <a-radio-group v-model:value="selectRowIndex" @change="setSelectedRowKeys" style="width: 100%">
- <a-table :columns="columns" :pagination="false" :data-source="dataTableSource" bordered style="margin-top: 5px" :scroll="tableScroll">
- <template #bodyCell="{ column, record }">
- <template v-if="column.dataIndex === 'isCheck'">
- <a-radio :value="record.deviceID" />
- </template>
- <a-tag v-if="column.dataIndex === 'warnFlag'" :color="record.warnFlag == 0 ? 'green' : 'red'">{{
- record.warnFlag == 0 ? '正常' : '报警'
- }}</a-tag>
- <a-tag v-if="column.dataIndex === 'netStatus'" :color="record.netStatus == 0 ? 'default' : 'green'">{{
- record.netStatus == 0 ? '断开' : '连接'
- }}</a-tag>
-
- </template>
- <template #operation="{ column, record }">
- <slot name="action" v-bind="{ column, record }"></slot>
- </template>
- </a-table>
- </a-radio-group>
- </div>
- </template>
- <script lang="ts" setup>
- //ts语法
- import { toRaw, watch, ref, onMounted, onUnmounted } from 'vue';
- import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
- const props = defineProps({
- columnsType: {
- type: String,
- required: true,
- },
- dataSource: {
- type: Array,
- required: true,
- },
- deviceType: {
- type: String,
- },
- designScope: {
- type: String,
- },
- title: {
- type: String,
- },
- scroll: {
- type: Object,
- default: () => null
- },
- isAction: {
- type: Boolean,
- default: false
- },
- isShowSelect: {
- type: Boolean,
- default: true
- }
- });
- const emits = defineEmits(['selectRow']);
- const dataTableSource = ref<any[]>([]);
- const loading = ref(true);
- const tableScroll = props.scroll.y ? ref({ y: props.scroll.y }) : ref({})
- const columns = ref<any[]>([])
- // 默认初始是第一行
- const selectRowIndex = ref(-1);
- const setSelectedRowKeys = (target) => {
- console.log(target, Object.prototype.toString.call(target));
- if (Object.prototype.toString.call(target) === '[object String]') {
- selectRowIndex.value = target;
- emits('selectRow', target);
- } else if (Object.prototype.toString.call(target) === '[object Object]') {
- const data = target.target.value;
- selectRowIndex.value = data;
- emits('selectRow', data);
- }
- };
- /** 定义table Columns */
- function setColumns(columnsType) {
- const isCheckColumn = {
- title: '',
- dataIndex: 'isCheck',
- width: 120,
- align: 'center',
- customCell: (_, index) => {
- if (index % 2 == 0) {
- return { rowSpan: 2 };
- } else {
- return { rowSpan: 0 };
- }
- },
- };
- const indexColumn = {
- title: '序号',
- dataIndex: 'key',
- width: 120,
- align: 'center',
- customCell: (_, index) => {
- if (index % 2 == 0) {
- return { rowSpan: 2 };
- } else {
- return { rowSpan: 0 };
- }
- },
- customRender: function ({ index }) {
- return index/2 + 1;
- }
- };
- const runDevice = {
- title: '运行风机',
- dataIndex: 'runDevice',
- width: 80,
- align: 'center',
- };
- columns.value = getTableHeaderColumns(columnsType);
- console.log('风机columns------------------>', columnsType)
- if (columns.value && columns.value.length < 1) {
- columns.value = getTableHeaderColumns(columnsType.split('_')[0] + '_monitor');
- }
- const strinstallpos = columns.value.find((item) => {
- return item.dataIndex === 'strinstallpos' || item.dataIndex === 'strname';
- });
- if (strinstallpos) {
- strinstallpos.customCell = (_, index) => {
- if (index % 2 == 0) {
- return { rowSpan: 2 };
- } else {
- return { rowSpan: 0 };
- }
- };
- }
- columns.value.splice(1, 0, runDevice);
- if (props.isShowSelect) {
- columns.value = [isCheckColumn, ...columns.value];
- }else{
- columns.value = [indexColumn, ...columns.value];
- }
- if(props.isAction){
- columns.value = [...columns.value, {
- title: '操作',
- dataIndex: 'operation',
- width: 120,
- slots: { customRender: 'operation' },
- }];
- }
-
-
- return columns;
- }
-
- watch(
- () => {
- return props.columnsType;
- },
- (newVal) => {
- if (!newVal) return
- setColumns(newVal)
- },
- {
- immediate: true
- }
- );
- watch(
- () => {
- return props.dataSource;
- },
- (newVal, oldVal) => {
- const list: unknown[] = [];
- newVal.forEach((item) => {
- const data: any = toRaw(item);
- const resultData1 = {};
- const resultData2 = {};
- // 将主风机、备风机的数据进行拆分
- columns.value.forEach((column) => {
- const columnKey = column.dataIndex;
- // data.f
- if(columnKey){
- if (columnKey.startsWith('Fan')) {
- const key1 = columnKey.replace('Fan', 'Fan1');
- const key2 = columnKey.replace('Fan', 'Fan2');
- resultData1[columnKey] = data[key1];
- resultData2[columnKey] = data[key2];
- } else {
- resultData1[columnKey] = resultData2[columnKey] = data[columnKey];
- }
- }
- });
- resultData1['deviceID'] = resultData2['deviceID'] = data['deviceID'];
- resultData1['runDevice'] = '主机';
- resultData2['runDevice'] = '备机';
- list.push(resultData1, resultData2);
- });
- // if (oldVal.length < 1 && selectRowIndex.value == -1) {
- // // 第一次
- // setSelectedRowKeys(list[0]['deviceID']);
- // }
- dataTableSource.value = list;
- loading.value = false;
- }
- );
-
- watch(() => props.scroll.y, (newVal) => {
- if (newVal) {
- tableScroll.value = { y: newVal }
- } else {
- tableScroll.value = {}
- }
- }
- )
- onMounted(() => {
- // 如果是https
- // 反之是websocket
- });
- onUnmounted(() => {});
- defineExpose({
- setSelectedRowKeys,
- });
- </script>
- <style scoped lang="less">
- @ventSpace: zxm;
- :deep(.@{ventSpace}-table-body) {
- height: auto !important;
- }
- :deep(.jeecg-basic-table .@{ventSpace}-table-wrapper .@{ventSpace}-table-title) {
- min-height: 0;
- }
- </style>
|