【问题标题】:Call data from the server without using GET Method on jqgrid在 jqgrid 上不使用 GET 方法从服务器调用数据
【发布时间】:2015-06-17 22:45:03
【问题描述】:

我是 jqgrid 的新手。我做了很多关于如何在不使用 GET 方法的情况下调用数据库中的数据但没有成功的研究。

我有以下 HTML 代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>jqGrid UI</title>
    <link rel='stylesheet' type='text/css' href='http://code.jquery.com/ui/1.10.3/themes/redmond/jquery-ui.css' />
    <link rel='stylesheet' type='text/css' href='http://www.trirand.com/blog/jqgrid/themes/ui.jqgrid.css' />

    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

    <script type='text/javascript' src='http://www.trirand.com/blog/jqgrid/js/jquery-ui-custom.min.js'></script>        
    <script type='text/javascript' src='http://www.trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js'></script>
    <script type='text/javascript' src='http://www.trirand.com/blog/jqgrid/js/jquery.jqGrid.js'></script>

    <script>
    $(document).ready(function () {
        $("#list_records").jqGrid({
            url: "getGridData.php", // I want to change this to be on the same page to call the data from the database, do I have to use the data: varname?
            datatype: "json",
            mtype: "GET", // should I change this to data type: local if I want to change the url?
            colNames: ["User Id", "User Name", "First Name", "Last Name"],
            colModel: [
                { name: "invid",align:"right"},
                { name: "invdate"},
                { name: "amount"},
                { name: "tax"}
            ],
            pager: "#perpage",
            rowNum: 10,
            rowList: [10,20],
            sortname: "invid",
            sortorder: "asc",
            height: 'auto',
            viewrecords: true,
            gridview: true,
            caption: ""
        });     
    });
    </script>
</head>

<body>
<table id="list_records"><tr><td></td></tr></table> 
<div id="perpage"></div>
</body>
</html>

我有下面的 php mysql 代码:

<?php 
$conn = mysql_connect("localhost", "root", "") or die("Connection Error: " . mysql_error()); 
mysql_select_db("jqgrid") or die("Error connecting to db."); 

$page = $_GET['page']; 
$limit = $_GET['rows']; 
$sidx = $_GET['sidx']; 
$sord = $_GET['sord']; 

if(!$sidx) $sidx =1; 

$result = mysql_query("SELECT COUNT(*) AS count FROM invheader"); 
$row = mysql_fetch_array($result,MYSQL_ASSOC); 

$count = $row['count']; 
if( $count > 0 && $limit > 0) { 
    $total_pages = ceil($count/$limit); 
} else { 
    $total_pages = 0; 
} 
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit;
if($start <0) $start = 0; 

$SQL = "SELECT * FROM invheader ORDER BY $sidx $sord LIMIT $start , $limit"; 
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error()); 

$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    $responce->rows[$i]['id']=$row['invid'];
    $responce->rows[$i]['cell']=array($row['invdate'],$row['amount'],$row['tax'],$row['total']);
    $i++;
}
echo json_encode($responce);
?>

这是完全有效的。但是,我不想使用 get 方法。我想把它全部放在一个php页面中。有人可以给我一个示例代码,说明如何最好使用数组来实现这一点。提前致谢!

更新

我现在已经设法了解如何按照此处给出的说明将 php 查询结果转换为 javascript http://www.dyn-web.com/tutorials/php-js/json/array.php

我现在的问题是我无法将转换后的变量用于 jqgrid。我希望得到与此页面 http://trirand.com/blog/jqgrid/jqgrid.html#t95 上相同的结果,唯一的区别是我使用转换后的 php 结果到 javascopt 而不是创建静态数据。

下面是我的脚本代码:

var mydata = <?php echo json_encode($dataitems)?>;
jQuery("#list4").jqGrid({
    data: mydata,
    datatype: "local",
    height: 250,
    colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],
    colModel:[
        {name:'id',index:'id', width:60, sorttype:"int"},
        {name:'invdate',index:'invdate', width:90, sorttype:"date"},
        {name:'name',index:'name', width:100},
        {name:'amount',index:'amount', width:80, align:"right",sorttype:"float"},
        {name:'tax',index:'tax', width:80, align:"right",sorttype:"float"},     
        {name:'total',index:'total', width:80,align:"right",sorttype:"float"},      
        {name:'note',index:'note', width:150, sortable:false}       
    ],
    multiselect: true,
    caption: "Manipulating Array Data"
});

【问题讨论】:

    标签: php jquery ajax json jqgrid


    【解决方案1】:

    虽然这不是非常有效,但你可以做这样的事情

    $data = array();
    while($row = mysql_fetch_assoc($result)) $data[] = $row;
    echo '<script>var data =' . json_encode($data) . ';</script>';
    

    此时您将拥有一个包含所有数据的 JSON 对象。这将使页面加载时间更长,但会在没有 AJAX 的情况下执行您所要求的操作

    当我这样做的时候,mysql_ functions are deprecated。考虑切换到mysqli_

    【讨论】:

    • 谢谢,但它没有回答我的问题。
    • 如果没有,那么您需要指定您想要的内容。通过将完整的 JSON 添加到您不再需要 AJAX 的页面
    • 感谢您的及时回复,我现在更新了我的查询。谢谢
    • 更新,谁能帮帮我
    猜你喜欢
    • 1970-01-01
    • 2015-05-06
    • 2021-04-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    相关资源
    最近更新 更多