Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Help with dynamic article sorting (Solved - smd_switch)
Hello
I have a long list of articles, one per line, output (via article_custom) as:
name, category, date
.. currently as lines wrapped in <h2> tags (i.e. not an unordered list or a table)
and I’d like users to be able to sort the articles by name (alphabetical), category (alphabetical) and date (chronological) – by clicking either name, category or date.
I’m sure I can achieve this (with perhaps a combination of JQuery and smd_if) but I can’t quite figure out how.
Could anyone please suggest how I might do this?
Thanks for your help.
Last edited by spiffin (2011-02-03 16:42:16)
Offline
Re: Help with dynamic article sorting (Solved - smd_switch)
You could use a select box to choose the desired sort order and append that to the url as an url variable, e.g. ?sortcrit=category
.
Then use adi_gps to grab the urlvar from the url and save it in a txp:variable (it will do that for you automatically).
You should first check if the value returned is allowed (e.g. not some illegal string) using if_variable and your allowed values and if ok, you then use sort='<txp:variable name="sortcrit" /> asc'
(tag in tag with single quotes) in your article_custom sort attribute. Note: this method requires a page refresh to re-sort.
Another approach, seeing as you have your information all on one line, is to make a table out of your article list and use jquery datatables which has built-in table column sorting and more (such as pagination and search, but you can also dumb it down if you want less). The markup is no longer h2
s but the effect is the same and the table is not necessarily wrong semantically (i.e. you’re using a table as a tabular overview).
Last edited by jakob (2011-02-02 22:28:36)
TXP Builders – finely-crafted code, design and txp
Offline
Re: Help with dynamic article sorting (Solved - smd_switch)
Thanks Jakob .. much appreciated.
Offline
Re: Help with dynamic article sorting (Solved - smd_switch)
Okay, after some fiddling, I did it this way (using Stef’s wonderful smd_switch plugin):
1. Defined links (to sort on click) for name, category, date as:
<a href="../?sort=name" title="sort by name">name</a> <a href="../?sort=category" title="sort by category">category</a> <a href="../?sort=date" title="sort by date">date</a>
2. Used smd_switch to switch between 3 article_custom statements – each with a different sort order:
<txp:smd_switch item="?sort" look_in="urlvar">
<txp:smd_case value="name" type="eq">
<txp:article_custom section="archive" form="listing" sort="Title" />
</txp:smd_case>
<txp:smd_case value="category" type="eq">
<txp:article_custom section="archive" form="listing" sort="Category1" />
</txp:smd_case>
<txp:smd_case value="date" type="eq" default="1">
<txp:article_custom section="archive" form="listing" sort="Posted desc" />
</txp:smd_case>
</txp:smd_switch>
3. Sorted! Thanks Jakob and Stef.
Last edited by spiffin (2011-02-03 17:24:59)
Offline