如何从另一个jqgrid

时间:2017-01-24 00:39:51

标签: javascript php mysql jqgrid

我有一个jqgrid显示一个表的两个字段(测试); ID字段和图片字段。在图片字段中,我从数据库中检索图像地址并在此字段中显示它们。这个jqgrid有多选属性。 1-我想从这个jqgrid中选择一些行,然后单击一个按钮从这些选定的行创建新的jqgrid。但是当我点击按钮而不是显示行this alert时。 2 - 当我想将此网格保存为仅保存的pdf图像地址(而不是图像)。 3 - 当我选择一些行时,“selr”变量包含此网格中的行的等级,而我需要selr包含所选行的“ID”。我的代码是:

//index.php
<!DOCTYPE html>
<html>
<head>
    <title>Insert selected row from grid1 to grid2</title>
    <link rel="stylesheet" type="text/css" media="screen" href="css/jquery-ui.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="css/trirand/ui.jqgrid.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="css/ui.multiselect.css" />
    <style type="text">
        html, body {
            margin: 0;            /* Remove body margin/padding */
            padding: 0;
            overflow: hidden;    /* Remove scroll bars on browser window */
            font-size: 75%;
        }

    </style>
    <script src="js/jquery.min.js" type="text/javascript"></script>
    <script src="js/trirand/i18n/grid.locale-en.js" type="text/javascript"></script>
    <script src="js/trirand/jquery.jqGrid.min.js" type="text/javascript"></script> <script type="text/javascript">
        $.jgrid.no_legacy_api = true;
        $.jgrid.useJSON = true;
        $.jgrid.defaults.width = "700";
    </script>

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

    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery("#addtolist").click(function () {
                var selr = jQuery('#grid').jqGrid('getGridParam', 'selarrrow');
                var se = JSON.stringify(selr);
                $.ajax({
                    type: 'post', // the method (could be GET btw)
                    url: 'selectedgrid.php', // The file where my php code is
                    data: {
                        'sel': se // all variables i want to pass. In this case, only one.
                    },
                    success: function (data) { // in case of success get the output, i named data
                        $("#divid").html(data);
                    }
                });
            });
        });
    </script>

</head>
<body>

<?php
include("imagegrid.php");
?>
<button id="addtolist">Selected List</button>
<div id="divid"></div>
</body>
</html>

imagegrid.php

    //imagegrid.php
<?php
require_once 'jq-config.php';
// include the jqGrid Class
require_once "php/PHPSuito/jqGrid.php";
// include the driver class
require_once "php/PHPSuito/DBdrivers/jqGridPdo.php";
// Connection to the server
$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
// Tell the db that we use utf-8
$conn->query("SET NAMES utf8");
// Create the jqGrid instance
$grid = new jqGridRender($conn);
$Model = array(
    array("name"=>"ID","width"=>20),
    array("name"=>"picture","width"=>300,"formatter"=>"js:formatImage", "unformat"=>"js:unformatImage"),
);
$grid->SelectCommand = 'SELECT ID, picture FROM test';
// set the ouput format to xml since json have problems
$grid->dataType = 'json';
// Let the grid create the model
$grid->setColModel($Model);
// Set the url from where we obtain the data
$grid->setUrl('imagegrid.php');
// Set some grid options

//get the height passed as parameter
// Set grid option datatype to be local
$grid->setGridOptions(array(
    "caption"=>"grid1",
    "rowNum"=>20,
    "sortname"=>"ID",
    "hoverrows"=>true,
    "rowList"=>array(20,30,40,50),
    "autowidth"=>true,
    "shrinkToFit"=>true,
    "direction"=>"rtl",
    "height"=>'auto',
    "multiselect"=>true,
));

$custom = <<<CUSTOM
function formatImage(cellValue, options, rowObject) {
    var imageHtml = "<img src="+ cellValue+" />";
return imageHtml;
}
function unformatImage(cellValue, options, cellObject) {
    return $(cellObject.html()).attr("originalValue");
}
CUSTOM;

