Akhil Sharma
Akhil Sharma

Reputation: 355

Ruby on rails display objects in a box table with columns and rows populated dynamically

I have a model class Student which belongs_to two other models (standard and house)

Now the Student object can be uniquely identified by the category_id from the the other models it belongs to.

I wanted to show students in the view.With categories from Standard model to be the x axis(rows) and the categories from the house model to be the Y axis.(columns). Kind of a matrix representation.

Also the standards and houses changes from school to school. So they need to be populated from the db.

Is there some gem I can use to simplify the process.

I am a backend developer and have limited knowledge of Jquery and JS.

Edit: I just want a kind of a data grid with defined columns and rows. And the link to the object of a class is defined in the particular box. For ex: In the box 1 defined by row 1 and column 1 there are three students. Box 2 defined by row 2 and column 1 has 2 students etc.(Kind of a matrix representation)

Upvotes: 3

Views: 458

Answers (1)

songyy
songyy

Reputation: 4573

I was facing this matrix kind of thing previously, and I asked this question. It's about active_admin, but the idea is the same.

I think for the problem you're facing, the easiest way is to build this matrix table yourself. Here's an example, and I use Slim syntax

  • the row header be the category
  • the column header be the level

and then do something like below:

table
  thead
    tr
      - @categories.each do |c|
        td = c.name
  tbody
    - @levels.each do |l|
      tr
        td.column-header l.name
        - @categories.each do |c|
          - intersect_students = l.students.where(category: c)
          - if intersect_students.empty?
            td
          - else
            td = intersect_students.plunk(:name).join(', ')

NOTE: the finding of intersect is a bit slow.. needs some pre-processing logic to improve it.

Upvotes: 1

Related Questions