Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Extension: SortableDataObject

Our old forums are still available as a read-only archive.

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

Page: 1 2
Go to End
Author Topic: 4614 Views
  • UncleCheese
    Avatar
    4085 Posts

    Extension: SortableDataObject Link to this post

    What it is

    Too many times I have encountered clients who need arbitrary sorts on their data. Silverstripe currently doesn’t support reordering in table lists, and even if it did, if the data bleeds over one page it would be a serious pain.

    The SortableData extension makes any SortableDataObject sortable through drag-and-drop on the front end for admin users. As an example, let's consider an AboutPage that has a $has_many relation to StaffMember. The StaffMember objects are not pages -- just components of the AboutPage.

    How it works

    By changing the parent class from DataObject to SortableDataObject, the StaffMember table now has a Sort field and all the other built-in functionality to sort itself.

    Now just add some minor enhancements to the template, we can make them sortable. There are two options for this. To keep it simple, use a UL. If your layout is more complex, use DIVs.

    SortableDataObjects in a UL:

    <h3>Our Staff</h3>
    <% if StaffMembers %>
    {$StaffMembers.BeginSortableUL}
    <% control StaffMembers %>
    {$LI}$Name, <em>$Title</em>{$_LI}
    <% end_control %>
    {$StaffMembers.EndSortableUL}
    <% end_if %>

    SortableDataObjects in DIVs:

    <h3>Our Staff</h3>
    <% if StaffMembers %>
    {$StaffMembers.BeginSortableDIV}
    <% control StaffMembers %>
    {$DIV}
       <div class="staffProfile clearfix">
        $Photo.SetWidth(100)
        <div>
          <h4>$Name</h4>
          <h5>$Title</h5>
          <p>$Description</p>
        </div>
       </div>
    {$_DIV}
    <% end_control %>
    {$StaffMembers.EndSortableDIV}
    <% end_if %>

    Anything within the {$LI} or {$DIV} pseudo-tag will drag as a single entity.

    Note: The curly braces are not necessary, but they help to distinguish variables that contain content from those that contain structure only.

    Well, have at it. It's super-beta!

  • Tim
    Avatar
    Core Development Team
    201 Posts

    Re: Extension: SortableDataObject Link to this post

    Sounds cool, can you post some screenshots of it in action?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Extension: SortableDataObject Link to this post

    Sure. Not too much to show, but here you go.

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: Extension: SortableDataObject Link to this post

    Art Vandelay! baahahaha

    Another great extension.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Extension: SortableDataObject Link to this post

    Update: You can now add "/sort" to the URL of any Page (or subclass thereof) to get access to the sort features. Just makes it easier than going to /Security/login every time.

    e.g.

    http://www.mysite.com/about-us/sort

    If you don't have the perms specified in SortableDataObject::$sorting_permission, you'll get a login form that will redirect back to the page.

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Extension: SortableDataObject Link to this post

    Thanks for sharing your work, UncleCheese

    I see "sortability" more as a behaviour than a subclass of DataObject, hence it should really be a DataObjectDecorator which you can attach to objects without changing their inheritance chain.

    Its also mixing model and view in one class. The markup necessary for sorting should be rendered by a helper-method in a controller. Seems like you want to make sure the correct markup is used by wrapping it in PHP. In SilverStripe, we often just create default implementations ("best practices") for copy and paste usage. I think its a reasonable assumption for users to leave certain classes in their templates which trigger the sortable javascript functionality.

    Modifying sort order could be decorated onto Page_Controller so it can be used on any page, without requiring a SortableDataObject_Controller.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Extension: SortableDataObject Link to this post

    Funny you mention the decorator. I recently made a post in the site builders forum asking for someone to once and for all explain to me what a decorator is, and ever since I figured it out, I've been using them everywhere! Indeed, this is one of the first things that came to mind as a candidate for a decorator, and I'm going to re-release it soon.

    I'm unsure, however, if the required instance_get() overload will carry through to the decorator.

    I see what you're saying about mixing the markup into the model, and I rarely violate MVC in my work, but I just think this is a case where 99% of the time, the markup is going to be the same, so some "helper" functions are useful. Who wants to keep track of the class that the javascript library is going to be looking for? Might as well bundle it in. Users can always write their own instead. If you think about it, this is part of the reason why Ruby on Rails got so much hype. They were able to embrace MVC, but also recognize that using server-side functions to draw HTML could really streamline the development process without getting in the way.

    Anyway, I really appreciate the feedback. I'm definitely going to do this as a decorator. I love those things!

  • UncleCheese
    Avatar
    4085 Posts

    Re: Extension: SortableDataObject Link to this post

    Here's the revised version using decorators as you suggested, Ingo. Let me know what you think. I think it's a lot cleaner this way.

    4614 Views
Page: 1 2
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.