Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Sorting DOM objects on the front end

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 926 Views
  • theoldlr
    Avatar
    Community Member
    103 Posts

    Sorting DOM objects on the front end Link to this post

    I built a FDOM like so:

    $manager = new FileDataObjectManager(
    $this, // Controller
    'Resources', // Source name
    'Resource', // Source class
    'Attachment', // File name on DataObject
    array(
    'Name' => 'Name',
    ), // Headings
    'getCMSFields_forPopup', // Detail fields (function name or FieldSet object)
    '',// Filter clause
    'Name ASC'// Sort clause
    // Join clause
    );


    The data objects are always sorted as I'd like in the CMS, but how do I get them to display the same way in the template?
    template:

    <table>
    <thead>
    <tr>
    <th>Name</th>
    <th>Link</th>
    <th>Size</th>
    </tr>
    </thead>
    <tbody>
    <% control Resources %>
    <tr class="$EvenOdd">
    <td class="left">$Name</td>
    <td><a href="$Attachment.URL" target="_blank">Download</a></td>
    <td>$Attachment.Size</td>
    </tr>
    <% end_control %>
    </tbody>
    </table>

  • UncleCheese
    Avatar
    4085 Posts

    Re: Sorting DOM objects on the front end Link to this post

    You can add a $default_sort property to your Resource object..

    static $default_sort = "Title ASC";

    --------------------
    SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

  • theoldlr
    Avatar
    Community Member
    103 Posts

    Re: Sorting DOM objects on the front end Link to this post

    Perfect. Thank you for the fast response (and your overall dedication to the SS community)!

  • DrWebGuy
    Avatar
    Community Member
    16 Posts

    Re: Sorting DOM objects on the front end Link to this post

    What if you are trying to sort by the "Attachment" instead of "Title". Since Attachment is part of the $has_one array instead of the $db array, it has no actual entry, but rather is represented by the "AttachmentID" field. How do I go about sorting by the actual attachment name?

    class Resource extends DataObject
    {
       static $db = array (
          'Name' => 'Text',
          'Description' => 'Text'

       );

       
       static $has_one = array (
          'Attachment' => 'File',
          'ReportPage' => 'ReportPage'
       );
       
       static $default_sort = "AttachmentID ASC"; // Doesn't actually sort by Attachment name
       
       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Name'),
             new TextareaField('Description'),
             new FileIFrameField('Attachment')
          );
       }
    }

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