动态ColModel Jquery中的错误

时间:2013-09-10 06:35:40

标签: php jquery jqgrid

我想将csv文件显示到jqgrid中,所以我编写了一个php脚本来读取csv文件并转换为JSON格式。调用ajax从php中获取数据显示错误     " colNames的长度<> colModel"!

Jquery:

    $(function () {
$('#ViewFile').click(function(){
$('#dataview').show();
    var ColN, ColM, ColD;
   jQuery.ajax({
        url: 'CSV2JSON.php',
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        data: "",
        success: function (data, st) {
            if (st == "success") {
    var response = $.parseJSON(data);
                ColN = response.columns;//jqgrid heading data
                ColM = response.colModel; // its column model
                ColD = data; // Data
                createGrid();
            }
        },
        error: function () {
            alert("Error with AJAX callback");
        }
    });

    function createGrid() {
        $("#dataview").jqGrid({
            url:'CSV2JSON.php',
            datatype: "json",
            mtype: 'POST',
            ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
            serializeGridData: function (postData) {
                return JSON.stringify(postData);
            },
            colNames: ColN,//["NO.","Genotype-name","YIELD","DF","DM"],
            colModel: ColM,//[{name:'SNO',index:'SNO',align:'center', width:50, search:false, sortable:true},{name:'GENOTYPE',index:'GENOTYPE',align:'center', width:50, search:false},{name:'YIELD',index:'YIELD',align:'center', width:50, search:false},{name:'DF',index:'DF',align:'center', width:50, search:false},{name:'DM',index:'DM',align:'center', width:50, search:false},],
            loadonce: true,
            pager: jQuery('#pager'),
            rowNum: 5,
            rowList: [5, 10, 20, 50],
            viewrecords: true,
    pgbuttons: true,
    sortorder: 'desc',
        caption: 'Data-file'
        })

jQuery("#dataview").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false }, null, null, true, { multipleSearch: true });
    }
});
});

PHP:

<?php
$limit=50;
$page=1;
if (($handle = fopen('/home/dadu/Desktop/del.txt', 'r')) === false) {
die('Error opening file');
}
$headers = fgetcsv($handle,  ',');
$complete = array();
$lines = count(file('/home/dadu/Desktop/del.txt'));
$total_pages = ceil($lines/$limit);
$responce->total = $total_pages;
$responce->page = $page;
$responce->records = $lines;
$i=0;
$responce->columns=$headers;
$responce->colModel='[';
foreach($headers as $val){
$responce->colModel.='{name:\''.$val.'\',index:\''.$val.'\',align:\'center\', width:30, search:false,sortable:true},';
}
$responce->colModel.=']';
while ($row = fgetcsv($handle, ',')) {
$responce->rows[$i]['id']=$i;
$responce->rows[$i]['cell'] = $row;
$i++;
}
fclose($handle);
echo json_encode($responce);

PHP脚本的结果:

{"total":1,"page":1,"records":11,"columns":["SNO","GENOTYPE","YIELD","DF","DM"],"colModel":"[{name:'SNO',index:'SNO',align:'center', width:30, search:false,sortable:true},{name:'GENOTYPE',index:'GENOTYPE',align:'center', width:30, search:false,sortable:true},{name:'YIELD',index:'YIELD',align:'center', width:30, search:false,sortable:true},{name:'DF',index:'DF',align:'center', width:30, search:false,sortable:true},{name:'DM',index:'DM',align:'center', width:30, search:false,sortable:true},]","rows":[{"id":0,"cell":["1","","","",""]},{"id":1,"cell":["2","Z008E0002","88.6","59","106"]},{"id":2,"cell":["3","Z008E0003","89.27","50","124"]},{"id":3,"cell":["4","Z008E0004","86.38","68","112"]},{"id":4,"cell":["5","Z008E0005","80.74","84","114"]},{"id":5,"cell":["6","Z008E0006","80.96","75","115"]},{"id":6,"cell":["7","Z008E0007","83.73","74","128"]},{"id":7,"cell":["8","Z008E0008","82.5","63","125"]},{"id":8,"cell":["9","Z008E0009","83.1","75","120"]},{"id":9,"cell":["10","Z008E0010","83.36","76","123"]}]}

请任何人帮我解决这个问题。

0 个答案:

没有答案