GroupMonitorTable.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <div class="vent-table">
  3. <a-radio-group v-model:value="selectRowIndex" @change="setSelectedRowKeys" style="width: 100%">
  4. <a-table :columns="columns" :pagination="false" :data-source="dataTableSource" bordered style="margin-top: 5px" :scroll="tableScroll">
  5. <template #bodyCell="{ column, record }">
  6. <template v-if="column.dataIndex === 'isCheck'">
  7. <a-radio :value="record.deviceID" />
  8. </template>
  9. <a-tag v-if="column.dataIndex === 'warnFlag'" :color="record.warnFlag == 0 ? 'green' : 'red'">{{
  10. record.warnFlag == 0 ? '正常' : '报警'
  11. }}</a-tag>
  12. <a-tag v-if="column.dataIndex === 'netStatus'" :color="record.netStatus == 0 ? 'default' : 'green'">{{
  13. record.netStatus == 0 ? '断开' : '连接'
  14. }}</a-tag>
  15. </template>
  16. </a-table>
  17. </a-radio-group>
  18. </div>
  19. </template>
  20. <script lang="ts" setup>
  21. //ts语法
  22. import { toRaw, watch, ref, onMounted, onUnmounted } from 'vue';
  23. import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
  24. const props = defineProps({
  25. columnsType: {
  26. type: String,
  27. required: true,
  28. },
  29. dataSource: {
  30. type: Array,
  31. required: true,
  32. },
  33. deviceType: {
  34. type: String,
  35. },
  36. designScope: {
  37. type: String,
  38. },
  39. title: {
  40. type: String,
  41. },
  42. scroll: {
  43. type: Object,
  44. default: () => null
  45. },
  46. });
  47. const emits = defineEmits(['selectRow']);
  48. const dataTableSource = ref<any[]>([]);
  49. const loading = ref(true);
  50. const tableScroll = props.scroll.y ? ref({ y: props.scroll.y }) : ref({})
  51. // 默认初始是第一行
  52. const selectRowIndex = ref(-1);
  53. const setSelectedRowKeys = (target) => {
  54. console.log(target, Object.prototype.toString.call(target));
  55. if (Object.prototype.toString.call(target) === '[object String]') {
  56. selectRowIndex.value = target;
  57. emits('selectRow', target);
  58. } else if (Object.prototype.toString.call(target) === '[object Object]') {
  59. const data = target.target.value;
  60. selectRowIndex.value = data;
  61. emits('selectRow', data);
  62. }
  63. };
  64. /** 定义table Columns */
  65. function setColumns() {
  66. const isCheckColumn = {
  67. title: '',
  68. dataIndex: 'isCheck',
  69. width: 120,
  70. align: 'center',
  71. customCell: (_, index) => {
  72. if (index % 2 == 0) {
  73. return { rowSpan: 2 };
  74. } else {
  75. return { rowSpan: 0 };
  76. }
  77. },
  78. };
  79. const runDevice = {
  80. title: '运行风机',
  81. dataIndex: 'runDevice',
  82. width: 80,
  83. align: 'center',
  84. };
  85. let columns: any[] = getTableHeaderColumns(props.columnsType);
  86. const strinstallpos = columns.find((item) => {
  87. return item.dataIndex === 'strinstallpos' || item.dataIndex === 'strname';
  88. });
  89. if (strinstallpos) {
  90. strinstallpos.customCell = (_, index) => {
  91. if (index % 2 == 0) {
  92. return { rowSpan: 2 };
  93. } else {
  94. return { rowSpan: 0 };
  95. }
  96. };
  97. }
  98. columns.splice(1, 0, runDevice);
  99. columns = [isCheckColumn, ...columns];
  100. return columns;
  101. }
  102. const columns = setColumns();
  103. console.log('[ columns ] >', columns);
  104. watch(
  105. () => {
  106. return props.dataSource;
  107. },
  108. (newVal, oldVal) => {
  109. const list: unknown[] = [];
  110. newVal.forEach((item) => {
  111. const data: any = toRaw(item);
  112. const resultData1 = {};
  113. const resultData2 = {};
  114. // 将主风机、备风机的数据进行拆分
  115. columns.forEach((column) => {
  116. const columnKey = column.dataIndex;
  117. // data.f
  118. if (columnKey.startsWith('Fan')) {
  119. const key1 = columnKey.replace('Fan', 'Fan1');
  120. const key2 = columnKey.replace('Fan', 'Fan2');
  121. resultData1[columnKey] = data[key1];
  122. resultData2[columnKey] = data[key2];
  123. } else {
  124. resultData1[columnKey] = resultData2[columnKey] = data[columnKey];
  125. }
  126. });
  127. resultData1['deviceID'] = resultData2['deviceID'] = data['deviceID'];
  128. resultData1['runDevice'] = '主机';
  129. resultData2['runDevice'] = '备机';
  130. list.push(resultData1, resultData2);
  131. });
  132. // if (oldVal.length < 1 && selectRowIndex.value == -1) {
  133. // // 第一次
  134. // setSelectedRowKeys(list[0]['deviceID']);
  135. // }
  136. dataTableSource.value = list;
  137. loading.value = false;
  138. }
  139. );
  140. watch(() => props.scroll.y, (newVal) => {
  141. if (newVal) {
  142. tableScroll.value = { y: newVal }
  143. } else {
  144. tableScroll.value = {}
  145. }
  146. }
  147. )
  148. onMounted(() => {
  149. // 如果是https
  150. // 反之是websocket
  151. });
  152. onUnmounted(() => {});
  153. defineExpose({
  154. setSelectedRowKeys,
  155. });
  156. </script>
  157. <style scoped lang="less">
  158. @ventSpace: zxm;
  159. :deep(.@{ventSpace}-table-body) {
  160. height: auto !important;
  161. }
  162. :deep(.jeecg-basic-table .@{ventSpace}-table-wrapper .@{ventSpace}-table-title) {
  163. min-height: 0;
  164. }
  165. </style>