GroupMonitorTable.vue 5.8 KB

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