2013-03-26 55 views
1

我工作在extjs + yii。我有看法原样在extjs如何使验证json

Ext.define('Balaee.view.qb.qbqns.QbqnsView', 
{ 
     extend:'Ext.view.View', 
     id:'qbqnsViewId', 
     alias:'widget.QbqnsView', 
     store:'qb.QbqnsStore', 
     autoScroll: true, 
     config: 
     { 
      tpl:'<tpl for=".">'+ 
       '<div id="main">'+ 
       '</br>'+ 
      // '<b>Question :-</b> {pollQuestion}</br>'+ 
       '<h1 id="q">Question :-</h1> {question} </br>'+ 

       '<tpl for="options">'+  // interrogate the kids property within the data 
       '<p>&nbsp&nbsp<input type="radio" name="{parent.questionId}" value="{optionId}">&nbsp{option}</p>'+ 
       //'<p>&nbsp&nbsp<input type="radio" name="{questionId}">&nbsp{option}</p>'+ 
       '</tpl></p>'+ 
       '<p>---------------------------------------------------------</p>'+ 
       '</div>'+ 
       '</tpl>', 
      itemSelector:'div.main' 
     } 
});// End of login class 

这种观点是选择答案,并提交按钮我想送Questionid和其选定的单选按钮选项点击后显示问题及其相关options.Now。随着数据以json的格式传播,我捕获了这些选定的单选按钮值,并将jsons作为参数传递给jsons。所以在控制器我写代码为 -

var answers = '{"data":['; 
     var i=0; 
     QbqnsStore.each(function(model){ 
      i++; 
      var inputs = document.getElementsByName(model.get('questionId')); 
      console.log(document.getElementsByName(model.get('questionId'))); 
      console.log("length is"+inputs.length); 
      for (var j = 0; j < inputs.length; j++) { 
       if (inputs[j].checked) { 
        console.log("count of store is"+QbqnsStore.count()); 
        //if (i == QbqnsStore.count()){ 
        if (i == QbqnsStore.count()){ 
         console.log("value of i is"+i); 
         answers = answers + '{"paperId":"'+paperNumber+'","userId":"'+userId+'","questionId":"'+inputs[j].name+'","option":'+inputs[j].value+'}' 
        //i=1; 
        } 
        else{ 
         console.log("value of i is"+i); 
         answers = answers + '{"paperId":"'+paperNumber+'","userId":"'+userId+'","questionId":"'+inputs[j].name+'","option":'+inputs[j].value+'},' 
        } 
       } 
      }// End of inner for loop 
     }); //End of each 

     answers =answers+']}'; 
     console.log("selected data is:"); 
     console.log(answers); 
     var storeObject=this.getStore('qb.QbquestionoptionStore'); 
     storeObject.load({ 

      params:{ 
       data: answers 
      }, 
      callback: function(records,operation,success){ 

      }, 
      scope:this 
     }); 

它在用户解决所有问题时正常工作。在解决所有问题的情况下,以上功能正在形成json,如下所示:

{"data":[{"paperId":"1517","userId":"116","questionId":"1","option":1},{"paperId":"1517","userId":"116","questionId":"2","option":4},{"paperId":"1517","userId":"116","questionId":"3","option":9},{"paperId":"1517","userId":"116","questionId":"4","option":9}]} 

它的格式正确。但是,当用户只解决从纸第一个或两个问题,那么它的形成JSON原样

{"data":[{"paperId":"1518","userId":"116","questionId":"2","option":4},]} 

即额外的逗号得到插入JSON这使得无效的JSON结束。所以当用户解决时,如何从json中删除这些额外的逗号,只能启动一个或两个问题。请指引我

回答

0

我觉得你的条件是不正确的。您是否尝试过:的

if (i == QbqnsStore.count() - 1){ 

代替

if (i == QbqnsStore.count()){