GroupMonitorTable.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 }) : ref({})
  63. const columns = ref<any[]>([])
  64. // 默认初始是第一行
  65. const selectRowIndex = ref(-1);
  66. const rowClick = (record) => {
  67. return {
  68. onClick: () => {
  69. setSelectedRowKeys(record['deviceID'])
  70. },
  71. };
  72. };
  73. const setSelectedRowKeys = (target) => {
  74. console.log(target, Object.prototype.toString.call(target));
  75. if (Object.prototype.toString.call(target) === '[object String]') {
  76. selectRowIndex.value = target;
  77. emits('selectRow', target);
  78. } else if (Object.prototype.toString.call(target) === '[object Object]') {
  79. const data = target.target.value;
  80. selectRowIndex.value = data;
  81. emits('selectRow', data);
  82. }
  83. };
  84. /** 定义table Columns */
  85. function setColumns(columnsType) {
  86. const isCheckColumn = {
  87. title: '',
  88. dataIndex: 'isCheck',
  89. width: 40,
  90. align: 'center',
  91. customCell: (_, index) => {
  92. if (index % 2 == 0) {
  93. return { rowSpan: 2 };
  94. } else {
  95. return { rowSpan: 0 };
  96. }
  97. },
  98. };
  99. const indexColumn = {
  100. title: '序号',
  101. dataIndex: 'key',
  102. width: 120,
  103. align: 'center',
  104. customCell: (_, index) => {
  105. if (index % 2 == 0) {
  106. return { rowSpan: 2 };
  107. } else {
  108. return { rowSpan: 0 };
  109. }
  110. },
  111. customRender: function ({ index }) {
  112. return index/2 + 1;
  113. }
  114. };
  115. const runDevice = {
  116. title: '运行风机',
  117. dataIndex: 'runDevice',
  118. width: 80,
  119. align: 'center',
  120. };
  121. columns.value = getTableHeaderColumns(columnsType);
  122. console.log('风机columns------------------>', columnsType)
  123. if (columns.value && columns.value.length < 1) {
  124. columns.value = getTableHeaderColumns(columnsType.split('_')[0] + '_monitor');
  125. }
  126. const strinstallpos = columns.value.find((item) => {
  127. return item.dataIndex === 'strinstallpos' || item.dataIndex === 'strname';
  128. });
  129. if (strinstallpos) {
  130. strinstallpos.customCell = (_, index) => {
  131. if (index % 2 == 0) {
  132. return { rowSpan: 2 };
  133. } else {
  134. return { rowSpan: 0 };
  135. }
  136. };
  137. }
  138. columns.value.splice(1, 0, runDevice);
  139. if (props.isShowSelect) {
  140. columns.value = [isCheckColumn, ...columns.value];
  141. }else{
  142. columns.value = [indexColumn, ...columns.value];
  143. }
  144. if(props.isAction){
  145. columns.value = [...columns.value, {
  146. title: '操作',
  147. dataIndex: 'operation',
  148. width: 120,
  149. align: 'center',
  150. slots: { customRender: 'operation' },
  151. }];
  152. }
  153. return columns;
  154. }
  155. watch(
  156. () => {
  157. return props.columnsType;
  158. },
  159. (newVal, oldVal) => {
  160. if (!newVal) return
  161. setColumns(newVal)
  162. },
  163. {
  164. immediate: true
  165. }
  166. );
  167. watch(
  168. () => {
  169. return props.dataSource;
  170. },
  171. (newVal, oldVal) => {
  172. const list: unknown[] = [];
  173. newVal.forEach((item) => {
  174. const data: any = toRaw(item);
  175. const resultData1 = {};
  176. const resultData2 = {};
  177. // 将主风机、备风机的数据进行拆分
  178. columns.value.forEach((column) => {
  179. const columnKey = column.dataIndex;
  180. if(columnKey){
  181. if (columnKey.startsWith('Fan')) {
  182. const key1 = columnKey.replace('Fan', 'Fan1');
  183. const key2 = columnKey.replace('Fan', 'Fan2');
  184. resultData1[columnKey] = data[key1];
  185. resultData2[columnKey] = data[key2];
  186. } else if(columnKey.startsWith('fan')) {
  187. const key1 = columnKey.replace('fan', 'fan1');
  188. const key2 = columnKey.replace('fan', 'fan2');
  189. if(data[key1] != undefined || data[key2] != undefined) {
  190. resultData1[columnKey] = data[key1];
  191. resultData2[columnKey] = data[key2];
  192. }else{
  193. resultData1[columnKey] = resultData2[columnKey] = data[columnKey];
  194. }
  195. } else {
  196. resultData1[columnKey] = resultData2[columnKey] = data[columnKey];
  197. }
  198. }
  199. });
  200. resultData1['deviceID'] = resultData2['deviceID'] = data['deviceID'];
  201. resultData1['runDevice'] = '主机';
  202. resultData2['runDevice'] = '备机';
  203. list.push(resultData1, resultData2);
  204. });
  205. // if (oldVal.length < 1 && selectRowIndex.value == -1) {
  206. // // 第一次
  207. // setSelectedRowKeys(list[0]['deviceID']);
  208. // }
  209. dataTableSource.value = list;
  210. loading.value = false;
  211. }
  212. );
  213. watch(() => props.scroll.y, (newVal) => {
  214. if (newVal) {
  215. tableScroll.value = { y: newVal }
  216. } else {
  217. tableScroll.value = {}
  218. }
  219. }
  220. )
  221. onMounted(() => {
  222. // 如果是https
  223. // 反之是websocket
  224. debugger
  225. });
  226. onUnmounted(() => {});
  227. defineExpose({
  228. setSelectedRowKeys,
  229. });
  230. </script>
  231. <style scoped lang="less">
  232. @ventSpace: zxm;
  233. :deep(.@{ventSpace}-table-body) {
  234. height: auto !important;
  235. }
  236. :deep(.jeecg-basic-table .@{ventSpace}-table-wrapper .@{ventSpace}-table-title) {
  237. min-height: 0;
  238. }
  239. </style>