【问题标题】:jqGrid data no longer appearingjqGrid 数据不再出现
【发布时间】:2011-11-24 03:08:53
【问题描述】:

在这个表格上有很多关于这个主题的主题;但是,我仍然无法让它正常工作。我想将表格的宽度设置为 900px。此外,因为我要显示近十几行,所以如果行溢出表格大小的边界(如果可能的话),我希望自动实现滚动条。

我目前遇到的问题:

  1. 用户帮我解决了这个问题,就是设置宽度

  2. 数据不再出现在我的网格中,猜测是服务器端问题。

更新为包含完整代码:

HTML:

      <?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 = true;
        $.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, sorttype:'int'},
        {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"
});

$("#grid").jqGrid('navGrid','#pager', {view: true,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;
?> 

我觉得好像我已经尝试了所有东西,但是如果有人看到我看不到的东西,请随时告诉我!

感谢您的帮助,

埃文

【问题讨论】:

  • 您是否仔细检查过父元素的大小?
  • 是的,我可以使用以下事件来更改网格的宽度(但仅在调整屏幕大小时才有效)。 $(window).bind('resize', function() { $("#grid").setGridWidth($(window).width()); }).trigger('resize');
  • 首先你应该在你的JavaScript代码中插入);在代码的末尾,这将对应于$(document).ready(function() {。目前,您的代码中有语法错误。第二个sortname: 'item_id' 错误,因为网格没有'item_id' 列。
  • @Oleg - 我以为我已经正确更新了它,它在当前版本中不存在(现在将修复)。此外,我更新了我遇到的问题,因为您解决了其中的一部分,这反过来又导致了另一个问题。猜测此错误与服务器端有关?
  • jqGrid 是开源的 JavaScript 代码。在您的问题中,您发布了 PHP 代码。此外,它使用来自trirand.net商业 产品。我个人不使用 PHP,当然也不使用 jqGrid for PHP。如果您要捕获返回服务器代码 'grid.php' 的 JSON 结果(您可以使用 FiddlerFirebug 捕获 HTTP 流量),则可以独立于 PHP 代码分析问题。

标签: php jquery jqgrid width


【解决方案1】:

你能发布你的 jqgrid 初始化代码吗?这是我的一个示例,应该按照您的描述进行:

$("#Building_8839_Transactions").jqGrid({

"colModel":[{"editable":false,"name":"TRANS_YEAR","label":"Year","width":"100.0"},{"name":"TRANS_DESCRIP","editable":true,"editrules":{"required":false},"label":"Action","width":"300.0"},{"name":"BLS_ID","hidden":true,"key":true}],

"shrinkToFit":false,
"sortorder":"desc",
"height":"auto",
"jsonReader":{"page":"PAGE","records":"RECORDS","repeatitems":false,"total":"TOTAL","root":"ROWS"},
"width":"500",
"viewrecords":true,
"editurl":"foobar.cfm",
"datatype":"json",
"caption":"BLS Transactions",
"rowNum":"15",
"url":"blah.cfm",
"sortname":"TRANS_YEAR"         
});

重要的位是“width”:“500”和“shrinkToFit”:false。试试这些。

【讨论】:

  • 只是好奇,editurl和url有什么区别?
  • editurl是处理插入/更新/删除的url,url是处理读取的url(包括分页/排序/过滤)
  • 如果记录甚至没有进入,我是否在服务器端代码中做错了什么?然而,他们是在进行此线程建议我进行的前端更改之前进行的。
  • 查看您是否收到错误,无论是在 JS 级别还是作为服务器响应的一部分。此外,您返回的数据可能与您在网格定义中指定的格式不匹配,因此请检查一下。
  • 我尽量不要在这里听起来很愚蠢,但我不记得,也没有看到我定义数据格式的任何地方。你指的是哪里?
【解决方案2】:

没有jQuery(document)gridComplete 方法。你的意思是肯定jQuery.ready。此外,只有在将 if 转换为网格后,您才能在 &lt;table id="grid"&gt; 上使用 setGridWidth。此外,代码$.jgrid.no_legacy_api = false;$.jgrid.useJSON = true; 应该在jquery.jqGrid.min.js 之前和grid.locale-en.js 之后执行。所以代码应该是关于以下的

<script type="text/javascript" src="js/i18n/grid.locale-en.js"></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({
            // parameters of jqGrid
        }); 

        // now you can any time change the width of the grid
        $('#grid').jqGrid('setGridWidth', 900);
    }
//]]>
</script>

【讨论】:

  • 已更新以显示完整代码并进一步解释了问题。谢谢!
  • @Evan:我收到否决票可能是因为我的回答是关于你的原始问题,而不是问题的最后一个版本。因此,请始终追加您的问题文本,并在新版本代码之后使用“UPDATED:​​>”之类的字眼。
  • 我只需要发布一个新问题,然后提供适当的指导。我没有意识到我造成的伤害,非常抱歉。
【解决方案3】:

PHP 的最佳方法:

// Set table with auto
$grid->setGridOptions(array( 
    "rowNum"=>10, 
    "sortable"=>true, 
    "rownumbers"=>true,
    "width"=>'auto',
    "altRows"=>true, 
    "multiselect"=>true, 
    "rowList"=>array(10,20,50), 
    )); 

// Set table with 100px
$grid->setGridOptions(array( 
    "rowNum"=>10, 
    "sortable"=>true, 
    "rownumbers"=>true,
    "width"=>1000,
    "altRows"=>true, 
    "multiselect"=>true, 
    "rowList"=>array(10,20,50), 
    )); 

Check my complete code.

【讨论】:

    猜你喜欢
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多