EditFillQuestion.vue 8.1 KB

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