edit_question.html 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. {extend name="common/common2" /}
  2. {block name="main"}
  3. <div class="row">
  4. <div class="col-sm-12">
  5. <div class="ibox float-e-margins">
  6. <div class="ibox-content">
  7. <form method="post" action="{:url('edit_question',['id'=>$question['id']])}" class="form-horizontal">
  8. <div class="form-group">
  9. <label class="col-sm-2 control-label">题干</label>
  10. <div class="col-sm-6">
  11. <textarea name="title" class="form-control" rows="3">{$question['title']}</textarea>
  12. </div>
  13. </div>
  14. <div class="form-group">
  15. <label class="col-sm-2 control-label">分值</label>
  16. <div class="col-sm-6">
  17. <select class="form-control" {if $info['status']==2} disabled {/if} name="score">
  18. {for start="1" end="50"}
  19. <option {if $question['score']==$i} selected {/if} value="{$i}">{$i}分</option>
  20. {/for}
  21. </select>
  22. </div>
  23. </div>
  24. <div class="form-group">
  25. <label class="col-sm-2 control-label">排序(正序)</label>
  26. <div class="col-sm-6">
  27. <input type="number" name="sort" min="0" {if $info['status']==2} disabled {/if} step="1" class="form-control" value="{$question['sort']}">
  28. </div>
  29. </div>
  30. <div id="vueapp">
  31. <div class="form-group">
  32. <label class="col-sm-2 control-label">类型</label>
  33. <div class="col-sm-6">
  34. <label class="cr-inline">
  35. <input type="radio" v-model="type" {if $info['status']==2} disabled {/if} {if $question['type']==0} checked {/if} name="type" value="0"> 单选
  36. </label>
  37. <label class="cr-inline">
  38. <input type="radio" v-model="type" {if $info['status']==2} disabled {/if} {if $question['type']==1} checked {/if} name="type" value="1"> 多选
  39. </label>
  40. </div>
  41. </div>
  42. <div class="form-group">
  43. <label class="col-sm-2 control-label">选项</label>
  44. {if $info['status']==0 || $info['status']==1}
  45. <a v-if="options.length < 8" @click="addOption" href="javascript:;">添加选项</a><br>
  46. {/if}
  47. <input type="hidden" id="datioptions" name="options" value="">
  48. <div class="col-sm-6">
  49. <table class="table table-bordered">
  50. <tr>
  51. <th>内容</th>
  52. <th>正确答案</th>
  53. {if $info['status']==0 || $info['status']==1}
  54. <th>操作</th>
  55. {/if}
  56. </tr>
  57. <tr v-for="(item, index) in options" :key="index">
  58. <td><input type="text" v-model="item.text" class="form-control input-sm"></td>
  59. <td width="100"><input type="checkbox" {if $info['status']==2} disabled {/if} v-model="item.answer" @click="changeAnswer(index)"></td>
  60. {if $info['status']==0 || $info['status']==1}
  61. <td width="100"><button type="button" v-if="options.length > 1" @click="delOption(index)">删除</button></td>
  62. {/if}
  63. </tr>
  64. </table>
  65. </div>
  66. </div>
  67. </div>
  68. <div class="hr-line-dashed"></div>
  69. {if $info['status']==2}
  70. <div class="form-group">
  71. <label class="col-sm-2 control-label"></label>
  72. <div class="col-sm-6">
  73. <div style="color: red;">说明:为了保持数据一致性,已发布过的试卷,试题只能修改文字</div>
  74. </div>
  75. </div>
  76. {/if}
  77. <div class="form-group">
  78. <div class="col-sm-6 col-sm-offset-2">
  79. <button class="btn btn-primary ajax-post" data-layer="1" target-form="form-horizontal" type="submit">确 定</button>
  80. <button class="btn cancel-btn btn-default" type="button">取 消</button>
  81. </div>
  82. </div>
  83. </form>
  84. </div>
  85. </div>
  86. </div>
  87. </div>
  88. {/block}
  89. {block name="script"}
  90. <script>
  91. var qoptions = {:json_encode($question["options"])};
  92. var options = [];
  93. for (let o in qoptions){
  94. options.push({
  95. "text":qoptions[o].text,
  96. "answer": qoptions[o].answer == '1'
  97. })
  98. }
  99. var vm = new Vue({
  100. el: '#vueapp',
  101. data: {
  102. options: options,
  103. initoption: {
  104. "text":'选项',
  105. "answer":false,
  106. },
  107. numToChar: ['A','B','C','D','E','F','G','H'],
  108. type: '{$question["type"]}',
  109. },
  110. computed: {
  111. // 计算属性的 getter
  112. resoptions: function () {
  113. return JSON.parse(JSON.stringify(this.options));
  114. }
  115. },
  116. watch: {
  117. resoptions: function (newval, oldval) {
  118. this.formatData();
  119. },
  120. type: function (newval, oldval) {
  121. console.log('type', this.type);
  122. // 多选改成单选是需判断正确答案是否存在多个,存在多个保留第一个
  123. if(this.type == '0'){
  124. let flag = false;
  125. for (let o in this.options){
  126. if(this.options[o].answer){
  127. if(!flag){
  128. flag = true;
  129. }else{
  130. this.options[o].answer = false;
  131. }
  132. }
  133. }
  134. }
  135. },
  136. },
  137. created: function () {
  138. this.formatData();
  139. },
  140. methods: {
  141. addOption() {
  142. this.options.push(JSON.parse(JSON.stringify(this.initoption)));
  143. },
  144. delOption(index) {
  145. let oldoptions = JSON.parse(JSON.stringify(this.options));
  146. let newoptions = [];
  147. for(let o in oldoptions){
  148. if(o != index){
  149. newoptions.push(oldoptions[o]);
  150. }
  151. }
  152. this.options = JSON.parse(JSON.stringify(newoptions));
  153. },
  154. changeAnswer(index){
  155. let that = this;
  156. setTimeout(function () {
  157. if(that.type == '0'){ // 单选正能选择一个正确答案
  158. let cflag = false;
  159. for(let o in that.options){
  160. if(o == index && that.options[o].answer){
  161. cflag = true;
  162. break;
  163. }
  164. }
  165. if(cflag){
  166. for(let o in that.options){
  167. if(o != index && that.options[o].answer){
  168. that.options[o].answer = false;
  169. }
  170. }
  171. }
  172. }
  173. },50);
  174. },
  175. formatData(){
  176. let ops = JSON.parse(JSON.stringify(this.options));
  177. let nops = [];
  178. for(let o in ops){
  179. nops.push({
  180. id: this.numToChar[o],
  181. text: ops[o].text,
  182. answer: ops[o].answer?'1':'0',
  183. })
  184. }
  185. console.log('ret',JSON.stringify(nops));
  186. $('#datioptions').val(JSON.stringify(nops));
  187. }
  188. }
  189. })
  190. </script>
  191. {/block}