In a related question, I asked how to filter down to and auto-select a given row in a jqGrid. The solution I currently have for this is:
$(function () {
$('.relatedrecipe').click(function () {
// store off the value of the related recipe I want to switch to
var recipe = $(this).data('recipename');
// clear any filters on the grid
setTimeout(function () {
$("#recipegrid")[0].clearToolbar();
}, 50);
// set the recipe filter to the related recipe name and trigger the filtering
setTimeout(function () {
$('#gs_RecipeName').val(recipe);
$('#recipegrid')[0].triggerToolbar();
}, 200);
// auto-select the first row
setTimeout(function () {
var firstRowID = $('#recipegrid').jqGrid('getDataIds')[0];
$('#recipegrid').setSelection(firstRowId, true);
}, 500);
});
}
What I don't like about this solution (I do like that it provided me with a solution) is that I am, essentially, queuing up a bunch of functions to be run in the future, at 50ms, 200ms, and 500ms. This seems like a potentially problematic solution based on getting the timing right, so I don't like it much.
I've considered nesting these functions one inside another, with a 50ms time for each. Something like:
$(function () {
$('.relatedrecipe').click(function () {
// store off the value of the related recipe I want to switch to
var recipe = $(this).data('recipename');
// clear any filters on the grid
setTimeout(function () {
$("#recipegrid")[0].clearToolbar();
// set the recipe filter to the related recipe name and trigger the filtering
setTimeout(function () {
$('#gs_RecipeName').val(recipe);
$('#recipegrid')[0].triggerToolbar();
// auto-select the first row
setTimeout(function () {
var firstRowID = $('#recipegrid').jqGrid('getDataIds')[0];
$('#recipegrid').setSelection(firstRowId, true);
}, 50);
}, 50);
}, 50);
});
}
Is that better? I've modified my code to do it this way and it seems to work as well, but is there a better way to do this?
The steps need to occur in this order but I believe there needs to be some time for each of the the first two sections to finish before doing the third. Any thoughts on this?