article_add.html 7.6 KB

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