QuestionInfoEdit.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <div class="question-info-edit">
  3. <el-form label-width="100px">
  4. <el-form-item label="难度">
  5. <el-select
  6. v-model="modelForm.difficulty"
  7. placeholder="请选择难度"
  8. @change="emitChange"
  9. >
  10. <el-option
  11. v-for="item in DIFFICULTY_LIST"
  12. :key="item.code"
  13. :label="item.name"
  14. :value="item.code"
  15. >
  16. </el-option>
  17. </el-select>
  18. </el-form-item>
  19. <el-form-item prop="quesProperties" label="属性名">
  20. <div class="box-flex">
  21. <property-select
  22. v-model="properties.coursePropertyId"
  23. :course-id="modelForm.courseId"
  24. @change="coursePropertyChange"
  25. ></property-select>
  26. <span>一级</span>
  27. <property-sub-select
  28. v-model="properties.firstPropertyId"
  29. :parent-id="properties.coursePropertyId"
  30. data-type="first"
  31. @change="firstPropertyChange"
  32. ></property-sub-select>
  33. <span>二级</span>
  34. <property-sub-select
  35. v-model="properties.secondPropertyId"
  36. :parent-id="properties.firstPropertyId"
  37. data-type="second"
  38. @change="secondPropertyChange"
  39. ></property-sub-select>
  40. <el-button
  41. type="primary"
  42. icon="icon icon-plus-white"
  43. :disabled="!propSelected"
  44. @click="addProperty"
  45. >新增属性</el-button
  46. >
  47. </div>
  48. </el-form-item>
  49. <el-form-item label="属性列表">
  50. <el-tag
  51. v-for="item in modelForm.quesProperties"
  52. :key="item.id"
  53. style="margin-right: 5px"
  54. closable
  55. effect="dark"
  56. type="primary"
  57. @close="removeProperty(item)"
  58. >
  59. {{ item.name }}
  60. </el-tag>
  61. </el-form-item>
  62. </el-form>
  63. </div>
  64. </template>
  65. <script>
  66. import { DIFFICULTY_LIST } from "@/constants/constants";
  67. const initModalForm = {
  68. courseId: "",
  69. difficulty: "易",
  70. quesProperties: [],
  71. };
  72. export default {
  73. name: "QuestionInfoEdit",
  74. props: {
  75. question: {
  76. type: Object,
  77. default() {
  78. return {};
  79. },
  80. },
  81. },
  82. data() {
  83. return {
  84. DIFFICULTY_LIST,
  85. modelForm: {
  86. ...initModalForm,
  87. },
  88. properties: {
  89. coursePropertyId: "",
  90. firstPropertyId: "",
  91. secondPropertyId: "",
  92. },
  93. selection: {
  94. courseProperty: {},
  95. firstProperty: {},
  96. secondProperty: {},
  97. },
  98. };
  99. },
  100. computed: {
  101. propSelected() {
  102. return (
  103. this.properties.coursePropertyId &&
  104. this.properties.firstPropertyId &&
  105. this.properties.secondPropertyId
  106. );
  107. },
  108. },
  109. mounted() {
  110. this.initData();
  111. },
  112. methods: {
  113. initData() {
  114. this.modelForm = this.$objAssign(initModalForm, this.question);
  115. },
  116. coursePropertyChange(val) {
  117. this.selection.courseProperty = val || {};
  118. },
  119. firstPropertyChange(val) {
  120. this.selection.firstProperty = val || {};
  121. },
  122. secondPropertyChange(val) {
  123. this.selection.secondProperty = val || {};
  124. },
  125. removeProperty(property) {
  126. this.modelForm.quesProperties = this.modelForm.quesProperties.filter(
  127. (item) => property.id !== item.id
  128. );
  129. this.emitChange();
  130. },
  131. addProperty() {
  132. if (!this.propSelected) return;
  133. const newProperty = {
  134. id: `${this.properties.coursePropertyId}_${this.properties.firstPropertyId}_${this.properties.secondPropertyId}`,
  135. name: `${this.selection.courseProperty.name},${this.selection.firstProperty.name},${this.selection.secondProperty.name}`,
  136. ...this.properties,
  137. };
  138. const propertyExist = this.modelForm.quesProperties.find(
  139. (item) => item.id === newProperty.id
  140. );
  141. if (propertyExist) {
  142. this.$message.error("属性已存在!");
  143. return;
  144. }
  145. this.modelForm.quesProperties.push(newProperty);
  146. this.emitChange();
  147. },
  148. emitChange() {
  149. this.$emit("change", this.modelForm);
  150. },
  151. },
  152. };
  153. </script>