1

csvファイルをjqgridに表示したいので、csvファイルを読み込んでJSON形式に変換するphpスクリプトを書きます。ajax を呼び出して php からデータを取得すると、「colNames の長さ <> colModel!」というエラーが表示されます。

Jクエリ:

    $(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"]}]}

この問題を解決するために誰か助けてください。

4

0 に答える 0