resetSubjectList.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {
  2. NButton,
  3. NSpace,
  4. useMessage,
  5. NForm,
  6. NFormItem,
  7. NCascader,
  8. NScrollbar
  9. } from 'naive-ui';
  10. import { defineComponent, onMounted, reactive, ref } from 'vue';
  11. import styles from './resetSubjectList.module.less';
  12. import { getClassSubjects, updateBatchInstrument } from '../api';
  13. export default defineComponent({
  14. props: {
  15. activeRow: {
  16. type: Array,
  17. default: () => []
  18. }
  19. },
  20. name: 'resetSubjectList',
  21. emits: ['close', 'getList'],
  22. setup(props, { emit }) {
  23. const data = reactive({
  24. uploading: false
  25. });
  26. const message = useMessage();
  27. const formsRef = ref();
  28. // const subjectList = ref(toRef(props.activeRow));
  29. const createClassForm = reactive({
  30. classList: props.activeRow
  31. // currentGradeNum: null,
  32. // gradeYear: null,
  33. // currentClass: null,
  34. // instrumentId: null,
  35. // id: null
  36. });
  37. onMounted(() => {
  38. // createClassForm.currentGradeNum = props.activeRow.currentGradeNum;
  39. // createClassForm.gradeYear = props.activeRow.gradeYear;
  40. // createClassForm.currentClass = props.activeRow.currentClass;
  41. // createClassForm.instrumentId = props.activeRow.instrumentId;
  42. // createClassForm.id = props.activeRow.id;
  43. getConfigSubject();
  44. });
  45. const submitForms = () => {
  46. formsRef.value.validate(async (error: any) => {
  47. if (error) {
  48. return;
  49. }
  50. // console.log(createClassForm.classList);
  51. data.uploading = true;
  52. // [{
  53. // "id": "1901469499486425089",
  54. // "instrumentId": "1002"
  55. // }]
  56. try {
  57. const params: any[] = [];
  58. createClassForm.classList.forEach((item: any) => {
  59. params.push({
  60. id: item.classGroupId,
  61. instrumentId: item.instrumentId
  62. });
  63. });
  64. // console.log(params, 'params');
  65. await updateBatchInstrument(params);
  66. message.success('修改成功');
  67. emit('close');
  68. emit('getList');
  69. data.uploading = false;
  70. } catch (e) {
  71. console.log(e);
  72. }
  73. data.uploading = false;
  74. });
  75. };
  76. const getConfigSubject = async () => {
  77. try {
  78. const ids = props.activeRow.map((item: any) => {
  79. return item.classGroupId;
  80. });
  81. const { data } = await getClassSubjects(ids);
  82. // const temp = data || [];
  83. if (Array.isArray(data)) {
  84. createClassForm.classList.forEach((item: any) => {
  85. const classSubject = data.find(
  86. (subject: any) => subject.classGroupId === item.classGroupId
  87. );
  88. if (classSubject) {
  89. item.subjectList = classSubject.subjectList || [];
  90. }
  91. });
  92. }
  93. // console.log(data, 'data', createClassForm.classList);
  94. // subjectList.value = temp;
  95. } catch {
  96. //
  97. }
  98. };
  99. return () => (
  100. <div class={[styles.addClass]}>
  101. <NForm label-placement="top" model={createClassForm} ref={formsRef}>
  102. <NScrollbar style="max-height: 360px; padding: 0 30px">
  103. {createClassForm.classList.map((item: any, index: number) => (
  104. <NFormItem
  105. path={`classList[${index}].instrumentId`}
  106. label={item.classGroupName}
  107. showRequireMark={false}
  108. rule={[
  109. {
  110. required: true,
  111. message: '请选择乐器',
  112. trigger: 'change'
  113. }
  114. ]}>
  115. <NCascader
  116. placeholder="请选择乐器"
  117. v-model:value={item.instrumentId}
  118. options={item.subjectList}
  119. checkStrategy="child"
  120. showPath={false}
  121. childrenField="instruments"
  122. expandTrigger="hover"
  123. labelField="name"
  124. valueField="id"
  125. clearable
  126. filterable
  127. style={{ width: '400px' }}
  128. />
  129. </NFormItem>
  130. ))}
  131. </NScrollbar>
  132. </NForm>
  133. <NSpace class={styles.btnGroup} justify="end">
  134. <NButton secondary onClick={() => emit('close')}>
  135. 取消
  136. </NButton>
  137. <NButton
  138. loading={data.uploading}
  139. onClick={() => submitForms()}
  140. type="primary">
  141. 确认
  142. </NButton>
  143. </NSpace>
  144. </div>
  145. );
  146. }
  147. });