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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Alphabetically display DataObjects


Go to End


9 Posts   2274 Views

Avatar
Stefdv

Community Member, 110 Posts

17 November 2010 at 10:30pm

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

18 November 2010 at 4:48am

Edited: 18/11/2010 4:49am

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

Avatar
Stefdv

Community Member, 110 Posts

18 November 2010 at 5:03am

Edited: 18/11/2010 5:28am

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.

Avatar
Stefdv

Community Member, 110 Posts

18 November 2010 at 5:24am

Edited: 18/11/2010 5:28am

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 .

Avatar
UncleCheese

Forum Moderator, 4102 Posts

18 November 2010 at 5:30am

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

18 November 2010 at 5:31am

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.

Avatar
Stefdv

Community Member, 110 Posts

18 November 2010 at 5:39am

Edited: 18/11/2010 6:07am

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>

Avatar
Stefdv

Community Member, 110 Posts

18 November 2010 at 5:41am

UncleCheese,

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

Its working great now

Thank u so much.

Go to Top