PointTable.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <div class="p-4">
  3. <a-button v-if="isAdd" type="primary" @click="addRow"> 新增 </a-button>
  4. <BasicTable @register="registerTable" :dataSource="dataSource" @edit-change="onEditChange">
  5. <template #action="{ record, column }">
  6. <TableAction :actions="createActions(record, column)" />
  7. </template>
  8. <template #bodyCell="{ record, column, index }">
  9. <div v-if="record.editable && column.dataIndex === 'link_devicetype'">
  10. <ApiTreeSelect
  11. style="width: 100%"
  12. :api="deviceList.bind(null, { devicetype: '' })"
  13. :fieldNames="{ children: 'children', label: 'itemText', value: 'itemValue' }"
  14. @change="handleChangeDeviceType($event, index)"
  15. v-model:value="record['editValueRefs']['link_devicetype']"
  16. placeholder="请选择设备分类"
  17. />
  18. </div>
  19. <div v-if="record.editable && column.dataIndex === 'link_id'">
  20. <Select
  21. @change="handleChange($event, index)"
  22. :options="options"
  23. :fieldNames="{ label: 'deviceName', value: 'deviceID' }"
  24. v-model:value="record['editValueRefs']['link_id']"
  25. style="width: 100%"
  26. ></Select>
  27. </div>
  28. <div v-if="record.editable && column.dataIndex === 'link_code'">
  29. <Select
  30. style="width: 100%"
  31. :options="monitorParamsOptions"
  32. :fieldNames="{ label: 'valuename', value: 'valuecode' }"
  33. @change="handleChangeLinkCode($event, index)"
  34. v-model:value="record['editValueRefs']['link_code']"
  35. ></Select>
  36. </div>
  37. </template>
  38. </BasicTable>
  39. </div>
  40. </template>
  41. <script lang="ts">
  42. import { defineComponent, ref, nextTick, inject, onMounted, watch } from 'vue';
  43. import { BasicTable, useTable, TableAction, BasicColumn, ActionItem, EditRecordRow } from '/@/components/Table';
  44. import { Select } from 'ant-design-vue';
  45. import { ApiTreeSelect } from '/@/components/Form';
  46. import { deviceId, getDeviceId, deviceList, list, pointEdit } from './point.api';
  47. // import { nextTick } from 'process';
  48. export default defineComponent({
  49. components: { BasicTable, TableAction, Select, ApiTreeSelect },
  50. props: {
  51. columns: {
  52. type: Array,
  53. requried: true,
  54. },
  55. deviceId: {
  56. type: String || Number,
  57. requried: true,
  58. },
  59. pointType: {
  60. type: String,
  61. requried: true,
  62. },
  63. list: {
  64. type: Function,
  65. requried: true,
  66. },
  67. isAdd: {
  68. type: Boolean,
  69. },
  70. },
  71. emits: ['save', 'delete'],
  72. setup(props, { emit }) {
  73. const deviceType = inject('deviceType');
  74. const options = ref([]);
  75. const monitorParamsOptions = ref([]);
  76. const currentEditKeyRef = ref('');
  77. const dataSource = ref<any>([]);
  78. const [registerTable, { insertTableDataRecord, reload }] = useTable({
  79. title: '',
  80. columns: props.columns as BasicColumn[],
  81. showIndexColumn: false,
  82. showTableSetting: false,
  83. tableSetting: { fullScreen: true },
  84. actionColumn: {
  85. width: 160,
  86. title: '操作',
  87. dataIndex: 'action',
  88. slots: { customRender: 'action' },
  89. },
  90. });
  91. function addRow() {
  92. const record = {} as EditRecordRow;
  93. insertTableDataRecord(record);
  94. nextTick(() => {
  95. handleEdit(record);
  96. });
  97. }
  98. function handleEdit(record: EditRecordRow) {
  99. currentEditKeyRef.value = record.key;
  100. record.onEdit?.(true);
  101. handleChange(record['link_id']);
  102. handleChangeDeviceType(record['link_devicetype']);
  103. }
  104. function handleCancel(record: EditRecordRow) {
  105. currentEditKeyRef.value = '';
  106. record.onEdit?.(false, false);
  107. }
  108. function handleDelete(record: EditRecordRow) {
  109. emit('delete', record.id, reload);
  110. }
  111. function getOptions(value) {
  112. console.log(value);
  113. }
  114. function handleChange(id, index?) {
  115. if (!id) return;
  116. if (index !== undefined) dataSource['value'][index]['link_id'] = id;
  117. deviceId({ id: id }).then((res) => {
  118. monitorParamsOptions.value = res;
  119. console.log(res);
  120. });
  121. }
  122. function handleChangeDeviceType(e, index?) {
  123. if (!e) return;
  124. if (index !== undefined) dataSource['value'][index]['link_devicetype'] = e;
  125. getDeviceId({ devicetype: e }).then((res) => {
  126. options.value = res;
  127. });
  128. }
  129. function handleChangeLinkCode(e, index?) {
  130. if (!e) return;
  131. if (index !== undefined) dataSource['value'][index]['link_code'] = e;
  132. }
  133. async function handleSave(record: EditRecordRow) {
  134. dataSource.value.filter((item) => {
  135. if (record.id && record.id === item.id) {
  136. console.log(111);
  137. Object.assign(item, record.editValueRefs);
  138. item['test'] = '1212';
  139. console.log(222, item, record.editValueRefs);
  140. }
  141. });
  142. // await edit(dataSource.value);
  143. await pointEdit({ deviceid: props.deviceId, linklist: dataSource.value });
  144. record.onEdit?.(false, false);
  145. await getDataSource();
  146. }
  147. function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
  148. if (!record.editable) {
  149. if (props.isAdd) {
  150. return [
  151. {
  152. label: '编辑',
  153. disabled: currentEditKeyRef.value ? currentEditKeyRef.value !== record.key : false,
  154. onClick: handleEdit.bind(null, record),
  155. },
  156. {
  157. label: '删除',
  158. disabled: currentEditKeyRef.value ? currentEditKeyRef.value !== record.key : false,
  159. onClick: handleDelete.bind(null, record),
  160. },
  161. ];
  162. } else {
  163. return [
  164. {
  165. label: '编辑',
  166. disabled: currentEditKeyRef.value ? currentEditKeyRef.value !== record.key : false,
  167. onClick: handleEdit.bind(null, record),
  168. },
  169. ];
  170. }
  171. }
  172. return [
  173. {
  174. label: '保存',
  175. onClick: handleSave.bind(null, record, column),
  176. },
  177. {
  178. label: '取消',
  179. popConfirm: {
  180. title: '是否取消编辑',
  181. confirm: handleCancel.bind(null, record, column),
  182. },
  183. },
  184. ];
  185. }
  186. function onEditChange({ column, value, record }) {
  187. // 本例
  188. if (column.dataIndex === 'devicetype') {
  189. // record.editValueRefs.name4.value = `${value}`;
  190. }
  191. // console.log(column, value, record);
  192. }
  193. // watch(() => props.pointType, async(pointType) => {
  194. // await getDataSource(pointType)
  195. // })
  196. async function getDataSource(pointType) {
  197. debugger
  198. const result = await list({ devicetype: pointType, valuetype: 9, deviceid: props.deviceId });
  199. dataSource.value = result.records;
  200. }
  201. onMounted(async () => {
  202. await getDataSource(props.pointType);
  203. });
  204. return {
  205. registerTable,
  206. handleEdit,
  207. createActions,
  208. onEditChange,
  209. addRow,
  210. handleChange,
  211. handleChangeDeviceType,
  212. handleChangeLinkCode,
  213. getOptions,
  214. options,
  215. monitorParamsOptions,
  216. deviceList,
  217. dataSource,
  218. };
  219. },
  220. });
  221. </script>
  222. <style scoped lang="less">
  223. @ventSpace: zxm;
  224. :deep(.@{ventSpace}-table-body) {
  225. height: auto !important;
  226. }
  227. </style>