MarkPaperStructure.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <template>
  2. <div class="mark-paper-structure">
  3. <p class="structure-desc">
  4. <span>课程名称:</span>
  5. <span class="mr-4">{{ datas.basicPaperInfo.courseName }}</span>
  6. <span>课程代码:</span>
  7. <span>{{ datas.basicPaperInfo.courseCode }}</span>
  8. </p>
  9. <el-table
  10. ref="TableList"
  11. :data="tableData"
  12. border
  13. :row-class-name="getRowClassName"
  14. >
  15. <el-table-column width="50" align="center">
  16. <template slot-scope="scope" v-if="scope.row.isMainFirstSub">
  17. <div
  18. :class="[
  19. 'expand-btn',
  20. { 'expand-btn-unexpand': !scope.row.expandSub }
  21. ]"
  22. @click="switchExpandSub(scope.row)"
  23. >
  24. <i
  25. :class="scope.row.expandSub ? 'el-icon-minus' : 'el-icon-plus'"
  26. ></i>
  27. </div>
  28. </template>
  29. </el-table-column>
  30. <el-table-column prop="mainTitle" label="大题名称">
  31. <span slot-scope="scope" v-if="scope.row.isMainFirstSub">
  32. <el-input
  33. v-model.trim="scope.row.mainTitle"
  34. size="small"
  35. :maxlength="200"
  36. clearable
  37. @change="mainTitleChange(scope.row)"
  38. ></el-input>
  39. </span>
  40. </el-table-column>
  41. <el-table-column prop="qType" label="类型" width="160">
  42. <template slot-scope="scope" v-if="scope.row.isMainFirstSub">
  43. <el-radio-group
  44. v-model="scope.row.qType"
  45. size="mini"
  46. @change="qTypeChange(scope.row)"
  47. >
  48. <el-radio-button
  49. v-for="(val, key) in Q_TYPE"
  50. :key="key"
  51. :label="key"
  52. >{{ val }}</el-radio-button
  53. >
  54. </el-radio-group>
  55. </template>
  56. </el-table-column>
  57. <el-table-column prop="mainNumber" label="大题号" width="80">
  58. <template slot-scope="scope" v-if="scope.row.isMainFirstSub">
  59. <span>{{ scope.row.mainNumber }}</span>
  60. </template>
  61. </el-table-column>
  62. <el-table-column
  63. prop="subNumber"
  64. label="小题号"
  65. width="80"
  66. ></el-table-column>
  67. <el-table-column prop="totalScore" label="小题满分" width="105">
  68. <template slot-scope="scope">
  69. <el-input-number
  70. v-model="scope.row.totalScore"
  71. class="width-80"
  72. size="small"
  73. :min="0.5"
  74. :max="500"
  75. :step="0.5"
  76. step-strictly
  77. :controls="false"
  78. ></el-input-number>
  79. </template>
  80. </el-table-column>
  81. <el-table-column class-name="action-column" label="操作" width="200px">
  82. <template slot-scope="scope">
  83. <el-button
  84. class="btn-primary"
  85. type="text"
  86. @click="toAddMain(scope.row)"
  87. >新增大题</el-button
  88. >
  89. <el-button
  90. class="btn-primary"
  91. type="text"
  92. @click="toAddSub(scope.row)"
  93. >新增小题</el-button
  94. >
  95. <el-button
  96. :disabled="tableData.length <= 1"
  97. class="btn-danger"
  98. type="text"
  99. @click="toDeleteSub(scope.row)"
  100. >删除</el-button
  101. >
  102. </template>
  103. </el-table-column>
  104. </el-table>
  105. <div class="total-info">
  106. 试卷总分:<span>{{ paperTotalScore }}</span
  107. >分
  108. </div>
  109. </div>
  110. </template>
  111. <script>
  112. import { calcSum } from "@/plugins/utils";
  113. export default {
  114. name: "mark-paper-structure",
  115. props: {
  116. datas: {
  117. type: Object,
  118. default() {
  119. return {
  120. basicPaperInfo: {},
  121. paperStructureInfo: [],
  122. groupInfo: []
  123. };
  124. }
  125. }
  126. },
  127. data() {
  128. return {
  129. tableData: [],
  130. Q_TYPE: {
  131. objective: "客观题",
  132. subjective: "主观题"
  133. }
  134. };
  135. },
  136. computed: {
  137. paperTotalScore() {
  138. return calcSum(this.tableData.map(item => item.totalScore || 0));
  139. }
  140. },
  141. mounted() {
  142. this.initData();
  143. },
  144. methods: {
  145. initData() {
  146. this.tableData = this.datas.paperStructureInfo.map(item => {
  147. return { ...item };
  148. });
  149. if (!this.tableData.length) {
  150. this.createMain();
  151. }
  152. this.$emit("on-ready");
  153. },
  154. createMain() {
  155. this.tableData.push({
  156. id: this.$randomCode(),
  157. qType: "objective",
  158. mainId: this.$randomCode(),
  159. mainTitle: "",
  160. mainNumber: 1,
  161. subNumber: 1,
  162. totalScore: 1,
  163. isMainFirstSub: true,
  164. expandSub: true
  165. });
  166. },
  167. getRowClassName({ row }) {
  168. let classNames = [];
  169. if (row.isMainFirstSub) {
  170. classNames.push("row-main-first-sub");
  171. }
  172. if (!row.isMainFirstSub && !row.expandSub) {
  173. classNames.push("row-unexpand-sub");
  174. }
  175. return classNames.join(" ");
  176. },
  177. getNextMainStartPos(startPos, curMainId) {
  178. let nextMainStartPos = null;
  179. for (let i = startPos, len = this.tableData.length; i < len; i++) {
  180. const element = this.tableData[i];
  181. if (element.mainId !== curMainId) {
  182. nextMainStartPos = i;
  183. return nextMainStartPos;
  184. }
  185. }
  186. if (nextMainStartPos === null) return this.tableData.length;
  187. },
  188. toAddMain(row) {
  189. const startPos = this.tableData.findIndex(item => item.id === row.id);
  190. let nextMainStartPos = this.getNextMainStartPos(startPos, row.mainId);
  191. this.tableData.splice(nextMainStartPos, 0, {
  192. id: this.$randomCode(),
  193. qType: row.qType,
  194. mainId: this.$randomCode(),
  195. mainTitle: "",
  196. mainNumber: row.mainNumber + 1,
  197. subNumber: 1,
  198. totalScore: 1,
  199. isMainFirstSub: true,
  200. expandSub: true
  201. });
  202. this.updateMainData();
  203. },
  204. updateMainData() {
  205. let curMainNumber = 0,
  206. curMainId = null;
  207. this.tableData.forEach(item => {
  208. if (item.mainId !== curMainId) {
  209. curMainId = item.mainId;
  210. curMainNumber++;
  211. }
  212. item.mainNumber = curMainNumber;
  213. });
  214. },
  215. toAddSub(row) {
  216. const subPos = this.tableData.findIndex(item => item.id === row.id);
  217. this.tableData.splice(subPos + 1, 0, {
  218. id: this.$randomCode(),
  219. qType: row.qType,
  220. mainId: row.mainId,
  221. mainTitle: row.mainTitle,
  222. mainNumber: row.mainNumber,
  223. subNumber: row.subNumber + 1,
  224. totalScore: 1,
  225. isMainFirstSub: false,
  226. expandSub: row.expandSub
  227. });
  228. this.updateSubData(row.mainId);
  229. },
  230. updateSubData(mainId) {
  231. this.tableData
  232. .filter(item => item.mainId === mainId)
  233. .forEach((item, index) => {
  234. item.subNumber = index + 1;
  235. });
  236. },
  237. toDeleteSub(row) {
  238. const subPos = this.tableData.findIndex(item => item.id === row.id);
  239. this.tableData.splice(subPos, 1);
  240. this.tableData
  241. .filter(item => item.mainId === row.mainId)
  242. .forEach((item, index) => {
  243. item.isMainFirstSub = !index;
  244. });
  245. this.updateSubData(row.mainId);
  246. this.updateMainData();
  247. },
  248. switchExpandSub(row) {
  249. row.expandSub = !row.expandSub;
  250. this.tableData
  251. .filter(item => item.mainId === row.mainId && !item.isMainFirstSub)
  252. .forEach(item => (item.expandSub = row.expandSub));
  253. },
  254. mainTitleChange(row) {
  255. this.tableData
  256. .filter(item => item.mainId === row.mainId && !item.isMainFirstSub)
  257. .forEach(item => (item.mainTitle = row.mainTitle));
  258. },
  259. qTypeChange(row) {
  260. this.tableData
  261. .filter(item => item.mainId === row.mainId && !item.isMainFirstSub)
  262. .forEach(item => (item.qType = row.qType));
  263. },
  264. checkData() {
  265. if (
  266. this.tableData.some(item => item.qType === "objective") &&
  267. this.tableData[0].qType !== "objective"
  268. ) {
  269. this.$message.error("请保持客观题在前,主观题在后!");
  270. return;
  271. }
  272. const objectiveNos = this.tableData
  273. .filter(item => item.isMainFirstSub && item.qType === "objective")
  274. .map(item => item.mainNumber);
  275. const unValid = objectiveNos.some(
  276. (no, index) => objectiveNos[0] + index !== no
  277. );
  278. if (unValid) {
  279. this.$message.error("请保持主客观题题号连续");
  280. return;
  281. }
  282. let errorMessages = [];
  283. this.tableData.forEach(item => {
  284. let errorMsg = ``;
  285. if (item.isMainFirstSub) {
  286. let errorFields = [];
  287. if (!item.mainTitle) {
  288. errorFields.push("大题名称");
  289. }
  290. if (!item.mainNumber) {
  291. errorFields.push("大题号");
  292. }
  293. if (errorFields.length) {
  294. errorMsg += `${errorFields.join("、")}不能为空,`;
  295. }
  296. }
  297. let errorFields = [];
  298. if (!item.subNumber) {
  299. errorFields.push("小题号");
  300. }
  301. if (!item.totalScore) {
  302. errorFields.push("小题满分");
  303. }
  304. if (errorFields.length) {
  305. errorMsg += `第${item.subNumber}小题,${errorFields.join(
  306. "、"
  307. )}不能为空,`;
  308. }
  309. if (errorMsg) {
  310. errorMsg = `第${item.mainNumber}大题,${errorMsg}`;
  311. errorMessages.push(errorMsg);
  312. }
  313. });
  314. if (errorMessages.length) {
  315. this.$message.error(errorMessages.join("。"));
  316. return;
  317. }
  318. this.$confirm(
  319. `当前试卷总分为${this.paperTotalScore}分, 确定要下一步吗?`,
  320. "提示",
  321. {
  322. type: "warning"
  323. }
  324. )
  325. .then(() => {
  326. this.updateData();
  327. this.$emit("next-step");
  328. })
  329. .catch(() => {});
  330. },
  331. getData() {
  332. return this.tableData.map(item => {
  333. return { ...item };
  334. });
  335. },
  336. updateData() {
  337. this.$emit("data-change", { paperStructureInfo: this.getData() });
  338. }
  339. }
  340. };
  341. </script>