PointTable.vue 7.9 KB

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