根据单元格的内容(而不仅仅是id)有条件地启用或禁用删除记录按钮


Conditionally enable or disable delete record button based on the Content of the cell (not just the the id)

我是jquery和jqgrid的新手,但我对javascript很熟悉。然而,经过一番努力,我终于安装了jqgrid。

我一直在尝试找到一个解决方案,根据"锁定"列的值启用或禁用导航栏中的删除功能。我阅读了以下链接jqgrid:如何根据所选行中的列值设置工具栏选项

但我无法获取javascript的"锁定"单元格的内容。我还试图格式化锁定字符串,但没有效果。

jqgrid是通过php加载的。脚本在这里http://www.trirand.net/demophp.aspx

php脚本如下

require_once("JQGrid/jq-config.php");
require_once("JQGrid/php/jqGridASCII.php");
require_once("JQGrid/php/jqGridPdo.php");
$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
$grid = new jqGridRender($conn);
$grid->SelectCommand = 'SELECT * FROM  `device_assignement` ';
$grid->dataType = 'json';
$grid->setColModel();
$grid->setUrl('Grid_ecu_display.php');
$grid->setColProperty("company", 

array("label"=>"Dealer Name", 
"width"=>350
), 
array( "searchrules"=> 
array("searchhidden"=>false, "required"=>false, "search"=>false)));


$grid->setGridOptions(array( 
"sortable"=>true, 
"rownumbers"=>true, 
"rowNum"=>40, 
"rowList"=>array(10,50,100), 
"sortname"=>"ecu",  
"width"=>940,  
"height"=>400,  
"shrinkToFit"=>true,  
"hidden" => true,
"hoverrows"=>true ));

$grid->toolbarfilter = true;
$grid->setFilterOptions(array("stringResult"=>true));
$grid->setColProperty("ecu", array(
"label"=>"ECU Number" ,  
"sortable"=>true
));
$grid->setColProperty("lock", array(
"label"=>"<i>Lock</i>" ,  
"width"=>60,
"sortable"=>false,
"editable"=>true
));

等等…

$ecu = jqGridUtils::GetParam('ecu'); 
// This command is executed immediatley after edit occur. 
$grid->setAfterCrudAction('edit', "UPDATE  `ecu_master` SET  `lock` =  '1'             WHERE  `ecu` =?",array($ecu));  

$grid->navigator = true;
$grid->setNavOptions('navigator', array("pdf"=>true, "add"=>false,"edit"=>true,"del"=>false,"view"=>false, "excel"=>true)); 

$grid->setColProperty('company',array("searchoptions"=>array("sopt"=>array("cn"))));
$oper = jqGridUtils::GetParam("oper"); 
if($oper == "pdf") { 
$grid->setPdfOptions(array( 
// set the page orientation to landscape 
"page_orientation"=>"L", 
// enable header information 
"header"=>true, 
// set bigger top margin 
"margin_top"=>27, 
// set logo image 
//"header_logo"=>"logo.gif", 
// set logo image width 
//"header_logo_width"=>30, 
//header title 
"header_title"=>"Autograde CMS ECU Allocation List", 
// and a header string to print 
"header_string"=>"$SoftwareVersion" 
)); 
} 
// Run the script
$grid->renderGrid('#grid','#pager',true, null, null, true,true);

这包含在另一个php脚本中。我只想根据"锁定"值启用或禁用删除行按钮如果这看起来太基本和荒谬,请让我知道我会理解的。

如果用户单击网格的一个单元格,则会选择整行,并调用回调函数onSelectRow。因此,您应该在SelectRow上实现回调函数,该函数将rowid(<tr>id)作为第一个参数。在onSelectRow处理程序内部,您可以调用getCell方法。根据"锁定"列的值(如果需要,可以隐藏),您可以启用或禁用导航栏的"编辑"answers"删除"按钮。

因此,代码可以是以下内容:

$('#list').jqGrid({
    ... all other jqGrid options which you need
    pager: '#pager',
    onSelectRow: function (rowid) {
        var gridId = $.jgrid.jqID(this.id);
        // test 'lock' column for some value like 'yes'
        if ($(this).jqGrid('getCell', rowid, 'lock') === 'yes') {
            // disable the "Edit" and "Delete" buttons of the navigator
            $("#edit_" + gridId).addClass('ui-state-disabled');
            $("#del_" + gridId).addClass('ui-state-disabled');
        } else {
            // enable the "Edit" and "Delete" buttons of the navigator
            $("#edit_" + gridId).removeClass('ui-state-disabled');
            $("#del_" + gridId).removeClass('ui-state-disabled');
        }
    }
}).jqGrid('navGrid', '#pager');

因为你是jqGrid的新手,我想评论一下$.jgrid.jqID()函数的用法。在大多数情况下,if返回输入参数的值:在示例中为"list"。如果网格的id(<table>元素的id)包含元字符,则在更常见的情况下需要它。$.jgrid.jqID()函数在任何元字符之前都包含额外的转义字符(两个反斜杠:'')。