ref.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <div>
  3. <BasicTable @register="registerTable">
  4. <template #toolbar>
  5. <a-button v-show="!getCheckRole('tourist')" type="primary" @click="handleCreate">
  6. 新增商品属性</a-button
  7. >
  8. </template>
  9. <template #action="{ record, column }">
  10. <TableAction :actions="createActions(record, column)" />
  11. </template>
  12. </BasicTable>
  13. <addModal @register="registerModal" @saveadd="handleSaveAdd" />
  14. </div>
  15. </template>
  16. <script lang="ts">
  17. import { defineComponent, ref } from 'vue'; // h
  18. import {
  19. BasicTable,
  20. useTable,
  21. BasicColumn,
  22. TableAction,
  23. EditRecordRow,
  24. ActionItem,
  25. } from '/@/components/Table';
  26. import { attributeListApi, attributeDeleteApi, updateItemApi } from '/@/api/product/category';
  27. import { cloneDeep } from 'lodash-es';
  28. // import { Tag } from 'ant-design-vue';
  29. import { useI18n } from '/@/hooks/web/useI18n';
  30. import { useModal } from '/@/components/Modal';
  31. import addModal from './addModal.vue';
  32. import { useMessage } from '/@/hooks/web/useMessage';
  33. import { useUserStore } from '/@/store/modules/user';
  34. export default defineComponent({
  35. components: { BasicTable, TableAction, addModal },
  36. setup() {
  37. const { createConfirm, createMessage: msg } = useMessage();
  38. const [registerModal, { openModal }] = useModal();
  39. const currentEditKeyRef = ref('');
  40. const userStore = useUserStore();
  41. const { getCheckRole } = userStore;
  42. const { t } = useI18n();
  43. console.log('registerModal', registerModal);
  44. const columns: BasicColumn[] = [
  45. {
  46. title: t('routes.dashboard.productRef'),
  47. editRow: true,
  48. editRule: async (text) => {
  49. if (!text) {
  50. return t('routes.product.productRefMessge');
  51. }
  52. if (text && text.length > 15) {
  53. return t('routes.corporation.maxlength');
  54. }
  55. return '';
  56. },
  57. dataIndex: 'name',
  58. fixed: 'left',
  59. width: 100,
  60. },
  61. {
  62. title: t('routes.scenes.sortOrder'),
  63. dataIndex: 'sortOrder',
  64. editComponent: 'InputNumber',
  65. editRule: async (text) => {
  66. if (text == null || text < 0) {
  67. return t('routes.product.sortOrderTips');
  68. }
  69. return '';
  70. },
  71. editRow: true,
  72. width: 50,
  73. },
  74. ];
  75. const [registerTable, { expandAll, reload, collapseAll }] = useTable({
  76. title: t('routes.dashboard.productRef'),
  77. api: attributeListApi,
  78. columns: columns,
  79. useSearchForm: true,
  80. showIndexColumn: false,
  81. // rowSelection: { type: 'checkbox' },
  82. rowKey: 'id',
  83. bordered: true,
  84. showIndexColumn: true,
  85. fetchSetting: {
  86. pageField: 'page',
  87. sizeField: 'limit',
  88. listField: 'list',
  89. totalField: 'totalCount',
  90. },
  91. formConfig: {
  92. labelWidth: 66,
  93. schemas: [
  94. {
  95. field: `name`,
  96. label: t('routes.dashboard.productRef'),
  97. component: 'Input',
  98. colProps: {
  99. xl: 6,
  100. xxl: 6,
  101. },
  102. componentProps: {
  103. maxLength: 15,
  104. },
  105. },
  106. ],
  107. },
  108. actionColumn: {
  109. ifShow: !getCheckRole('tourist'),
  110. width: 160,
  111. title: t('common.operating'),
  112. dataIndex: 'action',
  113. slots: { customRender: 'action' },
  114. },
  115. });
  116. async function handleCreate() {
  117. openModal(true);
  118. }
  119. function handleEdit(record: EditRecordRow) {
  120. currentEditKeyRef.value = record.key;
  121. record.onEdit?.(true);
  122. }
  123. function handleDelete(record) {
  124. createConfirm({
  125. iconType: 'warning',
  126. title: t('common.warning'),
  127. content: t('routes.staff.delMessage', { userName: record.name }),
  128. onOk: async () => {
  129. let res = await attributeDeleteApi(record.id);
  130. console.log('res', res);
  131. reload();
  132. },
  133. });
  134. }
  135. function handleCancel(record: EditRecordRow) {
  136. currentEditKeyRef.value = '';
  137. record.onEdit?.(false, false);
  138. }
  139. async function handleSaveAdd() {
  140. console.log('handleSaveAdd');
  141. reload();
  142. }
  143. async function handleSave(record: EditRecordRow) {
  144. // 校验
  145. msg.loading({ content: t('routes.product.preservation'), duration: 0, key: 'saving' });
  146. const valid = await record.onValid?.();
  147. console.log('ref', valid);
  148. if (valid) {
  149. try {
  150. const data = cloneDeep(record.editValueRefs);
  151. console.log(data);
  152. //TODO 此处将数据提交给服务器保存
  153. // ...
  154. updateItemApi({
  155. id: record.id,
  156. ...data,
  157. });
  158. // 保存之后提交编辑状态
  159. const pass = await record.onEdit?.(false, true);
  160. if (pass) {
  161. currentEditKeyRef.value = '';
  162. }
  163. msg.success({ content: t('routes.product.dataSave'), key: 'saving' });
  164. reload();
  165. } catch (error) {
  166. msg.error({ content: t('routes.product.dataSaveFail'), key: 'saving' });
  167. }
  168. } else {
  169. msg.error({ content: t('routes.product.correctData'), key: 'saving' });
  170. }
  171. }
  172. function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
  173. // console.log('editable', record);
  174. if (!record.editable) {
  175. return [
  176. {
  177. label: t('common.edit'),
  178. disabled: currentEditKeyRef.value ? currentEditKeyRef.value !== record.key : false,
  179. onClick: handleEdit.bind(null, record),
  180. },
  181. {
  182. label: t('common.delText'),
  183. color: 'error',
  184. disabled: currentEditKeyRef.value ? currentEditKeyRef.value !== record.key : false,
  185. onClick: handleDelete.bind(null, record),
  186. },
  187. ];
  188. }
  189. return [
  190. {
  191. label: t('common.saveText'),
  192. onClick: handleSave.bind(null, record, column),
  193. },
  194. {
  195. label: t('common.cancelText'),
  196. popConfirm: {
  197. title: t('common.EditorNot'),
  198. confirm: handleCancel.bind(null, record, column),
  199. },
  200. },
  201. ];
  202. }
  203. return {
  204. registerTable,
  205. createActions,
  206. t,
  207. registerModal,
  208. handleSaveAdd,
  209. handleCreate,
  210. handleEdit,
  211. handleDelete,
  212. expandAll,
  213. collapseAll,
  214. getCheckRole,
  215. reload,
  216. };
  217. },
  218. });
  219. </script>