Pass RowData to Custom_func in Jqgrid - jqgrid

I have a jqgrid with certain columns. I am trying to call a custom_function to validate the cell value.
I am also getting a regular expression from the database into rowData, which I want to use to validate the cell value.
var ret = jQuery("#settingsListGrid").jqGrid('getRowData', id);
cm.editrules = {
required: true,
custom: true,
custom_func: ValidateData,
custom_value:ret.RegX
};
So I need to pass rowData to a custom function.
ValidateData = function (value, colname, customValue) {
return customValue.test(value) ?
[true] :
[false, "Invalid Data"];
}
I want to pass the rowData using customValue
Please help?

The answer depend on the fork of jqGrid, which you use. I understand the problem which you wrote, but one have to change the code of jqGrid (the implementation of custom validation) to implement the requirement.
I develop free jqGrid fork of jqGrid after Tony Tomov changed the license agreement of jqGrid, renamed his product in version 4.7.1 to Guriddo jqGrid JS (see the post) and made it commercial (see the prices here). After starting the development based on the last free 4.7 version I made a lot of changes and improvements in the code and have implemented many new features. The feature which you need is implemented starting with version 4.12.1 (see here). Thus you can easy solve your problem after updating to the current free jqGrid 4.13.2.
The new feature works as following:
editrules: {
required: true,
custom: ValidateData,
custom_value: ret.RegX
}
It's important that one should specify the custom validation function as the value of custom property instead of usage custom_func. It allows free jqGrid to hold compatibility to old versions (via custom: true and custom_func), but providing new parameters of the validation callback via function as the value of custom property.
The new style ValidateData will look like
var ValidateData = function (options) {
return customValue.test(options.newValue) ?
[true] :
[false, "Invalid Data"];
}
with only one options parameter, which have many properties which you could use. Such style allows to provide many helpful information without the requirement to have a lot of unneeded parameters. Moreover the style of callback options allows to extend the options object in the future versions without breaking compatibility to previous versions.
The options parameter have the following properties
newValue - the current (modified) value which need be validated
oldValue - the previous value (the old value) of the cell before the modification
cmName - the column name. It could be practical if you use one callback function in many columns and you want to implement a little different behavior for different columns. It could be additionally helpful for producing readable error message in case of validation error.
iCol - the index in the current colModel which corresponds the column (the column cmName)
cm - the element of colModel, which represent the currently validating column.
rowid - the rowid of the current editing row. One can use getLocalRow for example to get the content of other columns before editing. It's important to remark that getLocalRow works only in case of usage datatype: "local" or loadonce: true. The method getRowData or getCell can be safe used to get data in form editing mode or to access the data, which are not currently editing (in cell or inline editing mode).
iRow - numeric index of the current editing row from the top of the grid (from the top of HTML <table>)
oldRowData - will be set only in case of usage inline editing or cell editing. It's not defined in form editing mode. It represent the values
mode - shows which editing mode is used now. It can be "addForm", "editForm" (in case of usage form editing), "cell" (cell editing), "add" or "edit" (inline editing). In some other callback function the property could have two other values: "filter" (field from the filter toolbar) or "search" (for validation of the field of the searching dialog)
tr and td - the DOM elements of the row and cell of the grid which will be edited using form editing mode. The properties will be set only in case of form editing.
I hope that the large set of properties of options parameter allows you easy implement your requirements on any custom validation.

Related

how can i get inbuilt value during Add process of jqgrid?

I am using jqGrid and when I want to enter new values,I click on Add(+) button which is at bottom of grid.I have 7 filed and out of them 1 is dropdown list so,
whenever I want to add new record I want that dropdown field to be filled automatically. How do I achieve that ?
I'm not sure what you exactly mean. Du you use inline editing (inlineNav) or form editing (navGrid)? Which version of jqGrid you use and from which fork of jqGrid (free jqGrid, which I develop, commercial Guriddo jqGrid JS or an old jqGrid in version <=4.7). You wrote about "dropdown field to be filled automatically", but the information can be interpreted in different ways. I try to guess.
I suppose that you should include defaultValue in editoptions to fill (or to choose in the dropdown) the default value in the filed new filed. It will work in case of usage form editing. It could work in some cases of usage inline editing (inlineNav) too, but one should know more details about your implementation.

Condtional editable depend on the value in colModel jqGrid

