jqgrid - 添加,编辑,删除不起作用

时间:2012-05-20 21:24:06

标签: javascript jqgrid

我无法添加,编辑,删除,搜索

甚至我把editrules:{required:true}也不起作用。

(萤火虫中的错误显示“isEmtpy不是函数”)


这是我的代码...

的index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jqgrid</title> 

    <!-- installation files of jqgrid -->
    <link rel="stylesheet" type="text/css" media="screen" href="themes/redmond/jquery-ui-1.8.16.custom.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="themes/ui.jqgrid.css" />
    <script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
    <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
    <!-- installation files of jqgrid -->

<script type="text/javascript"> 

function mypricecheck(value, colname) {
if (value < 0 || value >20) 
   return [false,"Please enter value between 0 and 20"];
else 
   return [true,""];
}

    jQuery().ready(function (){

        jQuery("#list1").jqGrid({
            url:'server.php?q=1',
            datatype: 'xml',
            //colNames:['ProductCode','ProductCategoryID', 'ProductBrandID', 'ProductDescription','ProductOriginal','ProductCompatible','ProductDealerPrice'],
            colModel:[
                {name: "ProductCode", index: "ProductCode", label: "รหัสสินค้า", width: 50, editable:true, key: true  , edittype: 'text', editoptions: { dataInit: function (el) { $(el).css('text-transform', 'uppercase'); }} },
                {name: "ProductCategoryID", index: "ProductCategoryID", label: "หมวดหมู่", width: 60, editable: true, edittype: 'select', editoptions:{value:{Toner:'Toner',Inkjet:'Inkjet',Ribbon:'Ribbon','Fax Film':'Fax Film'}} },
                {name: "ProductBrandID", index: "ProductBrandID", label: "แบรนด์", width: 50, editable: true, edittype: 'select', editoptions:{dataUrl: "optbrand.php"} },
                {name: "ProductDescription", index: "ProductDescription", label: "คำอธิบาย", width: 120, editable: true},
                {name: "ProductOriginal", index: "ProductOriginal", label: "รุ่นออริจินัล", width: 140, editable: true},
                {name: "ProductCompatible", index: "ProductCompatible", label: "เหมาะสำหรับรุ่น", width: 200, editable: true},
                {name: "ProductDealerPrice", index: "ProductDealerPrice", label: "ราคาเดลเลอร์", width: 40, editable: true, editrules:{number:true},sorttype:'number',formatter:'number'}
            ],
            rowNum:10,
            autowidth: true,
            rowList:[10,20,30],
            pager: jQuery('#pager1'),
            sortname: 'ProductCategoryID',
            viewrecords: true,
            sortorder: "asc",
            caption: 'ฐานข้อมูลสินค้า',
            rownumbers:true, 
            height:'auto',
            editurl:'server.php',
        });

        jQuery("#list1").jqGrid('navGrid','#pager1',{edit:true,add:true,del:true,view:true,search:true,refresh:true});
    });
</script>

</head>
<body>
<table id="list1"></table>
<div id="pager1"></div>
</body>
</html>

server.php

<?php
include "jq-config.php";
$page = $_GET['page']; // get the requested page
$limit = $_GET['rows']; // get how many rows we want to have into the grid
if($limit < 0) $limit = 10;
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction
if(!$sidx) $sidx =1;
// connect to the database
$db = mysql_connect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());

mysql_select_db($database) or die("Error conecting to db.");
$result = mysql_query("SELECT COUNT(*) AS count FROM tb_product");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];

if( $count >0 ) {
    $total_pages = ceil($count/$limit);
} else {
    $total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if($start <0)
{
    $start = 0;
}
$SQL = "SELECT * FROM tb_product ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldnt execute query.".mysql_error());

if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
header("Content-type: text/xml;charset=utf-8");
}
$et = ">";

echo "<?xml version='1.0' encoding='utf-8'?$et\n";
echo "<rows>";
echo "<page>".$page."</page>";
echo "<total>".$total_pages."</total>";
echo "<records>".$count."</records>";
// be sure to put text data in CDATA
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    echo "<row id='". $row[ProductCode]."'>";
    echo "<cell>". $row[ProductCode]."</cell>";

    $tmp = $row['ProductCategoryID'];
    $sql1 = "SELECT ProductCategoryName FROM tb_productcategory WHERE ProductCategoryID = '$tmp' ";
    $result1 = mysql_query($sql1) or die (mysql_error());
    list($ProductCategoryName) = mysql_fetch_row($result1);
    $tmp = $row['ProductBrandID'];
    $sql1 = "SELECT ProductBrandName FROM tb_productbrand WHERE ProductBrandID = '$tmp' ";
    $result1 = mysql_query($sql1) or die (mysql_error());
    list($ProductBrandName) = mysql_fetch_row($result1);

    echo "<cell><![CDATA[". $ProductCategoryName."]]></cell>";
    echo "<cell><![CDATA[". $ProductBrandName."]]></cell>";
    //echo "<cell>". $row[ProductCategoryID]."</cell>";
    //echo "<cell>". $row[ProductBrandID]."</cell>";
    echo "<cell><![CDATA[". $row[ProductDescription]."]]></cell>";
    echo "<cell><![CDATA[". $row[ProductOriginal]."]]></cell>";
    echo "<cell><![CDATA[". $row[ProductCompatible]."]]></cell>";
    echo "<cell>". $row[ProductDealerPrice]."</cell>";
    echo "</row>";
}
echo "</rows>";     

?>
请帮忙,

感谢,

1 个答案:

答案 0 :(得分:3)

在jqGrid 4.3.2版本的第二个版本中已知错误。您可以在the answerhere中阅读详细信息。现在问题已解决在github上的jqGrid代码中。因此,您可以从here下载jquery.jqGrid.src.js的固定版本,也可以从github下载最新版本的jqGrid。在最后一种情况下,您必须关注the instructions,其中介绍了如何使用jqGrid模块而不是一个jquery.jqGrid.src.jsjquery.jqGrid.min.js文件。