$grid->navigator = true;
// Enable excel export
$grid->setNavOptions('navigator', array("excel"=>false,"add"=>false,"edit"=>false,"del"=>false,"view"=>false,"cloneToTop"=>true));
// add a custom button via the build in callGridMethod
// note the js: before the function
$buttonoptions = array("#pager",
    array("caption"=>"Pdf", "title"=>"Extract to pdf", "onClickButton"=>"js: function(){
        jQuery('#grid').jqGrid('excelExport',{tag:'pdf', url:'imagegrid.php'});}"
    )
);
$grid->callGridMethod("#grid", "navButtonAdd", $buttonoptions);

// Set it to toppager
$buttonoptions[0] = "#grid_toppager";
$grid->callGridMethod("#grid", "navButtonAdd", $buttonoptions);
$grid->setJSCode($custom);
// Enjoy
$grid->renderGrid('#grid','#pager',true, null, null, true,true);

selectedgrid.php代码:

//selectedgrid.php
<?php
$IDlist=json_decode($_POST['sel']);
require_once 'jq-config.php';
// include the jqGrid Class
require_once "php/PHPSuito/jqGrid.php";
// include the driver class
require_once "php/PHPSuito/DBdrivers/jqGridPdo.php";
// Connection to the server
$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
// Tell the db that we use utf-8
$conn->query("SET NAMES utf8");
// Create the jqGrid instance
$grid1 = new jqGridRender($conn);
$Model = array(
    array("name"=>"ID","width"=>20),
    array("name"=>"picture","width"=>300,"formatter"=>"js:formatImage", "unformat"=>"js:unformatImage"),
);
$grid1->SelectCommand = 'SELECT ID, picture FROM test WHERE ID IN'."(".implode(',',$IDlist).")";
// set the ouput format to xml since json have problems
$grid1->dataType = 'json';
// Let the grid create the model
// Set the url from where we obtain the data
$grid1->setUrl('selectedgrid.php');
// Set some grid options

//get the height passed as parameter
$grid1->setColModel($Model);
// Set grid option datatype to be local
$grid1->setGridOptions(array(
    "caption"=>"grid2",
    "rowNum"=>20,
    "sortname"=>"ID",
    "hoverrows"=>true,
    "rowList"=>array(20,30,40,50),
    "autowidth"=>true,
    "shrinkToFit"=>true,
    "direction"=>"rtl",
    "height"=>'auto',
));
$custom = <<<CUSTOM
function formatImage(cellValue, options, rowObject) {
    var imageHtml = "<img src="+ cellValue+" />";
return imageHtml;
}
function unformatImage(cellValue, options, cellObject) {
    return $(cellObject.html()).attr("originalValue");
}
CUSTOM;

// Enable toolbar searching
$grid1->toolbarfilter = true;
$grid1->setFilterOptions(array("stringResult"=>true));
// Enjoy
$grid1->setJSCode($custom);
$grid1->renderGrid('#grid2','#pager2',true, null, null, true,true);

错误是:

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: sel in C:\wamp64\www\ltms\selectedgrid.php on line <i>2</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>250312</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\ltms\selectedgrid.php' bgcolor='#eeeeec'>...\selectedgrid.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: implode(): Invalid arguments passed in C:\wamp64\www\ltms\selectedgrid.php on line <i>18</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>250312</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\ltms\selectedgrid.php' bgcolor='#eeeeec'>...\selectedgrid.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.5280</td><td bgcolor='#eeeeec' align='right'>2216760</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.implode' target='_new'>implode</a>
(  )</td><td title='C:\wamp64\www\ltms\selectedgrid.php' bgcolor='#eeeeec'>...\selectedgrid.php<b>:</b>18</td></tr>
</table></font>
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1Could not execute query!!!

0 个答案:

没有答案