question_mixin.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import ImageUpload from '@/components/file-upload'
  2. export default {
  3. components: {
  4. ImageUpload
  5. },
  6. data() {
  7. return {
  8. formData: {
  9. name: "",
  10. type: "SINGLE",
  11. options: [],
  12. },
  13. formDialog: false,
  14. formUpBool: true,
  15. }
  16. },
  17. watch: {
  18. "formData.type"(newVal, oldVal) {
  19. if (!this.formUpBool) {
  20. return
  21. }
  22. if (!!~["PICK"].indexOf(this.formData.type)) {
  23. this.formData.options = [
  24. {
  25. option_name: "A",
  26. option_value: "正确",
  27. option_files: []
  28. },
  29. {
  30. option_name: "B",
  31. option_value: "错误",
  32. option_files: []
  33. }
  34. ]
  35. } else if (oldVal === "PICK") {
  36. this.formData.options = []
  37. }
  38. },
  39. },
  40. computed: {
  41. formItems() {
  42. return [{
  43. md: 24,
  44. isShow: true,
  45. name: 'el-input',
  46. attributes: { placeholder: '请输入' },
  47. formItemAttributes: {
  48. label: '名称',
  49. prop: 'name',
  50. rules: []
  51. },
  52. },
  53. {
  54. md: 24,
  55. isShow: true,
  56. name: 'el-radio',
  57. options: [{ label: "单选题", value: "SINGLE" }, { label: "多选题", value: "MULTI" }, { label: "判断题", value: "PICK" }],
  58. attributes: { filterable: true, placeholder: '请选择', disabled: this.formData.id ? true : false },
  59. formItemAttributes: {
  60. label: '类型',
  61. prop: 'type',
  62. rules: [{ required: true, message: '请选择', trigger: 'blur' }]
  63. }
  64. },
  65. {
  66. md: 24,
  67. isShow: true,
  68. name: 'el-radio',
  69. options: [{ label: "是", value: "1" }, { label: "否", value: "0" }],
  70. attributes: { filterable: true, placeholder: '请选择', disabled: this.formData.id ? true : false },
  71. formItemAttributes: {
  72. label: '是否必填',
  73. prop: 'type',
  74. rules: [{ required: true, message: '请选择', trigger: 'blur' }]
  75. }
  76. },
  77. ...(() => {
  78. if (!!~["SINGLE", "MULTI"].indexOf(this.formData.type)) {
  79. // 单选,多选
  80. return [{
  81. md: 24,
  82. isShow: true,
  83. name: 'slot-component',
  84. attributes: { placeholder: '请输入' },
  85. formItemAttributes: {
  86. label: '试题选项',
  87. prop: 'options',
  88. rules: [{ required: true, message: '请添加选项', trigger: 'blur' }]
  89. },
  90. render: (h, { props, onInput }) => {
  91. var { value } = props
  92. return (
  93. <div>
  94. <div>
  95. <el-button size="mini" onClick={() => {
  96. this.formData.options.push({
  97. option_name: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "N", "M", "O", "P", "Q"][this.formData.options.length],
  98. option_value: "",
  99. option_files: []
  100. })
  101. }} type="primary">添加</el-button>
  102. </div>
  103. {this.formData.options.map((item, index) => {
  104. return (
  105. <div>
  106. <div style="display: flex;justify-content: space-between;">
  107. <span style="font-weight: bold;">选项:{this.formData.options[index].option_name}</span>
  108. <span style="color: red;" onClick={() => {
  109. this.formData.options.splice(index, 1)
  110. }}>删除</span>
  111. </div>
  112. <div>
  113. <el-input type="textarea" rows={2} placeholder="请输入内容" value={this.formData.options[index].option_value} onInput={(val) => { this.formData.options[index].option_value = val }}></el-input>
  114. </div>
  115. <br />
  116. <div>
  117. <ImageUpload fileList={this.formData.options[index].option_files} uid={`questionFiles_ImageUpload_${index}`} limit={3} isUpdate={false} />
  118. </div>
  119. <br />
  120. </div>
  121. )
  122. })}
  123. </div>
  124. )
  125. }
  126. }]
  127. }else if(!!~["PICK"].indexOf(this.formData.type)){
  128. // 填写
  129. return [{
  130. md: 24,
  131. isShow: true,
  132. name: 'slot-component',
  133. attributes: { placeholder: '请输入' },
  134. formItemAttributes: {
  135. label: '试题选项',
  136. prop: 'options',
  137. rules: [{ required: true, message: '请添加选项', trigger: 'blur' }]
  138. },
  139. render: (h, { props, onInput }) => {
  140. var { value } = props
  141. return (
  142. <div>
  143. {this.formData.options.map((item, index) => {
  144. return (
  145. <div>
  146. <div style="display: flex;justify-content: space-between;">
  147. <span style="font-weight: bold;">选项:{this.formData.options[index].option_name}</span>
  148. </div>
  149. <div>
  150. <el-input type="textarea" rows={2} placeholder="请输入内容" value={this.formData.options[index].option_value} onInput={(val) => { this.formData.options[index].option_value = val }}></el-input>
  151. </div>
  152. </div>
  153. )
  154. })}
  155. </div>
  156. )
  157. }
  158. }]
  159. }
  160. return []
  161. })(),
  162. ]
  163. },
  164. },
  165. methods: {
  166. // 打开弹窗
  167. openSubjectDialog() {
  168. this.formDialog = true
  169. this.$nextTick(() => {
  170. this.formUpBool = true
  171. })
  172. },
  173. formCancel() {
  174. this.$refs?.formRef?.$refs.inlineForm.clearValidate()
  175. this.$data.formData = this.$options.data().formData
  176. this.formDialog = false
  177. },
  178. formConfirm() {
  179. this.$refs.formRef.validate((valid, invalidFields, errLabels) => {
  180. if (valid) {
  181. var data = {
  182. ...this.formData,
  183. options: JSON.stringify(this.formData.options),
  184. };
  185. // questionSave(data).then(res => {
  186. // this.$message({
  187. // type: 'success',
  188. // message: '保存成功!'
  189. // })
  190. // this.formCancel()
  191. // this.$refs.pageRef.refreshList()
  192. // this.$emit("fujipagelist")
  193. // })
  194. }
  195. })
  196. },
  197. fanzhuanjiexi(row, cb) {
  198. // questionDetail({ id: row.id }).then(res => {
  199. // Object.assign(this.formData, res.data, {
  200. // options: JSON.parse(res.data.options),
  201. // })
  202. // cb && cb()
  203. // })
  204. },
  205. }
  206. }