PointTable.vue 8.2 KB

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