Ruby on Rails
Monday, November 30, 2015
Several years ago, I had a similar desire to have a screen representation of a category tree. I was very surprised that I couldn't find any pre-baked solution, so I built my own (there may be one out there now of which I am unaware). Unfortunately, I have never got round to making my code public, but I can share some of the design considerations I had to consider.
1. What sort of tree would I like - I went for creating something along the lines of the windows file explorer - a pretty standard representation.
2. What elements would this require:
a. Database - a category structure - ie. Awesome nested set worked well for me.
b. Visual - simple graphics, small images for: node open, node closed, leaf
c. Next step was to build a simple mock up on the screen to create the CSS for the tree structure. HTML struture would be nested ul lists for branches and li elements for nodes and leafs. classes for the open and closed state. (I could share this if you like)
2. Perhaps the most difficult question is the decision of how much I am going to do in JS and how much server side. I considered supplying the whole tree in json format and then using js to build the visual and keep things in step. But it just seemed to get too involved. And took me more into js development than I was comfortable with.
3. The solution I used was to maintain the tree on the server, and open close, create and delete nodes using ajax calls. Rewriting sections of the tree by replacing nodes by rendering appropriate partials.
4. Giving each node an id of the category model object id allows the server to manage the tree by js responses that show/hide branches.
5. It can seem a bit of a daunting task, but if you break it down into the steps above, create open/closed partials that call each other, then build the ajax calls for open/close, it is actually not that hard, and I have a working solution that is common across three projects. Rails 2 and Rails 4.
Hopefully this at least gives some food for thought.
Tony
On Monday, 23 November 2015 21:13:57 UTC, Colin Law wrote:
-- 1. What sort of tree would I like - I went for creating something along the lines of the windows file explorer - a pretty standard representation.
2. What elements would this require:
a. Database - a category structure - ie. Awesome nested set worked well for me.
b. Visual - simple graphics, small images for: node open, node closed, leaf
c. Next step was to build a simple mock up on the screen to create the CSS for the tree structure. HTML struture would be nested ul lists for branches and li elements for nodes and leafs. classes for the open and closed state. (I could share this if you like)
2. Perhaps the most difficult question is the decision of how much I am going to do in JS and how much server side. I considered supplying the whole tree in json format and then using js to build the visual and keep things in step. But it just seemed to get too involved. And took me more into js development than I was comfortable with.
3. The solution I used was to maintain the tree on the server, and open close, create and delete nodes using ajax calls. Rewriting sections of the tree by replacing nodes by rendering appropriate partials.
4. Giving each node an id of the category model object id allows the server to manage the tree by js responses that show/hide branches.
5. It can seem a bit of a daunting task, but if you break it down into the steps above, create open/closed partials that call each other, then build the ajax calls for open/close, it is actually not that hard, and I have a working solution that is common across three projects. Rails 2 and Rails 4.
Hopefully this at least gives some food for thought.
Tony
On Monday, 23 November 2015 21:13:57 UTC, Colin Law wrote:
On 23 November 2015 at 21:11, fugee ohu <fuge...@gmail.com> wrote:
> how to format them on a page ... my thought was to use group_by for
> formatting a page that displays all categories
Do the tutorial.
Colin
>
> On Monday, November 23, 2015 at 3:39:05 PM UTC-5, Colin Law wrote:
>>
>> On 23 November 2015 at 20:11, fugee ohu <fuge...@gmail.com> wrote:
>> > I'm looking for a solution for managing categories My categories table
>> > has
>> > name , parent_id fields My first challenge is to display all categories
>> > on a
>> > single page and my next is to build select lists represenative of the
>> > tree
>> > (after this has been done I wanna integrate listings with them)
>>
>> I don't understand what is challenging about your first challenge. Do
>> you mean you don't know how to get the categories from the database
>> (Category.all) or how to format them on a page or what?
>>
>> It suggests to me that you have still not followed my and Tamara's
>> advice to work right through railstutorial.org. Until you understand
>> the basics you are just wasting your time and ours.
>>
>> Colin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com .
> To post to this group, send email to rubyonra...@googlegroups.com .
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/ .145b7f8d-6956-44b3-ac9a- 57fecd95e33e%40googlegroups. com
>
> For more options, visit https://groups.google.com/d/optout .
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/384f64e3-ae98-4ce7-a9d7-b1bda61ec04e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment