jqGrid不显示数据,没有错误

时间:2011-02-12 17:28:02

标签: javascript jquery jqgrid

好的,这主要适用于调用我的PHP操作并从查询中返回JSON数据就好了。问题是jqGrid不会显示数据。我错过了什么?我已经阅读了这里和其他地方关于我能找到的主题的每篇文章。这看起来应该有效。

HTML:

<table id="dsisBaseTable"></table>
<div id=dsisBasePager"></div>

这是我的网格定义(整个JavaScript函数):

function runGridBaseReport() {
    $('#DSISBaseReportResult').html('<table id="dsisBaseTable"></table>' +
        '<div id="dsisBasePager"></div> ');
    $('#DSISBusy').fadeIn();
    var loc = $('#dsisLocation').val();
    var f = (loc == '') ? '' : loc.substring(0, 1);
    if (f.match(/[A-Z]/) || f.match(/[a-z]/))
    loc = loc.substring(1);

    var dsisParams = {
        mode : 'basereport',
        dsisLocation : loc,
        dsisGuild : $('#dsisGuild').val(),
        dsisNick : $('#dsisNick').val(),
        dsisOccGuild : $('#dsisOccGuild').val(),
        dsisOccNick : $('#dsisOccNick').val(),
        dsisMinLevel : $('#dsisMinLevel').val(),
        dsisMaxLevel : $('#dsisMaxLevel').val(),
        dsisEconomy : $('#dsisEconomy').val(),
        dsisDt : $('#dsisDt').val(),
        dsisDs : $('#dsisDs').val(),
        dsisPr : $('#dsisPr').val(),
        dsisPs : $('#dsisPs').val(),
        dsisMinFleet : $('#dsisMinFleet').val(),
        dsisMaxFleet : $('#dsisMaxFleet').val(),
        dsisCapitol : $('#dsisCapitol').is(':checked'),
        dsisWormHole : $('#dsisWormHole').is(':checked'),
        dsisAeFormat : $('#dsisAeFormat').is(':checked')
        };
    var data = { data: jsonString(dsisParams)};

    $("#dsisBaseTable").jqGrid({
        url: indexUrl + '/basereport',
        datatype: 'json',
        postData: data,
        mtype: 'POST',
        colNames:['Name',
                  'Location',
                  'WH',
                  'Inc',
                  'Eco',
                  'Guild',
                  'Owner',
                  'Level',
                  'Guild',
                  'OccBy',
                  'JG(Log)',
                  'CC',
                  'PH',
                  'DS',
                  'DT',
                  'PS',
                  'PR',
                  'DB',
                  'Fleet',
                  'Days',
                  'Updated By'],
        colModel :[
          {name:'baseName',
            index:'baseName',
            sortable: false,
            width: 50,
            jsonmap: 'row.baseName'},
          {name:'location',
            index:'location',
            width: 50,
            jsonmap: 'row.location'},
          {name:'wh',
            index:'wh',
            width: 50,
            jsonmap: 'row.wormhole'},
          {name:'inc',
            index:'inc',
            align:'right',
            width: 50,
            jsonmap: 'row.income'},
          {name:'eco',
            index:'eco',
            align:'right',
            width: 50,
            jsonmap: 'row.economy'},
          {name:'ownerGuild',
            index:'ownerGuild',
            width: 50,
            jsonmap: 'row.ownerGuild'},
          {name:'ownerNick',
            index:'ownerNick',
            width: 50,
            jsonmap: 'row.ownerNick'},
          {name:'ownerLevel',
            index:'ownerLevel',
            width: 50,
            jsonmap: 'row.ownerLevel'},
          {name:'occGuild',
            index:'occGuild',
            width: 50,
            jsonmap: 'row.occGuild'},
          {name:'occNick',
            index:'occNick',
            width: 50,
            jsonmap: 'row.occNick'},
          {name:'jumpGate',
            index:'jumpGate',
            width: 50,
            jsonmap: 'row.jumpGate'},
          {name:'commandCenter',
            index:'commandCenter',
            width: 50,
            jsonmap: 'row.commandCenter'},
          {name:'photons',
            index:'photons',
            width: 50,
            jsonmap: 'row.photons'},
          {name:'dshield',
            index:'dshields',
            width: 50,
            jsonmap: 'row.dshields'},
          {name:'disruptors',
            index:'disruptors',
            width: 50,
            jsonmap: 'row.disruptors'},
          {name:'pshields',
            index:'pshields',
            width: 50,
            jsonmap: 'row.pshields'},
          {name:'prings',
            index:'prings',
            width: 50,
            jsonmap: 'row.prings'},
          {name:'debris',
            index:'debris',
            width: 50,
            jsonmap: 'row.debris'},
          {name:'fleetSize',
            index:'fleetSize',
            width: 50,
            jsonmap: 'row.fleetSize'},
          {name:'daysOld',
            index:'daysOld',
            width: 50,
            jsonmap: 'row.daysOld'},
          {name:'updatedBy',
            index:'updatedBy',
            sortable: false,
            width: 50,
            jsonmap: 'row.updatedBy'}
        ],
        pager: $('#dsisBasePager'),
        rowNum: $('#basePageSize').val(),
        rowList:[20,30,40,50],
        sortname: 'location',
        sortorder: 'asc',
        viewrecords: true,
        caption: 'Base Report',
        width: 1024,
        height: 300,
        jsonReader : {
            root: "rows",
            page: "page",
            total: "total",
            records: "records",
            repeatitems: false,
            id: "id"
        }
    });
    /*    pages'   => round($totRows/$dsisPageSize),
         'page'    => $dsisPage,
         'total'   => $numRows/$dsisPage,
         'records' => $totRows,
         'rows' */
    $('#DSISBusy').fadeOut();
    $('#DSISBaseReportResult').fadeIn();
    return false;
}

