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