1

I am new to jquery and jqgrid, but i am comfortable with javascript. However I have managed to install jqgrid after some effort.

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

But I was not able to get the contents of 'lock' cell for the javascript. I also tried to format the lock string without effect.

the jqgrid is loaded via php. The script is here http://www.trirand.net/demophp.aspx

The php script is the following

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. 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.

Community
  • 1
  • 1
Kaippally
  • 96
  • 10

1 Answers1

0

If the user click on a cell of the grid the whole row will be selected and the callback function onSelectRow will be called. So you should implement the callback function onSelectRow which has the rowid (the id of the <tr>) as the first parameter. Inside of the onSelectRow handler you can call getCell method. 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. In the most cases if returns the value of the input parameter: 'list' in case of the example. It's needed for more common case if the id of the grid (the id of the <table> element) contains meta-characters. $.jgrid.jqID() function include additional escape characters (two backslashes: \\) before any meta-character.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I applied your solution but it messes up my grid display should I add this within after including the php script that calls the json data? – Kaippally Feb 04 '12 at 11:55
  • @user1188970: sorry, but I don't understand what you mean. I am not sure that you use jqGrid in the correct way. Look at [the part](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:first_grid#html_file) of the documentation for an example of the code. – Oleg Feb 04 '12 at 12:05
  • This is the output of the php script that provides the jason data. `{"records":"1","page":1,"total":1,"rows":[ {"ecu":"D1005E-30275","lock":"1","add_date_timestamp":"2012-01-27 10:13:01","company":"Global Auto electricals","email":"global@email.com","phone":"+97335784587","mobile":"+9714967451348"} ] }` – Kaippally Feb 04 '12 at 12:06
  • @user1188970: It's just the JSON data, but you wrote about some problems withe the JavaScript code. You should [modify](http://stackoverflow.com/faq#howtoask) the text of your question and [include the JavaScript](http://meta.stackexchange.com/a/22189/147495) or probably the HTML code which you use. – Oleg Feb 04 '12 at 12:13
  • I am using this [link]http://www.trirand.net/demophp.aspx[/link] for creating grids in php scripts. the datagrid.php looks like this 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'); ..... This script produces the jason data above. – Kaippally Feb 04 '12 at 12:14
  • @user1188970: It seems that you use commercial version of jqSuite instead of free open source JavaScript jqGrid. So you should use use [jqgrid-php](http://stackoverflow.com/tags/jqgrid-php/info) instead of [jqgrid](http://stackoverflow.com/tags/jqgrid/info) as the tag of your question (or at least as additional tag). I personally don't use PHP and don't use commertial version or jqGrid or jqSuite. So I can't help you. So you should search in the documentation how to event handler like `onSelectRow` in the commercial version. – Oleg Feb 04 '12 at 12:34
  • I was not aware of an open source version of Jqgrid. Thank you Oleg. I will try this with the jqgrid-php. – Kaippally Feb 04 '12 at 12:36
  • @Kaippally: You are welcome! You can download the last version of the Free, Open Source jqGrid [here](http://www.trirand.com/blog/?page_id=6). It includes the full source code either as separate modules or as the one file `jquery.jqGrid.src.js`. You can download [here](https://github.com/tonytomov/jqGrid/) the most recent [developer version](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:how_to_install#development_installation) of jqGrid. – Oleg Feb 04 '12 at 12:41
  • Would you be able to help me if I use this instead ? https://sites.google.com/site/azghanvi/codebase – Kaippally Feb 04 '12 at 12:42
  • @Kaippally: I found in [the demos](http://www.trirand.net/demophp.aspx) that you can use `$grid->setGridEvent` to set JavaScript callback function in the PHP code. Open 'PHP' tab on [the page](http://www.trirand.com/blog/phpjqgrid/examples/selection/masterdetail/default.php) for an example. – Oleg Feb 04 '12 at 12:46
  • @Kaippally: I wrote you before, that I don't use PHP myself and don't know different products which are based of JavaScript version of jqGrid. Sorry. – Oleg Feb 04 '12 at 12:48
  • thanks any way. I guess I'll wait for someone with php experience. – Kaippally Feb 04 '12 at 13:00