Jump to:

5539 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Howto : Default search results for ModelAdmin

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2489 Views
  • Martijn
    Avatar
    Community Member
    271 Posts

    Howto : Default search results for ModelAdmin Link to this post

    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());
    }

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: Howto : Default search results for ModelAdmin Link to this post

    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

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Howto : Default search results for ModelAdmin Link to this post

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

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Howto : Default search results for ModelAdmin Link to this post

    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.

  • Ingo
    Avatar
    Forum Moderator
    801 Posts
  • blueskies
    Avatar
    Community Member
    42 Posts

    Re: Howto : Default search results for ModelAdmin Link to this post

    Thanks Martijn! Very usefull!

  • qbahamutp
    Avatar
    Community Member
    8 Posts

    Re: Howto : Default search results for ModelAdmin Link to this post

    Reuze bedankt hiervoor, Martijn. You rock.

    2489 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.