leikind / wice_grid

A Rails grid plugin to create grids with sorting, pagination, and (automatically generated) filters
MIT License
537 stars 215 forks source link

How to mark action_column as selected? #261

Closed kai-breitbarth-zz closed 8 years ago

kai-breitbarth-zz commented 8 years ago

How it is possible to mark an action_column as selected? Would be nice if in the controller something like @selected = ["2", "34, "78"] is possible to pre-select rows with the action_column

leikind commented 8 years ago

You can easily do this with javascript

leikind commented 8 years ago

1) Add a css class to grid rows you want to pre-select

 g.column ... do |rec|
  if ...
    [rec.foo , {class: 'your-preselect-class'}]  # will add this class to <tr>
  else
      rec.foo
  end
end

Example: http://wicegrid.herokuapp.com/styling

2) then in JS on dom:loaded:

$('.your-preselect-class .sel input').prop('checked', true)
kai-breitbarth-zz commented 8 years ago

ugly

leikind commented 8 years ago

This is called web development.

Bloated APIs are ugly.

kai-breitbarth-zz commented 8 years ago

True. But it would be nicer if auto_column would recognize something like @selected by itself. This way I wouldn't need javascript. I implemented it yesterday and it works. But you need to use row_attributes to get the class at the tr-tag:

g.row_attributes do |c|
    if @selected && @selected.include?(c.id.to_s)
      {class: 'preselected-row'}
    end
 end

And a little coffee script

$(document).ready ->
    $('.preselected-row .sel input').prop 'checked', true

Maybe it's worth to mention it in your example page. Btw. don't get me wrong, I really like this framework!

leikind commented 8 years ago

That's almost exactly how I suggested it, I think I made a mistake by not choosing row_attributes, but the idea is the same. I see no problem in this approach. But if you feel like adding such a feature to auto_column I would be willing to accept a pull request

Obi-TOB commented 8 years ago

Thanks for this. Works good. However it fails when the grid is refreshed by setting a filter. Did you find a solution for this as well?

Best