EditFillQuestion.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <div class="edit-fill-question">
  3. <el-form
  4. ref="modalFormComp"
  5. :model="modalForm"
  6. :rules="rules"
  7. :key="modalForm.id"
  8. label-width="120px"
  9. >
  10. <el-form-item prop="topicType" label="题型:">
  11. <el-radio-group v-model="topicType" @change="topicTypeChange">
  12. <el-radio-button label="single">单选</el-radio-button>
  13. <el-radio-button label="multiply">多选</el-radio-button>
  14. <el-radio-button label="boolean">判断</el-radio-button>
  15. </el-radio-group>
  16. </el-form-item>
  17. <el-form-item prop="topicName" label="题目名称:">
  18. <el-input
  19. v-model="modalForm.topicName"
  20. type="textarea"
  21. :autosize="{ minRows: 2, maxRows: 5 }"
  22. resize="none"
  23. placeholder="请输入题目名称"
  24. clearable
  25. ></el-input>
  26. </el-form-item>
  27. <el-form-item prop="endNumber" label="起止题号:">
  28. <el-input-number
  29. style="width:40px;"
  30. v-model="modalForm.startNumber"
  31. :min="0"
  32. :max="999"
  33. :step="1"
  34. step-strictly
  35. :controls="false"
  36. ></el-input-number>
  37. <span class="el-input-split"></span>
  38. <el-input-number
  39. style="width:40px;"
  40. v-model="modalForm.endNumber"
  41. :min="modalForm.startNumber"
  42. :max="999"
  43. :step="1"
  44. step-strictly
  45. :controls="false"
  46. ></el-input-number>
  47. </el-form-item>
  48. <el-form-item prop="optionCount" label="选项个数:">
  49. <el-input-number
  50. style="width:125px;"
  51. v-model="modalForm.optionCount"
  52. :min="2"
  53. :max="22"
  54. :step="1"
  55. step-strictly
  56. :controls="false"
  57. :disabled="modalForm.isBoolean"
  58. ></el-input-number>
  59. </el-form-item>
  60. <!-- <el-form-item label="小题排列方向:" required>
  61. <el-radio-group v-model="modalForm.questionDirection" size="small">
  62. <el-radio-button
  63. v-for="(val, key) in DIRECTION_TYPE"
  64. :key="key"
  65. :label="key"
  66. >{{ val }}</el-radio-button
  67. >
  68. </el-radio-group>
  69. </el-form-item> -->
  70. <el-form-item v-if="modalForm.isBoolean" label="是否配置:">
  71. <el-select
  72. v-model="modalForm.booleanType"
  73. style="width:125px;"
  74. placeholder="请选择"
  75. @change="booleanTypeChange"
  76. >
  77. <el-option
  78. v-for="item in BOOLEAN_TYPE"
  79. :key="item"
  80. :label="item"
  81. :value="item"
  82. ></el-option>
  83. </el-select>
  84. <span>(备选是否配置)</span>
  85. </el-form-item>
  86. <el-form-item v-if="modalForm.isBoolean" prop="booleanType">
  87. <span>是:</span>
  88. <el-input
  89. v-model.trim="booleanTypes.yes"
  90. :maxlength="1"
  91. placeholder="是"
  92. style="margin-right: 20px;width:60px;"
  93. ></el-input>
  94. <span>否:</span>
  95. <el-input
  96. v-model.trim="booleanTypes.no"
  97. :maxlength="1"
  98. placeholder="否"
  99. style="margin-right: 20px;width:60px;"
  100. ></el-input>
  101. </el-form-item>
  102. </el-form>
  103. </div>
  104. </template>
  105. <script>
  106. import { BOOLEAN_TYPE, DIRECTION_TYPE } from "../../enumerate";
  107. const initModalForm = {
  108. id: "",
  109. topicName: "",
  110. startNumber: 1,
  111. endNumber: 5,
  112. questionsCount: 10,
  113. optionCount: 5,
  114. questionDirection: "horizontal",
  115. isBoolean: false,
  116. booleanType: BOOLEAN_TYPE[0],
  117. isMultiply: false
  118. };
  119. export default {
  120. name: "edit-fill-question",
  121. props: {
  122. instance: {
  123. type: Object,
  124. default() {
  125. return {};
  126. }
  127. }
  128. },
  129. data() {
  130. const topicTypeValidater = (rule, value, callback) => {
  131. if (!this.topicType) {
  132. callback(new Error("请选择题型"));
  133. } else {
  134. callback();
  135. }
  136. };
  137. const numberRangeValidater = (rule, value, callback) => {
  138. if (!this.modalForm.startNumber || !this.modalForm.endNumber) {
  139. return callback(new Error("请输入起止题号"));
  140. }
  141. if (this.modalForm.startNumber > this.modalForm.endNumber) {
  142. callback(new Error("开始题号不能大于结束题号"));
  143. } else {
  144. callback();
  145. }
  146. };
  147. const booleanTypeValidater = (rule, value, callback) => {
  148. if (this.modalForm.isBoolean) {
  149. if (
  150. this.booleanTypes.yes &&
  151. this.booleanTypes.no &&
  152. this.booleanTypes.yes.length <= 2 &&
  153. this.booleanTypes.no.length <= 2
  154. ) {
  155. callback();
  156. } else {
  157. callback(new Error("请设置是否配置,单个设置最多两个字符。"));
  158. }
  159. } else {
  160. callback();
  161. }
  162. };
  163. return {
  164. modalForm: { ...initModalForm },
  165. topicType: null,
  166. BOOLEAN_TYPE,
  167. DIRECTION_TYPE,
  168. booleanTypes: {
  169. yes: "",
  170. no: ""
  171. },
  172. rules: {
  173. topicType: [
  174. {
  175. required: true,
  176. validator: topicTypeValidater,
  177. trigger: "change"
  178. }
  179. ],
  180. topicName: [
  181. {
  182. required: true,
  183. message: "请输入题目名称",
  184. trigger: "change"
  185. }
  186. ],
  187. endNumber: [
  188. {
  189. required: true,
  190. message: "请输入起止题号",
  191. trigger: "change"
  192. },
  193. {
  194. type: "number",
  195. validator: numberRangeValidater,
  196. trigger: "change"
  197. }
  198. ],
  199. optionCount: [
  200. {
  201. required: true,
  202. type: "number",
  203. message: "请输入选项个数",
  204. trigger: "change"
  205. }
  206. ],
  207. booleanType: [
  208. {
  209. required: true,
  210. validator: booleanTypeValidater,
  211. trigger: "change"
  212. }
  213. ]
  214. }
  215. };
  216. },
  217. mounted() {
  218. this.initData(this.instance);
  219. },
  220. methods: {
  221. initData(val) {
  222. const valInfo = val.parent || val;
  223. if (val["_edit"]) this.topicType = this.getTopicType(valInfo);
  224. this.modalForm = Object.assign({}, initModalForm, valInfo);
  225. this.modalForm.endNumber =
  226. this.modalForm.startNumber + this.modalForm.questionsCount - 1;
  227. this.booleanTypeChange();
  228. },
  229. booleanTypeChange() {
  230. const [yes, no] = this.modalForm.booleanType.split(",");
  231. this.booleanTypes.yes = yes;
  232. this.booleanTypes.no = no;
  233. },
  234. topicTypeChange() {
  235. if (this.topicType === "single") {
  236. this.modalForm.isMultiply = false;
  237. this.modalForm.isBoolean = false;
  238. this.modalForm.optionCount = 4;
  239. } else if (this.topicType === "multiply") {
  240. this.modalForm.isMultiply = true;
  241. this.modalForm.isBoolean = false;
  242. this.modalForm.optionCount = 4;
  243. } else {
  244. this.modalForm.isMultiply = false;
  245. this.modalForm.isBoolean = true;
  246. this.modalForm.optionCount = 2;
  247. this.modalForm.booleanType = BOOLEAN_TYPE[0];
  248. this.booleanTypeChange();
  249. }
  250. },
  251. getTopicType(info) {
  252. if (info.isMultiply) return "multiply";
  253. if (info.isBoolean) return "boolean";
  254. return "single";
  255. },
  256. async submit() {
  257. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  258. if (!valid) return;
  259. this.modalForm.questionsCount =
  260. this.modalForm.endNumber - this.modalForm.startNumber + 1;
  261. this.modalForm.booleanType = [
  262. this.booleanTypes.yes,
  263. this.booleanTypes.no
  264. ].join();
  265. this.modalForm.topicName = this.modalForm.topicName.trim();
  266. this.$emit("modified", this.modalForm);
  267. }
  268. }
  269. };
  270. </script>