Silverstripe - Load search results in Model Admin automatically
I just spent the evening looking at various solutions to this. Originally I had been pointed to this ticket, but this solution did not quite work for me. After shopping around, including this post on css-tricks, and also this on the Silverstripe forum, I concocted this little jQuery script:
ModelAdmin_improvement.js
(function($) {
$(document).ready(function() {
$('#Form_SearchForm_Therapist').submit();
$("#ModelClassSelector").change(function() {
var modelName = $("#ModelClassSelector select option:selected").val();
var strFormname = "#Form_Search" + modelName;
$(strFormname).submit();
})
})
})(jQuery);
Then of course, in your Subclass of ModelAdmin.php you need to add it as a requirement in the init() function:
function init()
{
parent::init();
Requirements::javascript("mysite/javascript/ModelAdmin_improvement.js");
}
Like this when you first load the ModelAmin in the CMS, results for the default model class will automatically show. Also, when you select a different model from the dropbox options, results for that will be loaded automatically.
Comment by Peter Bacon Darwin 27/06/2011
You can remove the hard-coded "Therapist" with this code:
(function($) {
$(document).ready(function() {
var modelName = $("#ModelClassSelector select option:selected").val();
var strFormname = "#Form_Search" + modelName;
$(strFormname).submit();
$("#ModelClassSelector").change(function() {
var modelName = $("#ModelClassSelector select option:selected").val();
var strFormname = "#Form_Search" + modelName;
$(strFormname).submit();
})
})
})(jQuery);