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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Sorting DOM objects on the front end


Reply


4 Posts   1031 Views

Avatar
theoldlr

Community Member, 103 Posts

21 October 2010 at 8:54am

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>

Avatar
UncleCheese

Forum Moderator, 4096 Posts

21 October 2010 at 9:01am

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

Avatar
theoldlr

Community Member, 103 Posts

21 October 2010 at 9:53am

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

Avatar
DrWebGuy

Community Member, 16 Posts

15 March 2011 at 6:09am

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