Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Alphabetically display DataObjects

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 1345 Views
  • Stefdv
    Avatar
    Community Member
    110 Posts

    Alphabetically display DataObjects Link to this post

    I know there's been some discussion about this in the past, there's even a recipe about this issue but i just don't get it to work.

    I have a lot of Movies (about 200+) and TV Series (about 180+) they're both DataObjects.
    I used the tutorials
    " http://ssbits.com/tutorials/2010/dataobjects-as-pages-part-1-keeping-it-simple/ "
    to make everything work as i need but my sidemenu is getting waaaaay to long.

    I need to sort them out in some kind of ABC, so i don't want the whole listing in the sidebar. I want to have some kind of Navbar on top of my page witch will show [ 0 - 9 | A - C | D - F etc. ] and then when i select one of those it will show only those movies.

    Now i've been through the recipe http://doc.silverstripe.org/recipes:alphabetical_dataobjectset but i don't get it to work.

    I could really use an (extended) example.

    Tx in advance

  • UncleCheese
    Avatar
    4085 Posts

    Re: Alphabetically display DataObjects Link to this post

    That's super easy..

    static $ranges = array (
    '0-9',
    'A-D',
    'E-G',
    // etc...
    );

    public function AlphaLinks() {
    $set = new DataObjectSet();
    foreach(self::$ranges as $range) {
    $set->push(new ArrayData(array(
    'Link' => HTTP::setGetVar('range', $range),
    'Current' => $this->getRequest()->getVar('range') == $range,
    'Label' => $range
    )));
    }
    return $set;
    }

    public function AlphabeticalItems() {
    $r = $this->getRequest()->getVar('range');
    if(!$r) {
    // default range to show
    $r = "A-D";
    }
    list($start, $end) = explode("-", $r);
    return DataObject::get("SomeDataObject", "Title > '$start' AND Title < '$end'");
    }

    template

    <% control AlphaLinks %>
    <a href="$Link" <% if Current %>class="current"<% end_if %>>$Label</a>
    <% end_control %>

    <% control AlphabeticalItems %>
    $Title
    <% end_control %>

    Can't promise it's free of parse errors, but that should get you started..

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

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Alphabetically display DataObjects Link to this post

    LOL UncleCheese,

    Sure...for you its easy. For me its ...well i give it a go.

    I have absolutely no idea what you are doing with that exploding thing but i will try to figure it out.

    anyway, as allways thank u so much for your quick reply.

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Alphabetically display DataObjects Link to this post

    Wauw UC,

    That sure gives me a great deal of what i was looking for. The only thing is that its giving me the ID instead of the name of the Serie ( SerieName in my case ).

    Could you help me out with that also please?

    tx

    btw; i figured out the exploding thing .

  • UncleCheese
    Avatar
    4085 Posts

    Re: Alphabetically display DataObjects Link to this post

    Ha.. I wrote "that's super easy" before I actually coded all that up. Agreed, it's not super easy, but it's really not a huge undertaking in the grand scheme of things.

    Quick PHP lesson:

    explode() is a function that breaks a string up into an array when given a certain delimiter. It's Javascript counterpart is split(), if you've ever used that.

    $str = "One/Two/Three/Four";
    var_dump(explode("/", $str));

    // returns:
    array (
    'One', 'Two', 'Three', 'Four'
    )

    list() is a weird function that takes all the members of an array and makes them their own variables..

    list($one, $two, $three, $four) = explode("/", $str);

    echo $two;

    // returns "Two"

    So in my example, you're exploding "A-D" to be an array of "A" and "D"
    list($start, $end) will give you:

    echo $start; // "A";
    echo $end; // "D";

    Make sense?

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Alphabetically display DataObjects Link to this post

    What do you mean "it's giving me the ID".. where? On the template?

    If you don't have a "Title" field, replace that with whatever you're using as a label.

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Alphabetically display DataObjects Link to this post

    UncleCheese,

    Do you get payed for all your advice? My God this must be the best support forum i have ever encountered.

    Anyway,

    on my frontend it shows the 0-9, a-d etc. links, but when i click on one of them it returns the ID of the corresponding DataObjects.

    I did change

    <Code> return DataObject::get("SomeDataObject", "Title > '$start' AND Title< '$end'"); </Code>

    to

    <Code> return DataObject::get("Serie", "SerieName > '$start' AND SerieName < '$end'"); </Code>

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Alphabetically display DataObjects Link to this post

    UncleCheese,

    I am so sorry, i did not change Title to SerieName in the Template.

    Its working great now

    Thank u so much.

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