Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

Howto : Default search results for ModelAdmin


Reply


7 Posts   2578 Views

Avatar
Martijn

Community Member, 271 Posts

26 August 2010 at 10:19am

Edited: 26/08/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());
}

Avatar
TotalNet

Community Member, 181 Posts

1 September 2010 at 5:18pm

Edited: 01/09/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

Avatar
Martijn

Community Member, 271 Posts

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....

Avatar
Martijn

Community Member, 271 Posts

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.

Avatar
Ingo

Forum Moderator, 801 Posts

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

Avatar
blueskies

Community Member, 42 Posts

19 March 2011 at 11:11am

Thanks Martijn! Very usefull!

Avatar
qbahamutp

Community Member, 8 Posts

24 November 2011 at 3:02am

Reuze bedankt hiervoor, Martijn. You rock.