简体   繁体   English

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

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

I am new to jquery and jqgrid, but i am comfortable with javascript. 我是jquery和jqgrid的新手,但是我对javascript很满意。 However I have managed to install jqgrid after some effort. 但是,经过一些努力,我设法安装了jqgrid。

I have been trying a to find a solution to enable ore disable the delete feature from the navigation bar based on the value of the 'lock' column. 我一直在尝试寻找一种解决方案,以基于“锁定”列的值启用或禁用导航栏中的删除功能。 I read the following link jqgrid: how to set toolbar options based on column value in row selected 我阅读了以下链接jqgrid:如何根据所选行中的列值设置工具栏选项

But I was not able to get the contents of 'lock' cell for the javascript. 但是我无法获取JavaScript的“锁定”单元格的内容。 I also tried to format the lock string without effect. 我也尝试格式化锁定字符串,但没有任何效果。

the jqgrid is loaded via php. jqgrid是通过php加载的。 The script is here http://www.trirand.net/demophp.aspx 脚本在这里http://www.trirand.net/demophp.aspx

The php script is the following 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
));

etc etc... 等等等等

$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);

This is included in another php script where. 这包含在另一个php脚本中。 All I want is to enable or disable the delete row button based on the "lock" value If this seems too basic and ridiculous please let me know I will understand. 我只想根据“锁定”值启用或禁用“删除行”按钮。如果这看起来太基本和荒谬,请告诉我,我会理解。

If the user click on a cell of the grid the whole row will be selected and the callback function onSelectRow will be called. 如果用户单击网格的某个单元格,则将选择整行,并将调用回调函数onSelectRow So you should implement the callback function onSelectRow which has the rowid (the id of the <tr> ) as the first parameter. 因此,您应该实现将以rowid( <tr>id )作为第一个参数的onSelectRow回调函数。 Inside of the onSelectRow handler you can call getCell method. onSelectRow处理程序内部,您可以调用getCell方法。 Depend on the value of the 'lock' column (which can be hidden if needed) you can enable of disable "Edit" and "Delete" buttons of the navigator bar. 根据“锁定”列的值(必要时可以将其隐藏),可以启用导航栏的“编辑”和“删除”按钮。

So the code can be about the following: 因此,代码可以与以下内容有关:

$('#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');

Because you are new in jqGrid I want comment the usage of $.jgrid.jqID() function. 因为您是jqGrid的新手,所以我想评论$.jgrid.jqID()函数的用法。 In the most cases if returns the value of the input parameter: 'list' in case of the example. 在大多数情况下,if返回输入参数的值:在本示例中为'list'。 It's needed for more common case if the id of the grid (the id of the <table> element) contains meta-characters . 如果网格的ID( <table>元素的ID)包含meta-characters,则是更常见的情况。 $.jgrid.jqID() function include additional escape characters (two backslashes: \\\\ ) before any meta-character. $.jgrid.jqID()函数在任何元字符之前包含其他转义符(两个反斜杠: \\\\ )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM