GroupMonitorTable.vue 6.3 KB

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