EditFillQuestion.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.optionDirection" 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. optionDirection: "horizontal",
  126. questionDirection: "vertical",
  127. isBoolean: false,
  128. booleanType: BOOLEAN_TYPE[0],
  129. isMultiply: false,
  130. };
  131. export default {
  132. name: "edit-fill-question",
  133. props: {
  134. instance: {
  135. type: Object,
  136. default() {
  137. return {};
  138. },
  139. },
  140. },
  141. data() {
  142. const topicTypeValidater = (rule, value, callback) => {
  143. if (!this.topicType) {
  144. callback(new Error("请选择题型"));
  145. } else {
  146. callback();
  147. }
  148. };
  149. const numberRangeValidater = (rule, value, callback) => {
  150. if (!this.modalForm.startNumber || !this.modalForm.endNumber) {
  151. return callback(new Error("请输入起止题号"));
  152. }
  153. if (this.modalForm.startNumber > this.modalForm.endNumber) {
  154. callback(new Error("开始题号不能大于结束题号"));
  155. } else {
  156. callback();
  157. }
  158. };
  159. const booleanTypeValidater = (rule, value, callback) => {
  160. if (this.modalForm.isBoolean) {
  161. if (
  162. this.booleanTypes.yes &&
  163. this.booleanTypes.no &&
  164. this.booleanTypes.yes.length <= 2 &&
  165. this.booleanTypes.no.length <= 2
  166. ) {
  167. callback();
  168. } else {
  169. callback(new Error("请设置是否配置,单个设置最多两个字符。"));
  170. }
  171. } else {
  172. callback();
  173. }
  174. };
  175. return {
  176. modalForm: { ...initModalForm },
  177. BOOLEAN_TYPE,
  178. DIRECTION_TYPE,
  179. topicType: null,
  180. booleanTypes: {
  181. yes: "",
  182. no: "",
  183. },
  184. rules: {
  185. topicType: [
  186. {
  187. required: true,
  188. validator: topicTypeValidater,
  189. trigger: "change",
  190. },
  191. ],
  192. topicNo: [
  193. {
  194. required: true,
  195. message: "请输入大题序号",
  196. trigger: "change",
  197. },
  198. ],
  199. topicName: [
  200. {
  201. required: true,
  202. message: "请输入题目名称",
  203. trigger: "change",
  204. },
  205. ],
  206. endNumber: [
  207. {
  208. required: true,
  209. message: "请输入起止题号",
  210. trigger: "change",
  211. },
  212. {
  213. type: "number",
  214. validator: numberRangeValidater,
  215. trigger: "change",
  216. },
  217. ],
  218. optionCount: [
  219. {
  220. required: true,
  221. type: "number",
  222. message: "请输入选项个数",
  223. trigger: "change",
  224. },
  225. ],
  226. booleanType: [
  227. {
  228. required: true,
  229. validator: booleanTypeValidater,
  230. trigger: "change",
  231. },
  232. ],
  233. },
  234. };
  235. },
  236. mounted() {
  237. this.initData(this.instance);
  238. },
  239. methods: {
  240. initData(val) {
  241. const valInfo = val.parent || val;
  242. if (val["_edit"]) this.topicType = this.getTopicType(valInfo);
  243. this.modalForm = Object.assign({}, initModalForm, valInfo);
  244. this.modalForm.endNumber =
  245. this.modalForm.startNumber + this.modalForm.questionsCount - 1;
  246. this.booleanTypeChange();
  247. },
  248. booleanTypeChange() {
  249. const [yes, no] = this.modalForm.booleanType.split(",");
  250. this.booleanTypes.yes = yes;
  251. this.booleanTypes.no = no;
  252. },
  253. topicTypeChange() {
  254. if (this.topicType === "single") {
  255. this.modalForm.isMultiply = false;
  256. this.modalForm.isBoolean = false;
  257. this.modalForm.optionCount = 4;
  258. } else if (this.topicType === "multiply") {
  259. this.modalForm.isMultiply = true;
  260. this.modalForm.isBoolean = false;
  261. this.modalForm.optionCount = 4;
  262. } else {
  263. this.modalForm.isMultiply = false;
  264. this.modalForm.isBoolean = true;
  265. this.modalForm.optionCount = 2;
  266. this.modalForm.booleanType = BOOLEAN_TYPE[0];
  267. this.booleanTypeChange();
  268. }
  269. },
  270. getTopicType(info) {
  271. if (info.isMultiply) return "multiply";
  272. if (info.isBoolean) return "boolean";
  273. return "single";
  274. },
  275. async submit() {
  276. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  277. if (!valid) return;
  278. this.modalForm.questionsCount =
  279. this.modalForm.endNumber - this.modalForm.startNumber + 1;
  280. this.modalForm.booleanType = [
  281. this.booleanTypes.yes,
  282. this.booleanTypes.no,
  283. ].join();
  284. this.modalForm.topicName = this.modalForm.topicName.trim();
  285. this.$emit("modified", this.modalForm);
  286. },
  287. },
  288. };
  289. </script>