In my jqGrid, I have a checkbox which is also available for editing, i.e. a user can click on the checkbox and that checkbox's value will be updated in the database. That is working fine. However when I click on the checkbox and if I try clicking on it again, nothing happens. The row does not get saved. Theoretically the unchecked value of the checkbox should be saved. But this does not happen.
I have tried referring to this answer of Oleg but it does not help.
The weird problem is if I select another row and then try to unselect the checkbox again, I do see a save request going.
I am guessing this is because I am trying to edit a row which is currently selected. But I am not sure what I am doing wrong here.
This is what I am doing in my beforeSelectRow
beforeSelectRow: function (rowid, e) {
var $target = $(e.target),
$td = $target.closest("td"),
iCol = $.jgrid.getCellIndex($td[0]),
colModel = $(this).jqGrid("getGridParam", "colModel");
if (iCol >= 0 && $target.is(":checkbox")) {
if (colModel[iCol].name == "W3LabelSelected") {
console.log(colModel[iCol].name);
$(this).setSelection(rowid, true);
$(this).jqGrid('resetSelection');
$(this).jqGrid('saveRow', rowid, {
succesfunc: function (response) {
$grid.trigger('reloadGrid');
return true;
}
});
}
}
return true;
},
Configuration:
jqGrid version: Latest free jqGrid
Data Type: Json being saved to server
Minimal Grid Code: jsFiddle
EDIT: After Oleg's answer this is what I have so far:
beforeSelectRow: function (rowid, e) {
var $self = $(this),
iCol = $.jgrid.getCellIndex($(e.target).closest("td")[0]),
cm = $self.jqGrid("getGridParam", "colModel");
if (cm[iCol].name === "W3LabelSelected") {
//console.log($(e.target).is(":checked"));
$(this).jqGrid('saveRow', rowid, {
succesfunc: function (response) {
$grid.trigger('reloadGrid');
return true;
}
});
}
return true; // allow selection
}
This is close to what I want. However if I select on the checkbox the first time and the second time, the console.log
does get called everytime. However the saveRow
gets called only when I check the checkbox and then click on it again to uncheck it the first time and never after that. By default the checkbox can be checked or unchecked based on data sent from server.
In the image, the request is sent after selecting the checkbox two times instead of being sent everytime.
UPDATE: As per @Oleg's suggestion, I have implemented cellattr
and called a function inside. In the function I simply pass the rowid and update the checkbox of that rowid on the server.
Here's the code I used:
{
name: 'W3LabelSelected',
index: 'u.W3LabelSelected',
align: 'center',
width: '170',
editable: false,
edittype: 'checkbox',
formatter: "checkbox",
search: false,
formatoptions: {
disabled: false
},
editoptions: {
value: "1:0"
},
cellattr: function (rowId, tv, rawObject, cm, rdata) {
return ' onClick="selectThis(' + rowId + ')"';
}
},
and my selectThis function:
function selectThis(rowid) {
$.ajax({
type: 'POST',
url: myurl,
data: {
'id': rowid
},
success: function (data) {
if (data.success == 'success') {
$("#list").setGridParam({
datatype: 'json',
page: 1
}).trigger('reloadGrid');
} else {
$("<div title='Error' class = 'ui-state-error ui-corner-all'>" + data.success + "</div>").dialog({});
}
}
});
}
I think there is a misunderstanding in the usage of
formatter: "checkbox", formatoptions: { disabled: false }
. If you creates non-disabled checkboxs in the column of the grid in the way then the user just see the checkbox, which can be clicked and which state can be changed. On the other side nothing happens if the user changes the state of the checkbox. On the contrary the initial state of the checkbox corresponds to input data of the grid, but the changed checkbox makes illusion that the state is changed, but nothing will be done automatically. Even if you usedatatype: "local"
nothing is happens and even local data will be changed on click. If you do need to save the changes based on the changing the state of the checkbox then you have to implement additional code. The answer demonstrates a possible implementation. You can change the state of some checkboxes on the corresponding demo, then change the page and go back to the first page. You will see that the state of the checkbox corresponds the lates changes.Now let us we try to start inline editing (start
editRow
) on select the row of the grid. First of all inline editing get the values from the rows (editable columns) using unformatter, saves the old values in internalsavedRow
parameter and then it creates new editing controls inside of editable cells on the place of old content. Everything is relatively easy in case of using standard disabled checkbox offormatter: "checkbox"
. jqGrid just creates enabled checkboxs on the place of disabled checkboxs. The user can change the state of the checkboxs or the content of any other editable columns and saves the changes by usage of Enter for example. After that the selected row will be saved and will be not more editable. You can monitor the changes of the state of the checkbox additionally (by usageeditoptions.dataEvents
with"change"
event for example) and callsaveRow
on changing the state. It's important that the row will be not editable after the saving. If you do need to hold the row editable you will have to calleditRow
once more after successful saving of the row. You can calleditRow
inside ofaftersavefunc
callback ofsaveRow
, but I recommend you to wrap the call ofeditRow
inside ofsetTimeout
to be sure that processing of previous saving is finished. It's the way which I would recommend you.On the other side if you try to combine enabled checkboxs of
formatter: "checkbox"
with inline editing then you will have much more complex processing. It's important that enabled checkbox can be changed first of all before processing ofonclick
andonchange
event handlers. The order of 3 events (changing the state of the checkbox, processing ofonclick
and processing ofonchange
) can be different in different web browsers. If the methodeditRow
be executing it uses unformatter of checkbox-formatter to get the current state of the checkbox. Based of the value of the stateeditRow
replace the content of the cell to another content with another enabled checkbox. It can be that the state of the checkbox is already changed, buteditRow
interpretsthe changes state
likethe initial state
of the checkbox. In the same way one can callsaveRow
only aftereditRow
. So you can't just usesaveRow
inside ofchange
handler offormatter: "checkbox", formatoptions: { disabled: false }
, because the line is not yet in editing mode.UPDATED: The corresponding implementation (in case of usage
formatter: "checkbox", formatoptions: { disabled: false }
) could be the following:See jsfiddle demo http://jsfiddle.net/OlegKi/HJema/190/