I have a datagrid with a pop-up modal dialog to update and insert records based on the Basic CRUD Application in the demo.
On my dialog form I have a checkbox field.
The corresponding database field is a MySQL tinyint which is 0=false and 1=true.
This numeric value has to be converted from and into the form's checkbox.prop('checked',false) or checkbox.prop('checked',true).
To set the value before opening the dialog, I code as follows ("IsActive" is my checkbox field = 1 or 0):
function editRecord(){
var row = $('#dg').datagrid('getSelected');
if (row){
$('#dlg').dialog('open').dialog('setTitle','Edit Class');
$('#fm').form('load',row);
//CHECKBOX START
if(row.IsActive=='1'){
$('#idformIsActive').prop('checked',true);
} else {
$('#idformIsActive').prop('checked',false);
}
//CHECKBOX END
url = 'data_update.php?class_id='+row.id;
}
}
That works fine.
THIS IS WHAT DOESN'T WORK AND I CAN'T WORK OUT WHY.
Translating the checkbox property back to a 1 or 0 when submitting the popup dialog edit form
function updateRecord(){
//CHECKBOX START
if ($('#idformIsActive').prop('checked') == true) {
$('#idformIsActive').val('1');
} else {
$('#idformIsActive').val('0');
}
//CHECKBOX END
$('#fm').form('submit',{
url: url,
onSubmit: function(){
return $(this).form('validate');
},
success: function(result){
var result = eval('('+result+')');
if (result.success){ //WORKS TO HERE - FORM SUBMITTED BUT DOESN'T CLOSE DIALOG AND RELOAD GRID
$('#dlg').dialog('close'); // close the dialog
$('#dg').datagrid('reload'); // reload the user data
} else {
$.messager.show({
title: 'Error',
msg: result.msg
});
}
}
});
}
For some unknown reason it ONLY works when the checkbox is checked so the if condition is true:
if ($('#idformIsActive').prop('checked') == true) {
$('#idformIsActive').val('1');
} else {
$('#idformIsActive').val('0');
}
What happens when the else clause fires (checkbox is unchecked) is:
- It correctly converts the checkbox property to a zero
- The dialog does NOTclose
- The datagrids does NOT reload
- The database is however correctly updated with the zero value
It's like this clause is failing when it shouldn't
if (result.success){
Any ideas? Thanks for your insights!