前端和后端之间的通信错误

问题描述 投票:0回答:1

由于某种原因,我的jqGrid插件的前端(HTML)无法与插件的后端(PHP)正常通信。 我现在已经花了五个小时了,对此我什么也没显示。 我已经尝试过XML和jSon方法。 我感觉我正在使用一种过时的后端方法; 但是,我不确定,因为找不到太多的文档。 我将在这里睡着写代码,因为我真的睡着了,并且将尽快检查更新(快速小睡)。

前端

<?php 
require_once 'tabs.php';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>jQuery jqGrid Demonstration</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" media="screen" href="themes/redmond/jquery-ui-1.8.2.custom.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="themes/ui.jqgrid.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="themes/ui.multiselect.css" />

    <script src="js/jquery.js" type="text/javascript"></script>
    <script src="js/jquery-ui-custom.min.js" type="text/javascript"></script>
    <script src="js/i18n/grid.locale-en.js" type="text/javascript" ></script>

    <script type="text/javascript">
        $.jgrid.no_legacy_api = false;
        $.jgrid.useJSON = true;
    </script>

    <script type="text/javascript" src="js/jquery.jqGrid.min.js"></script>

<script type="text/javascript">
//<![CDATA[
    $(document).ready(function() {
        // first create the grid
        $('#grid').jqGrid({
    url:'grid.php',
    datatype: "json",
    height: 100,
    width: 900,
    colNames:['Customer ID','Hardware ID', 'Username','Password','Email','Last Login','Last IP','Registration Date','Expire Date'],
    colModel:[
        {name:'customerid',index:'customerid', width:150},
        {name:'hardware_id',index:'hardware_id', width:150},
        {name:'username',index:'username', width:100},
        {name:'password',index:'password', width:100},
        {name:'email',index:'email', width:100},
        {name:'lastlogin',index:'lastlogin', width:100},
        {name:'lastipaddress',index:'lastipaddress', width:100},
        {name:'registration_date',index:'registration_date', width:100},
        {name:'expire_date',index:'expire_date', width:100}
    ],
    rowNum:5,
    rowList:[5,8,10,20,30], 
    mtype: "GET",
    gridview: true,
    pager: '#pager',
    sortname: 'customerid',
    viewrecords: true,
    sortorder: "desc",
    caption: "Virtual scrolling on local data"
});

jQuery("#grid").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false});
        // now you can any time change the width of the grid
        $('#grid').jqGrid('setGridWidth', 900);


    });

//]]>
</script>
  </head>

  <body>
      <div>
          <?php include ("grid.php");?>
      </div>
      <div id="pager"></div>

   </body></html> 

后端-Grid.php

<?php

    include '../dbc.php';
    page_protect();
    require_once 'jq-config.php';
    // include the jqGrid Class
    require_once ABSPATH."php/jqGrid.php";
    // include the driver class
    require_once ABSPATH."php/jqGridPdo.php";
    // Connection to the server

    $link = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
    // Tell the db that we use utf-8
    $link ->query("SET NAMES utf8");

    // Create the jqGrid instance
    $grid = new jqGridRender($link);

    $username = $_SESSION['user_name'];
    $grid->SelectCommand = "SELECT * FROM tblcustomer_$username";
    // set the ouput format to json
    $grid->dataType = 'json';
    // Let the grid create the model
    $grid->setColModel();
    // Set the url from where we obtain the data
    $grid->setUrl('grid.php');
    // Set some grid options
    $grid->setGridOptions(array("rowNum"=>100,"sortname"=>"customerid","height"=>150));

    // Change some property of the field(s)
    $grid->setColProperty("lastlogin", array(
        "formatter"=>"date",
        "formatoptions"=>array("srcformat"=>"Y-m-d H:i:s","newformat"=>"m/d/Y"),
        "search"=>false,
        "width"=>"400"
        ));
    // Registration date
    $grid->setColProperty("registration_date", array(
        "formatter"=>"date",
        "formatoptions"=>array("srcformat"=>"Y-m-d H:i:s","newformat"=>"m/d/Y"),
        "search"=>false,
        "width"=>"400"
        ));
    $grid->setColProperty("expire_date", array(
        "formatter"=>"date",
        "formatoptions"=>array("srcformat"=>"Y-m-d H:i:s","newformat"=>"m/d/Y"),
        "search"=>false,
        "width"=>"400"
        ));
    $grid->setColProperty("customerid",array("width"=>"650"));
    $grid->setColProperty("hardware_id",array("width"=>"250"));
    $grid->setColProperty("username",array("width"=>"150"));
    $grid->setColProperty("password",array("width"=>"150"));
    $grid->setColProperty("email",array("width"=>"150"));
    $grid->setColProperty("lastipaddress",array("width"=>"100"));

    $grid->setFilterOptions(array("stringResult"=>true));
    $grid->navigator=true;
    $grid->setNavOptions('navigator',array("excel"=>true,"add"=>true,"edit"=>true,"view"=>true,"del"=>true,"search"=>true));
    // Enjoy
    $grid->renderGrid('#grid','#pager',true, null, null, true,true);
    $link = null;
    ?> 
php jqgrid
1个回答
1
投票

我没有使用任何jqGrid的商业版本,例如jqSuite或PHP的jqGrid,但是在看了一下演示页面后,我可能在代码中看到了一些问题:

您的代码是jqGrid和jqGrid商业版代码的混合。

如果您将jqGrid用于PHP,则应删除 $(document).ready(function() {/*the code should be removed*/ 。中$(document).ready(function() {/*the code should be removed*/因为您使用该行

$grid->renderGrid('#grid','#pager',true, null, null, true,true);

在您的PHP代码中,您应该将HTML代码的主体

<div> 
    <?php include ("grid.php");?> 
</div>

如果HTML片段<table id="grid"></table><div id="page"></div>将生成您自己的PHP代码。

如果您不使用商业版本的jqGrid,则应在HTML代码中包含<table id="grid"></table> 。 你也应该改变'#pager2'参数'navGrid''#pager' 。 要使网格充满数据,您需要将jqGrid的url参数更改为为网格提供JSON数据的任何URL。 请参阅有关JSON数据格式的文档

© www.soinside.com 2019 - 2024. All rights reserved.