5121 Posts in 1527 Topics by 1119 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 2153 Views |
-
Howto : Default search results for ModelAdmin

26 August 2010 at 10:19am Last edited: 26 August 2010 10:44am
Finally I found it!
For those who need default search results in ModelAdmin, this is how it can be easily achieved:Add this method to your ModelAdmin subclass:
function getEditForm(){
$defaultModel = 'YourDataObjectClassName';
$collectionController = $this->getCollectionControllerClass($defaultModel);
$collection = new $collectionController($this, $defaultModel);
return $collection->ResultsForm(array());
}Edit : this can be done with less code :
function getEditForm(){
return $this->bindModelController('YourDataObjectClassName')->ResultsForm(array());
} -
Re: Howto : Default search results for ModelAdmin

1 September 2010 at 5:18pm Last edited: 1 September 2010 6:19pm
Genius. Thank you for sharing
Edit
Small issue though, the "back" "Delete" and "Save" buttons do not appear when you select an object from the default list. Click search again and its fine. This is on a 2.4.0 dev server, will try on 2.4.1 -
Re: Howto : Default search results for ModelAdmin

2 September 2010 at 1:55am
Ah yes, I see it. It's because there is no history set, so the buttons will be hidden.
Not an easy fix. But let me dig into that....
-
Re: Howto : Default search results for ModelAdmin

2 September 2010 at 3:58am
Ok, this is posible to fix, but you will have to add an extra javascript file in your ModelAdmin Subclass to add a correct url to the history array.
//MyModelAdmin
function init(){
parent::init();
Requirements::javascript('/myfolder/javascript/MyModelAdmin.js');
}MyModelAdmin.js
(function($){
$(document).ready(function() {
// Hide goBack and goForward buttons on initial load
if(!this.future || !this.future.length) {
$('#Form_EditForm_action_goForward, #Form_ResultsForm_action_goForward').hide();
}
if(!this.history || this.history.length <= 1) {
$('#Form_EditForm_action_goBack, #Form_ResultsForm_action_goBack').hide();
}
/**
* This will create a url with data from the corresponding SearchForm of the Default DataObject.
* Don't use live to ensure this is run first on initial load.
*/
$('#right #Form_ResultsForm tbody td a:not(.deletelink,.downloadlink)').click(function(){
// Set history on initial load for default managed Model
if(!this.history || this.history.length < 1) {
var action = $('#Form_ResultsForm').attr('action').replace('ResultsForm?','SearchForm');
var form = $('form[action="'+action+'"]');
if(typeof $(form).attr('action') != 'undefined'){
var url = $(form).attr('action') + '?' + $(form).serialize();
$('#ModelAdminPanel').fn('addHistory', url);
}
}
});
})
})(jQuery);Please let me know if this works for you as well.
-
Re: Howto : Default search results for ModelAdmin

16 September 2010 at 2:45pm Last edited: 16 September 2010 2:45pm
Legend! It worked for me. I'd been looking / hacking around for a solution, and this is better than What i'd come up with. Well done!
-
Re: Howto : Default search results for ModelAdmin

16 October 2010 at 4:14pm
BTW, there's two tickets on that already:
http://open.silverstripe.org/ticket/3119#comment:4
http://open.silverstripe.org/ticket/5947#comment:4 -
Re: Howto : Default search results for ModelAdmin

19 March 2011 at 11:11am
Thanks Martijn! Very usefull!
-
Re: Howto : Default search results for ModelAdmin

24 November 2011 at 3:02am
Reuze bedankt hiervoor, Martijn. You rock.
| 2153 Views | ||
|
Page:
1
|
Go to Top |




