davidb
davidb

Reputation: 273

How to post a static value in jqgrid when using inline edit

i want to post a static value to server, something like this <input type="hidden" name="table_name" value="<?php echo $table_name; ?>"> the value is not a part of colmodel, i want to post it whenever the inline edit happens, this is my php file with jqgrid,

<head>
<script type="text/ecmascript" src="jq/jquery.min.js"></script>
<script type="text/ecmascript" src="jq/jquery.jqGrid.min.js"></script>
<script type="text/ecmascript" src="jq/grid.locale-en.js"></script> 
<link rel="stylesheet" type="text/css" media="screen" href="jq/jquery-ui.css"/>
<link rel="stylesheet" type="text/css" media="screen" href="jq/ui.jqgrid.css"/>
<meta charset="utf-8" />
</head>
<body>
<table id="rowed5"></table>
<script type="text/javascript"> 
var lastsel2
var mydata=<?PHP echo $json_data;?>;
jQuery("#rowed5").jqGrid({
    postData: {test_value:'<?PHP echo $table?>'},
    serializeCellData: function( post_to_server ) {
        var postParams  =  jQuery("#rowed5").jqGrid('getGridParam','postData');
        if(postParams.hasOwnProperty('test_value') ) {
            post_to_server['test_value'] = postParams.test_value;
        }
        return post_to_server;
    },
    datatype: "local",
    shrinkToFit: false,
    data: mydata,
    height: 320,
    autowidth:true,
    colNames:['RowID','status','note','Variant ID'],
    colModel:[ 
        {name:'id',index:'id', width:55, sorttype:"int",align:"center"},
        {name:'status',index:'status', width:150,align:"left", editable: true,
edittype:"select",editoptions:{value:"Exclude:Exclude"}},
        {name:'note',index:'note', width:200, sortable:false,editable: true,edittype:"textarea", editoptions:{rows:"2",cols:"10"}},
        {name:'v_id',index:'v_id', width:150,align:"left"}],
    /*onSelectRow: function(id){
        if(id && id!==lastsel2){
            jQuery('#rowed5').jqGrid('restoreRow',lastsel2);
            jQuery('#rowed5').jqGrid('editRow',id,true);
            lastsel2=id;
        }
    },*/
    editurl: "functions.php",
    cellEdit : true,
    cellsubmit : 'remote',
    cellurl : 'functions.php',
});
jQuery("#rowed5").jqGrid('filterToolbar', {stringResult: true, searchOnEnter: false, defaultSearch : "cn"});
</script>
</body>

i am able to post the colmodel values like:

if($_POST['oper']=='edit')
{
    $id = mysql_real_escape_string($_POST['id']);
} 

but i want to to post a static value which is not a part of colmodel.

error Uncaught TypeError: jQuery(...).jqGrid is not a function at HTMLTableElement.serializeCellData (cam.php:224) at HTMLTableElement.serializeFeedback (jquery.jqGrid.src.js:2278) at HTMLTableElement.<anonymous> (jquery.jqGrid.src.js:8478) at Function.each (jquery.min.js:2) at n.fn.init.each (jquery.min.js:2) at n.fn.init.saveCell (jquery.jqGrid.src.js:8403) at n.fn.init.$.fn.jqGrid (jquery.jqGrid.src.js:2643) at HTMLSelectElement.<anonymous> (jquery.jqGrid.src.js:8368) at HTMLSelectElement.dispatch (jquery.min.js:3) at HTMLSelectElement.r.handle (jquery.min.js:3)

Upvotes: 1

Views: 851

Answers (2)

Oleg
Oleg

Reputation: 221997

The main reason of your problem seems to me the option cellEdit : true together with inline editing methods like editRow inside of onSelectRow. jqGrid supports three main alternative editing modes: incline editing, cell editing and form editing. If you enable cell editing with respect of the option cellEdit: true then the callback onSelectRow will be never called. Cell editing means typically cell selection instead of row selection. Thus the usage of cellEdit: true prevent typically calling of onSelectRow callback. New option noCellSelection: true is introduced in free jqGrid 4.15.0, which will be soon released. It allows to combine row selection with cell editing, but calling of editRow inside of onSelectRow is not good even in case of cellEdit: true with noCellSelection: true, because it will cancel just started cell editing.

My short recommendation is following: you should decide, which one editing mode you want to use. In case of usage cell editing (cellEdit: true) you should remove unused onSelectRow and serializeRowData and to add serializeCellData or alternatively beforeSubmitCell to extend the data posted to the server. The code of serializeCellData could be the same, which you use for serializeRowData. Alternatively the callback beforeSubmitCell could return object {test_value: postParams.test_value} or {}. The object returned from beforeSubmitCell will be combined (extended) with the standard parameters of cell editing (see the old documentation).

Upvotes: 1

Tony Tomov
Tony Tomov

Reputation: 3277

Use postData jqGrid parameter or serailizeRowData grid parameter. All of these are described here and here

You may want to look at this stackoverflow post

EDIT:

In this case one possible solution is to use serializeRowData

jQuery("#rowed5").jqGrid({
    ...
    postData: {test_value:'<?PHP echo $table?>'},
    serializeRowData : function( post_to_server ) {
        var postParams  =  jQuery("#rowed5").jqGrid('getGridParam','postData');
        if(postParams.hasOwnProperty('test_value') ) {
            post_to_server['test_value'] = postParams.test_value;
        }
        return post_to_server;
    },
    datatype: "local",
    ...
 });

In order this to work, please be a sure that your variable $table has a value. The simple test if this work is to set a static value in postData object instead of variable.

jQuery("#rowed5").jqGrid({
    postData: {test_value:'mystaticvalue'},
...
});

Upvotes: 1

Related Questions