GroupMonitorTable.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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" ref="tableRef" bordered style="margin-top: 5px" :scroll="tableScroll" :selectType="'radio'" :customRow="rowClick">
  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. <template #operation="{ column, record }">
  17. <slot name="action" v-bind="{ column, record }"></slot>
  18. </template>
  19. </a-table>
  20. </a-radio-group>
  21. </div>
  22. </template>
  23. <script lang="ts" setup>
  24. //ts语法
  25. import { toRaw, watch, ref, onMounted, onUnmounted, nextTick } from 'vue';
  26. import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
  27. const props = defineProps({
  28. columnsType: {
  29. type: String,
  30. required: true,
  31. },
  32. dataSource: {
  33. type: Array,
  34. required: true,
  35. },
  36. deviceType: {
  37. type: String,
  38. },
  39. designScope: {
  40. type: String,
  41. },
  42. title: {
  43. type: String,
  44. },
  45. scroll: {
  46. type: Object,
  47. default: () => null
  48. },
  49. isAction: {
  50. type: Boolean,
  51. default: false
  52. },
  53. isShowSelect: {
  54. type: Boolean,
  55. default: true
  56. }
  57. });
  58. const tableRef = ref()
  59. const emits = defineEmits(['selectRow']);
  60. const dataTableSource = ref<any[]>([]);
  61. const loading = ref(true);
  62. const tableScroll = props.scroll.y ? ref({ y: props.scroll.y, x: 'max-content' }) : ref({})
  63. let scrollY = 0
  64. const columns = ref<any[]>([])
  65. // 默认初始是第一行
  66. const selectRowIndex = ref(-1);
  67. const headElHeight = ref(0)
  68. const rowClick = (record) => {
  69. return {
  70. onClick: () => {
  71. setSelectedRowKeys(record['deviceID'])
  72. },
  73. };
  74. };
  75. const setSelectedRowKeys = (target) => {
  76. if (Object.prototype.toString.call(target) === '[object String]') {
  77. selectRowIndex.value = target;
  78. emits('selectRow', target);
  79. } else if (Object.prototype.toString.call(target) === '[object Object]') {
  80. const data = target.target.value;
  81. selectRowIndex.value = data;
  82. emits('selectRow', data);
  83. }
  84. };
  85. /** 定义table Columns */
  86. function setColumns(columnsType) {
  87. const isCheckColumn = {
  88. title: '',
  89. dataIndex: 'isCheck',
  90. width: 40,
  91. align: 'center',
  92. customCell: (_, index) => {
  93. if (index % 2 == 0) {
  94. return { rowSpan: 2 };
  95. } else {
  96. return { rowSpan: 0 };
  97. }
  98. },
  99. };
  100. const indexColumn = {
  101. title: '序号',
  102. dataIndex: 'key',
  103. width: 120,
  104. align: 'center',
  105. customCell: (_, index) => {
  106. if (index % 2 == 0) {
  107. return { rowSpan: 2 };
  108. } else {
  109. return { rowSpan: 0 };
  110. }
  111. },
  112. customRender: function ({ index }) {
  113. return index/2 + 1;
  114. }
  115. };
  116. const runDevice = {
  117. title: '运行风机',
  118. dataIndex: 'runDevice',
  119. width: 80,
  120. align: 'center',
  121. };
  122. columns.value = getTableHeaderColumns(columnsType);
  123. console.log('风机columns------------------>', columnsType)
  124. if (columns.value && columns.value.length < 1) {
  125. columns.value = getTableHeaderColumns(columnsType.split('_')[0] + '_monitor');
  126. }
  127. const strinstallpos = columns.value.find((item) => {
  128. return item.dataIndex === 'strinstallpos' || item.dataIndex === 'strname';
  129. });
  130. if (strinstallpos) {
  131. strinstallpos.customCell = (_, index) => {
  132. if (index % 2 == 0) {
  133. return { rowSpan: 2 };
  134. } else {
  135. return { rowSpan: 0 };
  136. }
  137. };
  138. }
  139. columns.value.forEach(item => {
  140. if(item.dataIndex === 'strinstallpos' || item.dataIndex === 'strname' || item.dataIndex.endsWith('_merge')){
  141. item.customCell = (_, index) => {
  142. if (index % 2 == 0) {
  143. return { rowSpan: 2 };
  144. } else {
  145. return { rowSpan: 0 };
  146. }
  147. };
  148. }
  149. })
  150. columns.value.splice(1, 0, runDevice);
  151. if (props.isShowSelect) {
  152. columns.value = [isCheckColumn, ...columns.value];
  153. }else{
  154. columns.value = [indexColumn, ...columns.value];
  155. }
  156. if(props.isAction){
  157. columns.value = [...columns.value, {
  158. title: '操作',
  159. dataIndex: 'operation',
  160. width: 120,
  161. align: 'center',
  162. slots: { customRender: 'operation' },
  163. customCell:(_, index) => {
  164. if (index % 2 == 0) {
  165. return { rowSpan: 2 };
  166. } else {
  167. return { rowSpan: 0 };
  168. }
  169. }
  170. }];
  171. }
  172. // columns.value = [...columns.value, ...columns.value]
  173. return columns;
  174. }
  175. watch(
  176. () => {
  177. return props.columnsType;
  178. },
  179. (newVal, oldVal) => {
  180. if (!newVal) return
  181. setColumns(newVal)
  182. nextTick(() => {
  183. const headEl = document.querySelector(`.zxm-table-thead`);
  184. if (headEl) {
  185. headElHeight.value = headEl.clientHeight
  186. tableScroll.value = { y: (scrollY || props.scroll.y) - (headElHeight.value - 56), x: 'max-content' }
  187. }
  188. })
  189. },
  190. {
  191. immediate: true
  192. }
  193. );
  194. watch(
  195. () => {
  196. return props.dataSource;
  197. },
  198. (newVal, oldVal) => {
  199. const list: unknown[] = [];
  200. newVal.forEach((item) => {
  201. const data: any = toRaw(item);
  202. const resultData1 = {};
  203. const resultData2 = {};
  204. // 将主风机、备风机的数据进行拆分
  205. columns.value.forEach((column) => {
  206. const columnKey = column.dataIndex;
  207. if(columnKey){
  208. if (columnKey.startsWith('Fan')) {
  209. const key1 = columnKey.replace('Fan', 'Fan1');
  210. const key2 = columnKey.replace('Fan', 'Fan2');
  211. if (columnKey.endsWith('_merge')) {
  212. resultData1[columnKey] = (data[key1] == 0 || data[key1] == null || data[key1] == undefined) ? data[key2] : data[key1];
  213. }else{
  214. resultData1[columnKey] = data[key1];
  215. resultData2[columnKey] = data[key2];
  216. if (resultData1[columnKey] == undefined && resultData2[columnKey] == undefined) {
  217. resultData1[columnKey] = data[columnKey]
  218. resultData2[columnKey] = data[columnKey]
  219. }
  220. }
  221. } else if(columnKey.startsWith('fan')) {
  222. const key1 = columnKey.replace('fan', 'fan1');
  223. const key2 = columnKey.replace('fan', 'fan2');
  224. if (columnKey.endsWith('_merge')) {
  225. resultData1[columnKey] = (!data[key1] || data[key1] == 0 || data[key1] == null || data[key1] == undefined ) ? data[key2] : data[key1];
  226. } else {
  227. resultData1[columnKey] = data[key1];
  228. resultData2[columnKey] = data[key2];
  229. if(resultData1[columnKey] == undefined && resultData2[columnKey] == undefined){
  230. resultData1[columnKey] = data[columnKey]
  231. resultData2[columnKey] = data[columnKey]
  232. }
  233. }
  234. } else if (columnKey.endsWith('_merge')) {
  235. resultData1[columnKey] = data[columnKey];
  236. } else {
  237. resultData1[columnKey] = resultData2[columnKey] = data[columnKey];
  238. }
  239. }
  240. });
  241. resultData1['deviceID'] = resultData2['deviceID'] = data['deviceID'];
  242. if(props.columnsType.startsWith('fanlocal')){
  243. resultData1['runDevice'] = '主机';
  244. resultData2['runDevice'] = '备机';
  245. }else{
  246. resultData1['runDevice'] = '1#风机';
  247. resultData2['runDevice'] = '2#风机';
  248. }
  249. list.push(resultData1, resultData2);
  250. });
  251. // if (oldVal.length < 1 && selectRowIndex.value == -1) {
  252. // // 第一次
  253. // setSelectedRowKeys(list[0]['deviceID']);
  254. // }
  255. dataTableSource.value = list;
  256. loading.value = false;
  257. }
  258. );
  259. watch(() => props.scroll.y, (newVal) => {
  260. if (newVal) {
  261. scrollY = newVal
  262. tableScroll.value = { y: newVal - (headElHeight.value - 56) , x: 'max-content' }
  263. } else {
  264. tableScroll.value = {}
  265. }
  266. }
  267. )
  268. onMounted(() => {
  269. // 如果是https
  270. // 反之是websocket
  271. });
  272. onUnmounted(() => {});
  273. defineExpose({
  274. setSelectedRowKeys,
  275. });
  276. </script>
  277. <style scoped lang="less">
  278. @ventSpace: zxm;
  279. :deep(.@{ventSpace}-table-body) {
  280. height: auto !important;
  281. &::-webkit-scrollbar{
  282. height: 5px !important;
  283. }
  284. }
  285. :deep(.jeecg-basic-table .@{ventSpace}-table-wrapper .@{ventSpace}-table-title) {
  286. min-height: 0;
  287. }
  288. </style>