这是我处理请求并返回结果的方式:

public function basereportAction()
{
    try
    {
      $params = $this->_getAllParams();
      //Log::logErr('params:'.print_r($params, true));

      $data = $this->_getParam('data');
      //Log::logErr('Data:'.print_r($data, true));

      $reportParams->request = json_decode($data);
      //Log::logErr('DSIS Obj:'.print_r($obj, true));

      $reportParams->request->dsisPage = $this->_getParam('page');
      $reportParams->request->dsisPageSize = $this->_getParam('rows');
      $reportParams->request->dsisOrderBy = $this->_getParam('sidx');
      $reportParams->request->dsisSortDir = $this->_getParam('sord');

      $base = new Base();
      // $result is a PHP array
      $result = $base->outputDSISReportHtml($reportParams, true);

      $js = json_encode($result);
      Log::logErr('JSON:'.$js);

      $callback = $this->_getParam('callback');

      echo $js;
    }
    catch( Exception $e)
    {
        Log::logErr('DSISReportController::basereport:Error:'.$e->getMessage());
        throw $e;
    }
}

这是我的数据:

{
    "total": 0,
    "page": "1",
    "records": 1,
    "rows": [
        {
            "id": 0,
            "row": {
                "baseName": "Stratos",
                "location": "J15:32:53:41",
                "wormhole": "",
                "income": 78,
                "economy": 78,
                "ownerGuild": "CDA",
                "ownerNick": "D-Day 9000",
                "ownerLevel": "17.14",
                "occGuild": "",
                "occNick": "",
                "jumpGate": 0,
                "commandCenter": 1,
                "photons": "0",
                "dshields": "0",
                "disruptors": "0",
                "pshields": "0",
                "prings": "0",
                "debris": 0,
                "fleetSize": "22250",
                "daysOld": 11,
                "updatedBy": 3057
            }
        }
    ]
}

2 个答案:

答案 0 :(得分:1)

我在JSON数据末尾看到的唯一错误以“},]}”结尾。您应该在']'之前删除逗号。

在进行最小化更改后,能够重现您的测试示例your demo的工作没有任何明显的问题。

更新: The same demo with new JSON data也没有任何问题。我想错误是在你没有在这里发布的代码部分。例如,我将您的代码放在jQuery(document).ready(function () {/*the code*/});中。我必须删除postData参数,因为您在代码中使用的值data未定义。出于同样的原因,我必须更改indexUrl参数中使用的url变量。参数rowNum:$('#basePageSize').val()看起来也很奇怪,我将其替换为rowNum:20。如果您可以将网址发布到工作(非工作)原始解决方案或真正的完整HTML和JavaScript代码,那么可能会对您有所帮助。

答案 1 :(得分:1)

我遇到了同样的问题。展出的jqgrid不返回XML。我注意到注册表数据库有一些特殊字符,如“&amp;”。问题是浏览器。通过将CDATA标记字段以XML格式返回来决定如下:

sb.Append ("<cell>"); sb.Append ("<! [CDATA [" + g.Name + "]"); sb.Append ("]> </ cell>");