动态填充jqgrid colModel

时间:2013-06-02 14:07:44

标签: jquery jqgrid

我在网上看到很少的好教程......但不知怎的,我无法申请或不知道如何应用它。

无论如何,这是我想做的事情:

用户将添加Start Week&结束一周

所以colModel将是:

  1. id,例如int_weekid
  2. 从开始周和结束周产生,例如第2周到第5周
  3. colModel将是> int_weekid |第2周|第3周|第4周| week5

    我尝试这样做,但有些事情是错误的。有人可以帮我吗?请@oleg帮助。

    jQuery(document).ready(function(){
    
      var start = $("#weekstart").val();
      var end = $("#weekend").val();
      var lastsel2;  
      var j=0;
    
      var ColModel1 = [
        {name:'ID',index:'int_userid', hidden:false, align:'center', width:30, search:false}, 
    
        for (j=start;j<=end;j++) {
          {name:'WEEK"+j+"',index:'WEEK"+j+"', align:'center', width:30, search:false},;
        }
      ];
    
      jQuery("#grn_transac").jqGrid({
        url:'transaction/grn_transacdata2.php',
        datatype: "json",
        colModel: ColModel1,
        pager: '#pager', //pagination enable
        rowNum:30,
        rowList:[10,20,30],
        width:950,
        height: 'auto',
        //sortname: 'int_weekid',
        sortorder: 'DESC',
      }); /* end of jqgrid */
    
    }); 
    

    更新

    在动态成功添加colModel后,我发现了一个奇怪的问题...... 假设我有一个2012年的50周,当我把start = 1和end = 50没有问题一切看起来都很棒..但不知何故,当我键入start = 4(单个数字)和结束= 18(双数字号码)除了CODE和&amp;之外,我没有为我的周colModel得到任何东西。网站..我通常不会遇到任何问题,如果它开始= 1或开始和结束都是单个数字或两者都是双数..下面是我的代码..希望有人可以帮我这个.. @owlwark,@ oleg

    <input name="mula" type="text" id="mula" />
    <input name="akhir" type="text" id="akhir" />
    <input name="btn_cons" type="button" id="btn_cons" value="SUBMIT" />
    
    <script type="text/javascript">  
    <!-- Connected Consumers Trend -->
    
    jQuery(document).ready(function(){
    
      $("#btn_cons").click(function(){
      $("#production").jqGrid('GridUnload');
        var mula = $("#mula").val();
        var akhir = $("#akhir").val(); 
        var projek = 21;
        var tahun = 2013; 
    
        var ColModel1 = [];
     ColModel1.push({name:'CODE',index:'txt_site_code', hidden:false, align:'center', width:70 });
     ColModel1.push({name:'SITE',index:'txt_site_name', hidden:false, align:'left', width:190 });  
    
         for (var j = mula; j<=akhir; j++) {
         ColModel1.push({name:'WEEK'+j,index:'WEEK'+j, align:'center', width:60 });
          }   
    
        var lastsel2;   
        jQuery("#production").jqGrid({
          url:'dash/production/call_data.php?start='+mula +'&end='+akhir +'&project='+projek +'&year='+tahun,
          datatype: "json",
          colModel: ColModel1,
          pager: '#pager', //pagination enable
          rowNum:30,
          rowList:[10,20,30],
          width:1000,
          height: 'auto',
          shrinkToFit:false,
          //sortname: 'int_userid',
          sortorder: 'DESC',
          hidegrid: false,  //show/hide  grid button on caption header
          viewrecords: true, //display the number of total records
         // editurl:"transaction/grnedit.php",
          loadtext: "Loading Data, Please Wait...",
          rownumbers:true, // add row numbers on left side
          caption: '&nbsp; Consumer',
    
        }); /* end of jqgrid */
    
     }); //end btn_cons           
    });  /*end of document ready*/
        </script>
    
    <h3>CONSUMER</h3>&nbsp;
      <div id="prodgrid" class="hiddenDiv" align="center">
      <table id="production" class="scroll" cellpadding="0" cellspacing="0"></table>
      <div id="pager" class="scroll" style="text-align:center;"></div>
      </div>   &nbsp;&nbsp;
    

    这是我的数据文件call_data.php,我在生成页面总数等方面也有问题。:

    <?php //MARTIN
    require_once('../../Connections/myconn.php');
    
    $start   = $_REQUEST['start']; 
    $end     = $_REQUEST['end']; 
    $project = $_REQUEST['project']; 
    $year    = $_REQUEST['year']; 
    
    $query = mysql_query(' CALL martin ('.$start.', '.$end.', '.$year.', '.$project.');');
    
    $i=0;
    $j=0;
    
    
    while($row = mysql_fetch_array($query,MYSQL_ASSOC)) {
        $rows[$i]['id']=$row[txt_site_code]; 
         $rows[$i]['cell']=array($row[txt_site_code],$row[txt_site_name]);
    
          for ($j=$start; $j<=$end;$j++) {
          array_push($rows[$i]['cell'], $row['WEEK'.$j]);
          }
        $i++;
    }
    
     //this part I dunno how to create it dynamically 
    echo '{
       "page": "1", 
       "total": "1", 
        "records": "1",
        "rows" : ';
    
    echo json_encode($rows); 
    
    echo '}'; 
    
    ?>
    

1 个答案:

答案 0 :(得分:2)

因为Owlvark说你不应该在阵列中做一个。

尝试这样的事情:

var ColModel1 = [];
ColModel1.push({name:'ID',index:'int_userid', hidden:false, align:'center', width:30, search:false});
for (j=start;j<=end;j++) {
    ColModel1.push({name:'WEEK"+j+"',index:'WEEK"+j+"', align:'center', width:30, search:false});
}