I am trying to put editable value either true or false depend on the value of the cell in colModel. Is there is any way to do that?
{name:'keywords',index:'keywords', width:150, editable:true,edittype:'select',
editrules:{required: true},
editoptions:{value:{'one':'one','two':'two','three':'three','four':'four'},size:5} },
In this I want to set editable:false if the cellvalue exist else editable:true and editoptions:{value:{'one':'one','two':'two',...} for a particular cell.
The most easy way to implement the requirement would be to use free jqGrid. It's the fork (based on jqGrid 4.7) which I develop currently. Because of such requirements I implemented the feature described in the wiki article. It allows to define editable property as function. So you need just to define the callback from which you returns true or false base on any your custom criteria. The callback editable get many information about the context of execution as properties of the only parameter. The rowid, iRow, iCol and name properties allows you to use getLocalRow or getCell to get contain of any column of the row. So you can enable editing selectively in very simple way.
If you can't upgrade to free jqGrid then you can use old tricks which are specific for the editing mode which you use. I can forward you to the answer which can be used in case of form editing and this one which can be used in case of form editing if you call editRow directly. In case of usage inlineNav or formatter: "actions" there are alternative solutions which you can find also in my old answers.

jqGrid method setGridParam on event subGridRowExpanded causes error

I successfully configured a subgrid including the subGridRowExpanded callback in my grid.
This means the callback and config values are valid.
I want to remove the subgrid configuration now in order to add it programmatically.
First step - set subgrid to true:
jQuery('#s3list').jqGrid('setGridParam', {
subGrid: true
});
This causes the following error:
TypeError: this.p.subGridOptions is undefined.
Second step - adding required subGridOptions:
jQuery('#s3list').jqGrid('setGridParam', {subGridOptions: {
reloadOnExpand: false
}});
This causes the following error:
TypeError: b.p.colModel[(((n + x) + C) + H)] is undefined
Adding my valid callback for subGridRowExpanded has no effect - the error stays the same:
jQuery('#s3list').jqGrid('setGridParam', {
subGridRowExpanded: function(subgrid_id, row_id) {
/* lots of valid code */
}
});
Is it possible at all?
Using jqGrid 4.5.2.
Related issues / questions:
https://github.com/tonytomov/jqGrid/issues/478
jqGrid dynamic event
How to programatically add an event handler on a JQGrid?
How to reload JQuery grid keeping scroll position and collapse elements open
Add an event handler to jqGrid after instantiation
I suppose that there are misunderstanding what subGrid: true option do. jqGrid add column with the namse subgrid if you create grid with the option. Because you can't add new column dynamically in jqGrid you can't switch on subGrid option in the grid without recreating of it with respect of GridUnload method (see the answer).
The method setGridParam is stupid. It just use $.extend to extend the internal option of jqGrid (see the source code). Usage of the method without taking in consideration the specific of the option can break functionality of the grid.
There are some scenarios when one can create grid with subgrid and hide the column with (see the answer and this one). You should consider in details whether such approach is acceptable for your requirements.

JQGrid-Trirand. How to Edit Fields?

I have one page where i have bind data by using JqGrid, but My requirement is In editing one row of a grid, the editable fields must NOT BE builtin fields( builtin textboxs) i want to bind that to other textboxes, check boxes which are outside grid. The data which i want to edit must be supplied to other input controls other than jqgrid builtin input controls, Finally IS THIS POSSIBLE???
Hope iam clear with my question.
Please do help me in reply saying either YES or NO or How, and why.
edited
I am not sure that I understand correctly what you mean, but it seems that you should just use form editing mode. To activate it on the client side you should just add editable: true property to all columns which are editable or use the option cmTemplate: {editable: true} which makes default value of editable for all columns as true. After it you can for example use navGrid to add Add, Edit and Delete buttons in the pager. The functionality on the client side will be ready after that. Now you have to implement editing part in the server code only. If you use commercial version of jqGrid like jqSuite you should address to the documentation or demos for more details.
try this
http://www.trirand.net/examples/grid/selection/selectedrow_client/default.aspx
or
Try this
<ClientSideEvents BeforeEditDialogShown="beforeEdit"/>
<script type="text/javascript">
function beforeEdit(rowID) {
var grid = jQuery("#<%= JQGrid1.ClientID %>");
lastSelectedRow = grid.getGridParam("selrow");
};
</script>

Post non editable values in jqGrid?

I'm using jqGrid, and I would like to post non editable values to the server. I'm editing row by row (not form). Is that possible?
The column is visible, and I'm using inline editing. The data is posted using "editurl" property of the grid.
** Solution **
I solved it in a completely different way, by not using jqGrids setCell, but instead setting the textbox value using document.getElementById(selr + "_Verksamhetskod").value = data.
Not exactly what I had in mind initially, but it works...
Just add that to your cell configuration:
editable: true, editoptions: {disabled: true}
Well if I'm understanding correctly you are trying to make a cell become read-only once it has a value but still be in a format where you can post to the server correctly.
You could use Jquery to place a read-only attribute on each input field
$("#PrimaryKey".columnName").attr("disabled", true)
and either fire this code if there is a value in the input field or after an input has been entered.

Resources