Is there a way to implement 2 different edit form in jqGrid. I mean one normal usual edit form and the other with a much lesser form field (customized). both in the same navGrid. say edit and editpartial. can anyone throw some light?? many thanks.
The buttons "Add" and "Edit" added by navGrid call both editGridRow method with different parameters as the second parameter (properties parameter). In case of usage navGrid called as
$("#grid").jqGrid("navGrid", "#pager", {parameters}, prmEdit, prmAdd);
then navGrid calls
$("#grid").jqGrid("editGridRow", rowid, prmEdit);
if the user click "Edit" button and calls
$("#grid").jqGrid("editGridRow", rowid, prmAdd);
if the user click "Add" button.
In the same way you can add new custom button to navigator with respect of navButtonAdd method. For example
$("#grid").jqGrid("navButtonAdd", "#pager", {
caption: ""/*"My Edit"*/,
buttonicon: "ui-icon-note", // some icon
title: "My edit",
onClickButton: function() {
var $self = $(this);
$self.jqGrid("editGridRow",
$self.jqGrid("getGridParam", "selrow"),
{ // some options
recreateForm: true
}
);
}
});
In the way you add one more Edit button. The only thing which you need do now is temporary changing of editable property of some columns before call of editGridRow and resetting it to original value after call of editGridRow. You can use setColProp method to simplify the work. For example if you want to make myColumn column editable you can use
$("#grid").jqGrid("navButtonAdd", "#pager", {
caption: ""/*"My Edit"*/,
buttonicon: "ui-icon-note", // some icon
title: "My edit",
onClickButton: function() {
var $self = $(this);
// make "myColumn" temporary editable
$self.jqGrid("setColProp", "myColumn", {editable: true});
$self.jqGrid("editGridRow",
$self.jqGrid("getGridParam", "selrow"),
{ // some options
recreateForm: true
}
);
// make "myColumn" back as non-editable
$self.jqGrid("setColProp", "myColumn", {editable: false});
}
});
I want to emphasize that usage of recreateForm: true option for all usage of editGridRow (inclusive navGrid) is very important. So I recommend you just change default value of the option:
$.extend($.jgrid.edit, { recreateForm: true });
Related
I am using form editing. I would like to disable certain fields in my add and edit forms based on the selection from a drop down box. What event is best to use to trigger this? I have attempted using dataEvents:
{ name:'type_cd',
edittype:'select',
editoptions:{
dataUrl:'functions.php',
dataEvents:[{
type:'change',
fn: function(e){
$(this).setColProp('cntrct_id',{
editoptions:{editable:false}
});
}
}]
}
},
This has no visible effect on my form fields, but I know that it's being reached because I can get an alert message from it if I put one in.
EDIT
If I submit the form, the next time I open it, the column that was set to editable:false will not appear. This is a step in the right direction, BUT I want it to immediately be uneditable. Really, I would like it to be visible, but disabled (i.e. disabled:true)
First of all dataEvents allows you to register callbacks on elements of edit elements. Inside of callbacks this will be initialized to the DOM element which will be bound. So $(this) inside of change handler it will be wrapper on <select> element and not on the grid. The usage of $(this).setColProp will be incorrect.
To disable some input field in Add/Edit form you can use the fact that all input elements get the same id like the value of name property on the corresponding column in colModel. So if you need to disable input of cntrct_id you can set disabled property to true on the element with id="cntrct_id"
{
name: 'type_cd',
edittype: 'select',
editoptions: {
dataUrl: 'functions.php',
dataEvents: [{
type: 'change',
fn: function (e) {
// disable input/select field for column 'cntrct_id'
// in the edit form
$("#cntrct_id").prop("disabled", true);
}
}]
}
}
It's important to understand that editoptions will be used for any existing editing modes (form editing, inline editing and cell editing). If you want to write the code of dataEvents which supports all editing modes you have to detect editing mode and use a little other ids of editing fields. The code (not tested) can be about as below
{
name: 'type_cd',
edittype: 'select',
editoptions: {
dataUrl: 'functions.php',
dataEvents: [{
type: 'change',
fn: function (e) {
var $this = $(e.target), $td, rowid;
// disable input/select field for column 'cntrct_id'
if ($this.hasClass("FormElement")) {
// form editing
$("#cntrct_id").prop("disabled", true);
} else {
$td = $this.closest("td");
if ($td.hasClass("edit-cell") {
// cell editing
// we don't need to disable $("#cntrct_id")
// because ONLY ONE CELL are edited in cell editing mode
} else {
// inline editing
rowid = $td.closest("tr.jqgrow").attr("id");
if (rowid) {
$("#" + $.jgrid.jqID(rowid) + "_cntrct_id")
.prop("disabled", true);
}
}
}
}
}]
}
}
The last remark. If you still use old version of jQuery (before jQuery 1.6) which don't support prop method you have to use attr instead.
#Oleg: This is working(could get the alert messages) but its not disabling the field.
Should the form field require any special values?
I have a jqGrid (using inline editing) with an autocomplete column. When the user selects a value from the autocomplete column, an event handler sets a value on another column, and also sets the value on the autocomplete column to something other than the label returned from the autocomplete source. The two column definitions (complete jsFiddle example here):
{
name: 'cartoonId',
index: 'cartoonId',
width: 90,
editable: false},
{
name: 'cartoon',
index: 'cartoon',
width: 200,
editable: true,
edittype: 'text',
editoptions: {
dataInit: function(elem) {
$(elem).autocomplete({
source: autocompleteSource,
select: function(event, ui){
var rowId = $("#inlineGrid").jqGrid('getGridParam', 'selrow');
if(ui.item){
$("#inlineGrid").jqGrid('setCell', rowId, 'cartoonId', ui.item.CartoonId);
$("#inlineGrid").jqGrid('setCell', rowId, 'cartoon', ui.item.Name);
}
return false;
}
});
}
}},
The problem is that whenever the user selects a value from the autocomplete, either by clicking it or using arrows and pressing the tab key, that cell is no longer editable, and the grid appears to lose focus entirely. If I comment out the line that sets the cartoon cell value, it behaves normally. Is there any way I can get around this behavior? I need the entire row to remain in edit mode, including the cartoon column, until the user completes the edit.
jqGrid 4.4.1
jQuery 1.7.2
jQuery UI 1.8.18
You should rename Name property of the items from the autocompleteSource to value because jQuery UI Autocomplete examines the label and value per default (see the documentation).
You can't use setCell of the 'cartoon' column which is currently in the editing mode. You should remove return false; from select callback too. So the code could looks about the following
dataInit: function (elem) {
$(elem).autocomplete({
source: autocompleteSource,
select: function (event, ui) {
var rowId = $("#inlineGrid").jqGrid('getGridParam', 'selrow');
if (ui.item) {
$("#inlineGrid").jqGrid('setCell', rowId, 'cartoonId',
ui.item.CartoonId);
}
}
});
}
See http://jsfiddle.net/27dLM/38/
I want to write code on Refresh button click of JQGrid. Is there any event for that?
If you need to do some actions before refresh will be started you should use beforeRefresh callback:
$("#grid_id").jqGrid('navGrid', '#gridpager', {
beforeRefresh: function () {
// some code here
}
});
If you need absolute another implementation of grid Refreshing where you will not call $("#grid_id").trigger("reloadGrid"); (which sound strange) you can do this by the usage of refresh: false option to remove the standard Refresh button and using navButtonAdd to add your custom button which looks exactly like the original one:
$("#grid_id").jqGrid('navGrid', '#gridpager', {refresh: false});
$("#grid_id").jqGrid('navButtonAdd', "#gridpager", {
caption: "", title: "Reload Grid", buttonicon: "ui-icon-refresh",
onClickButton: function () {
alert('"Refresh" button is clicked!');
}
});
The css for refresh button is ui-icon-refresh
so you can write your custom code on this css like
jQuery('.ui-icon-refresh').click(function(){
// do your work
});
jqGrid Edit form invoked using Edit button from top toolbar is defined as
grid.navGrid("#grid_toppager", { del: false,add: false,view: true },
{ url: '/Edit',
bSubmit: 'Save',
editCaption: 'Change',
} );
This is used to show data in readonly grid. Pressing submit button does not make sence in this case.
How to remove submit button ?
You can hide Submit button the in the beforeShowForm of the edit form options:
beforeShowForm: function ($form) {
$form.parent().find('#sData').hide();
}
i've added custom button on the footer of jqgrid for adding record. The code is as under:
$("#jqgUsers").navGrid('#jqgpUsers', { edit: false, add: false, del: false, search: false })
.navButtonAdd('#jqgpUsers', {
caption: "",
buttonicon: "ui-icon-plus",
onClickButton: function () {
alert("Adding Row");
},
position: "last"
});
How can I open jqGrid add form while click on that add button?
You should just call the editGridRow manually with "new" as the first parameter (see here the documentation)
$(this).jqGrid("editGridRow", "new");
or with any properties or events like for example
$(this).jqGrid("editGridRow", "new", {recreateForm: true, closeAfterAdd: true});
I'm adding an additional answer because I have a different scenario with a beforeShowForm event defined in my pager. In order to make sure the add row settings and events in my pager definition are used when the add row dialogue is opened with a custom button click I called
`$('#add_' + your_grid_name_here).click();`
Which will make sure your grid's pager settings are used. It's works the same as if the user pressed the '+' button in the pager.