123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- {extend name="common/common2" /}
- {block name="main"}
- <div class="row">
- <div class="col-sm-12">
- <div class="ibox float-e-margins">
- <div class="ibox-content">
- <form method="post" action="{:url('edit_question',['id'=>$question['id']])}" class="form-horizontal">
- <div class="form-group">
- <label class="col-sm-2 control-label">题干</label>
- <div class="col-sm-6">
- <textarea name="title" class="form-control" rows="3">{$question['title']}</textarea>
- </div>
- </div>
- <div class="form-group">
- <label class="col-sm-2 control-label">分值</label>
- <div class="col-sm-6">
- <select class="form-control" {if $info['status']==2} disabled {/if} name="score">
- {for start="1" end="50"}
- <option {if $question['score']==$i} selected {/if} value="{$i}">{$i}分</option>
- {/for}
- </select>
- </div>
- </div>
- <div class="form-group">
- <label class="col-sm-2 control-label">排序(正序)</label>
- <div class="col-sm-6">
- <input type="number" name="sort" min="0" {if $info['status']==2} disabled {/if} step="1" class="form-control" value="{$question['sort']}">
- </div>
- </div>
- <div id="vueapp">
- <div class="form-group">
- <label class="col-sm-2 control-label">类型</label>
- <div class="col-sm-6">
- <label class="cr-inline">
- <input type="radio" v-model="type" {if $info['status']==2} disabled {/if} {if $question['type']==0} checked {/if} name="type" value="0"> 单选
- </label>
- <label class="cr-inline">
- <input type="radio" v-model="type" {if $info['status']==2} disabled {/if} {if $question['type']==1} checked {/if} name="type" value="1"> 多选
- </label>
- </div>
- </div>
- <div class="form-group">
- <label class="col-sm-2 control-label">选项</label>
- {if $info['status']==0 || $info['status']==1}
- <a v-if="options.length < 8" @click="addOption" href="javascript:;">添加选项</a><br>
- {/if}
- <input type="hidden" id="datioptions" name="options" value="">
- <div class="col-sm-6">
- <table class="table table-bordered">
- <tr>
- <th>内容</th>
- <th>正确答案</th>
- {if $info['status']==0 || $info['status']==1}
- <th>操作</th>
- {/if}
- </tr>
- <tr v-for="(item, index) in options" :key="index">
- <td><input type="text" v-model="item.text" class="form-control input-sm"></td>
- <td width="100"><input type="checkbox" {if $info['status']==2} disabled {/if} v-model="item.answer" @click="changeAnswer(index)"></td>
- {if $info['status']==0 || $info['status']==1}
- <td width="100"><button type="button" v-if="options.length > 1" @click="delOption(index)">删除</button></td>
- {/if}
- </tr>
- </table>
- </div>
- </div>
- </div>
- <div class="hr-line-dashed"></div>
- {if $info['status']==2}
- <div class="form-group">
- <label class="col-sm-2 control-label"></label>
- <div class="col-sm-6">
- <div style="color: red;">说明:为了保持数据一致性,已发布过的试卷,试题只能修改文字</div>
- </div>
- </div>
- {/if}
- <div class="form-group">
- <div class="col-sm-6 col-sm-offset-2">
- <button class="btn btn-primary ajax-post" data-layer="1" target-form="form-horizontal" type="submit">确 定</button>
- <button class="btn cancel-btn btn-default" type="button">取 消</button>
- </div>
- </div>
- </form>
- </div>
- </div>
- </div>
- </div>
- {/block}
- {block name="script"}
- <script>
- var qoptions = {:json_encode($question["options"])};
- var options = [];
- for (let o in qoptions){
- options.push({
- "text":qoptions[o].text,
- "answer": qoptions[o].answer == '1'
- })
- }
- var vm = new Vue({
- el: '#vueapp',
- data: {
- options: options,
- initoption: {
- "text":'选项',
- "answer":false,
- },
- numToChar: ['A','B','C','D','E','F','G','H'],
- type: '{$question["type"]}',
- },
- computed: {
- // 计算属性的 getter
- resoptions: function () {
- return JSON.parse(JSON.stringify(this.options));
- }
- },
- watch: {
- resoptions: function (newval, oldval) {
- this.formatData();
- },
- type: function (newval, oldval) {
- console.log('type', this.type);
- // 多选改成单选是需判断正确答案是否存在多个,存在多个保留第一个
- if(this.type == '0'){
- let flag = false;
- for (let o in this.options){
- if(this.options[o].answer){
- if(!flag){
- flag = true;
- }else{
- this.options[o].answer = false;
- }
- }
- }
- }
- },
- },
- created: function () {
- this.formatData();
- },
- methods: {
- addOption() {
- this.options.push(JSON.parse(JSON.stringify(this.initoption)));
- },
- delOption(index) {
- let oldoptions = JSON.parse(JSON.stringify(this.options));
- let newoptions = [];
- for(let o in oldoptions){
- if(o != index){
- newoptions.push(oldoptions[o]);
- }
- }
- this.options = JSON.parse(JSON.stringify(newoptions));
- },
- changeAnswer(index){
- let that = this;
- setTimeout(function () {
- if(that.type == '0'){ // 单选正能选择一个正确答案
- let cflag = false;
- for(let o in that.options){
- if(o == index && that.options[o].answer){
- cflag = true;
- break;
- }
- }
- if(cflag){
- for(let o in that.options){
- if(o != index && that.options[o].answer){
- that.options[o].answer = false;
- }
- }
- }
- }
- },50);
- },
- formatData(){
- let ops = JSON.parse(JSON.stringify(this.options));
- let nops = [];
- for(let o in ops){
- nops.push({
- id: this.numToChar[o],
- text: ops[o].text,
- answer: ops[o].answer?'1':'0',
- })
- }
- console.log('ret',JSON.stringify(nops));
- $('#datioptions').val(JSON.stringify(nops));
- }
- }
- })
- </script>
- {/block}
|