-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default order/sort of lists #442
Comments
@gylaz The user would choose the attribute used in default_order method ? |
Is there a way to change the default order now, for all dashboard/models by default? |
@avk As long as you're ok with overwriting a private method, you can definitely set a default order. You'll just have to check the source every update to make sure the private API you're using doesn't change. For example, say you have a User model with email attribute. If you want to sort by email ascending by default, you could add the following to module Admin
class UsersController < Admin::ApplicationController
private
def order
if params[:order] && params[:direction]
@_order ||= Administrate::Order.new(params[:order], params[:direction)
else
@_order ||= Administrate::Order.new(email: :asc)
end
end
end
end |
I found another way that worked for me, across all dashboards: module Admin
class ApplicationController < Administrate::ApplicationController
before_filter :default_params
def default_params
params[:order] ||= "created_at"
params[:direction] ||= "desc"
end
end
end |
👍 That is a way better way to set a default order. |
I'm using a gem that handles ordering through a method #roots_and_descendants_preordered (specifically, the closure_tree gem, which has preordering ClosureTree/closure_tree#38). Setting this as the default scope seemed to throw a SQL error from
I wondered if there were a way to set a default order in administrate by calling a class method. Any thoughts? Thank you! |
very nice @avk. It would be nice to be able to sort nulls also. I tried "DESC NULLS LAST" which is fine in Postgres but I got an error from Administrate.
Fair enough Administrate. |
In rails 5.1 I've had to change module Admin
class ApplicationController < Administrate::ApplicationController
before_action :default_params
def default_params
params[:order] ||= 'id'
params[:direction] ||= 'desc'
end
end
end |
I’m going to close this as that seems like a good solution. If you’re not on Rails 5.1 yet, I’d recommend standardising on |
* Default sort order to show most recently created first * Show office location on the dashboard * Ref for order code: thoughtbot/administrate#442 (comment)
* Default sort order to show most recently created first * Show office location on the dashboard * Ref for order code: thoughtbot/administrate#442 (comment)
+ Use upvoted code sample from this thread: thoughtbot/administrate#442
+ Use upvoted code sample from this thread: thoughtbot/administrate#442
+ Use upvoted code sample from this thread: thoughtbot/administrate#442
I am building a Rails 5.2.1 app with Administrate 0.11.0.
but the server log says:
...and the desired sorting on the
I am afraid I am missing something obvious, but I am thoroughly stumped... |
@giuseb I also just ran into this, and can confirm the behavior changed between 0.10.0 and 0.11.0. This piece in code expects a different nesting of params than before:
(expects order & direction to be under key vs.
(expects order & direction to be at root of params) Here's what I did for now... mostly a workaround for not being able to do a deep_merge on params:
|
- Cannot update events when we set sort ordering on start date when we pass the :order and :direction parameters. - Reference [thoughtbot/administrate#442 (comment)]
- Cannot update events when we set sort ordering on start date when we pass the :order and :direction parameters. - Reference [thoughtbot/administrate#442 (comment)]
this is a much simpler workaround (copy past from the applicationcontroller). wouldnt it be a nobrainer to add configuration-options to set those defaults?
|
+1 for adding a default of |
If this is the solution you're going with, it should be added to the comments in the controllers. That's the second place I looked for a method I needed to overwrite (after the dashboard). |
Hello all. Thank you for the discussion and the alternatives proposed. I'd be happy to accept a PR that introduced methods |
Tests and documentation are very important. I can't figure out how to use this. AT ALL. |
Not working for Rails 6 :\ |
@atinder I use the code below in my Rails 6.0.2.2 app, no problem with Rails 6. # app/controllers/admin/application_controller.rb
module Admin
class ApplicationController < Administrate::ApplicationController
....
def order
@order ||= Administrate::Order.new(
params.fetch(resource_name, {}).fetch(:order, default_sort[:order]),
params.fetch(resource_name, {}).fetch(:direction, default_sort[:direction]),
)
end
# override this in specific controllers as needed
def default_sort
{ order: :updated_at, direction: :desc }
end
...
end
end |
thanks a lot @sedubois |
@sedubois, can we improve our docs around this? Seems like it's still a bit confusing |
Yes @nickcharlton that would be helpful. I can just paste that snippet somewhere why not. Where should it fit in the docs? |
"Customising Dashboards", perhaps? It seems like the closest place with the current arrangement. |
is there a way to order two data by 'last_name' and 'id' at the same time? def order |
found a better way in documentation
|
Thanks @atinder. Works for me on Rails 6 |
is there a way we can sort by two columns? example i have 2 columns, by "last_name" and "amount"? |
@valcaro87 the sorting example shared by @atinder above is for all tables, where it makes sense to sort by universal items such as Did you want to sort a single table or all your tables have the |
@kaka-ruto single table.. i have "sales" table. i want to sort by "last_name" and their "amount" in desc order..
( we can achieved this in sql by: SELECT * FROM sales ORDER BY last_name DESC, amount DESC; ) |
Done a little digging and it seems at the moment you can only sort by a single column at a time @valcaro87 -> codebase |
Something close that could help is to use collection filters |
If you are not looking for much flexibility, it might be possible to sort by multiple fields. Let me explain. It's possible to define a default sorting to be as complex as you want. This is an example based on Administrate's own example app. It overrides
If default ordering is all you need, that's your solution. If instead you are looking for a lot of flexibility, that's probably not supported at the moment. For example, say that you are in the same customers index and you want the users to be able to click on "Email", then "Territory", and have all the "Australia" records coming first, ordered by email. Not only this is not possible at the moment, but also it would be complex to implement. We would need to make the links in the table headers "remember" what the previous clicked columns were. So for example, if you click "Email" and then "Territory", the URI would look something like There's an intermediate way though... You could override the index template to provide your own links to "blessed" orders, or in general scopes of any type that you need. For example, I tried by adding this link to the index template of the example app: </header>
<section class="main-content__body main-content__body--flush">
+ <p><%= link_to "Special order", request.path + "?special-order" %></p>
+
<%= render(
"collection",
collection_presenter: page, Then I can read this class Admin::CustomersController < Admin::ApplicationController
private
def scoped_resource
if @special_order
Customer.where(hidden: false).order(:country_code, :name)
else
Customer.where(hidden: false)
end
end
end I call it before_action :read_special_order
def read_special_order
@special_order = params.key?("special-order")
params.delete("special-order")
end This registers the presence of the query param in the instance variable, then deletes the param so that it doesn't cause an error later on. |
The solution by atinder works. Overwrite default_sorting_attribute or default_sorting_direction in your dashboard controller like this:
The documentation for this solution can be found here: https://administrate-demo.herokuapp.com/customizing_controller_actions |
It would be nice if there was an option to configure the default order that record collection uses when displaying a list on the dashboard.
Something like:
The text was updated successfully, but these errors were encountered: