I have defined a beforeSubmit
method in a jqGrid like this: (notice I'm using AngularJS, which provides $http
service to get and post data)
beforeSumit: function(data) {
// I need this to be filled with default data atomatically
if(data.someField == "") {
$http.get('defaultValue/' + data.id).success(function(getData) {
// I put the default value into the data model returned to server
data.someField = getData;
// return sentence, jqGrid should continue saving without message
return [true, null];
}).error(function() {
// return sentence, jqGrid should keep the edit form, showing a message
return [false, "Couldn't get data from server"];
});
} else {
// return sentence, jqGrid should continue saving without message
return [true, null];
}
}
What I'm doing is to check if someField
is empty, and if it's empty I get a default value from server.
The problem is that return
statement is throwing an undefined method
error when I uses it inside the success/error of $http.get
.
Is it posible to return a value there? I need to notify jqGrid if it should continue the process of saving data or not...
EDIT: Thanks to @Oleg and @Sacho comments I've realised I can't use asynchronous functions inside beforeSubmit
, so finally I have put the $http
call in the onselectrow
event of jqGrid.
Now I need to ask the user, beforeSubmit
edit form, if wants to set the default value or wants to go back to the edit form and write a custom value for the field.
I can do it with a modal using $modal
AngularJS's service but I'm afraid it's asynchronous too so I get the same problem than before.
Is there a way to open a modal synchronously? It doesn't matter if the browser frezees as the process won't continue until the user decides.
This is the code now:
beforeSubmit: function(data) {
// I get this value in the onselectrow event
var defaultValue = $scope.defaultalue;
var modalInstance = $modal.open({
templateUrl: 'app/partials/selectOptionDialog.html',
controller: 'SelectOptionDialogCtrl',
windowTemplateUrl: 'app/partials/modalTemplate.html',
size: 'sm',
resolve: {
defaultValue: function() {
return $scope.defaultValue;
}
}
}).result.then(function(resolution) {
if (resolution == true) {
// user wants to put default value
data.someField = defaultValue;
// jqGrid should continue
return [true, null];
} else {
// jqGrid should go back to edit form with error message
return [false, "Please, fill the 'Archive Folder Name' field"];
}
});
}
The modal is a window with some test including the defaultValue
and two buttons, one which closes the modal with value true
(save) and other which closes the modal with value false
(go back).
SOLUTION: After all, it's not posible to do what I want the way I want.
Finally, I get the default value when users click a row using jqGrid event onSelectRow
.
I've replaced jqGrid's edit button with a custom one. What my edit button does is to check if the row has the field that I want to check empty. If so, the dialog to ask the user is shown and when the user selects an option the edit form is shown (with the field filled if user selected "Set default value" or not if selected "Another value".
I have changed colModel to have the field mandatory filled, so if user selects "Another value" in the dialog and then doesn't fill the field, an error will be shown in the jqGrid.