bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 116 forks source link

How to disable order per instance #233

Closed rusikf closed 7 years ago

rusikf commented 7 years ago

Hello, how to disable order per instance ? For example, for the same grid at one action I want to disable order, for another - to enable. It looks that in wiki there is only way to disable it per class, not per instance.( If we set order: false for column - false is evaluated per class, if use proc, it is used for scope, not for disable/enable )

bogdan commented 7 years ago

As I understand you want to disable order for entire grid not for particular columns. Both things are possible.

Disable for entire grid pass order: false into the datagrid_table. Disable particular columns on particular action:


class Grid

attr_accessor :disable_order

def self.controlled_order
  proc { |grid|
    !grid.disable_order && yield
  }
end

column(:a, order: "column1") # column where order is always enabled.
column(:b, order: controlled_order { "column2" })
column(:c, order: controlled_order { "column3" })

end

# In controller

@grid = Grid.new(params[:grid].merge(disable_order: true)
rusikf commented 7 years ago

@bogdan thank's for answer, but no, in your example !disable_order is executed in class context, I checked it

rusikf commented 7 years ago

If I try proc {|grid| !disabled }, so in this case disable_order is nill because this all block evaluated in class context before instance has been initialized

bogdan commented 7 years ago

What about proc {|grid| !grid.disabled } ?

rusikf commented 7 years ago

I tried it , but doesn't works. |grid| in this context has not assigned disabled property yet

bogdan commented 7 years ago

True, lets try a different plan:

attr_accessor :restrict_order
column(:b, order: "whatever", restricted_order: true)

Run rake datagrid:copy_partials and modify app/views/datagird/_order_for.html.erb like this:

+ <% if !column.options[:restricted_order] || !grid.restrict_order %>
  <div class="order">
    <%= link_to(
        I18n.t("datagrid.table.order.asc").html_safe,
        url_for(grid.param_name => grid.as_query.merge(:order => column.name, :descending => false)),
        :class => "asc") %>
    <%= link_to(
        I18n.t("datagrid.table.order.desc").html_safe,
        url_for(grid.param_name => grid.as_query.merge(:order => column.name, :descending => true )),
        :class => "desc") %>
  </div>
+ <% end %>

Same recommendations to control order from controller.

rusikf commented 7 years ago

no, this hardcoding is not a best idea, I don't want to change views in this case :smile_cat:

bogdan commented 7 years ago

I don't envision this as a hardcode: you add your own option to columns. Why would it be hardcode? What are other reasoning behind that?