Replies: 3 comments 2 replies
-
additional context ...
returns
|
Beta Was this translation helpful? Give feedback.
-
Hey, one way to achieve your requirement is to use the Suppose you have a schema "school" do
field :name, :string
has_many :students, Student
# add the following virtual field
field :students_count, :integer, virtual: true
end you can use the @impl Backpex.LiveResource
def item_query(query, _live_action, _assigns) do
query
|> join(:left, [school], student in Student, on: student.school_id == school.id)
|> group_by([school, _student], school.id)
|> select_merge([_school, student], %{students_count: count(student.id)})
end You are now able to display the students count with a number field like this: def fields do
[
students_count: %{
module: Backpex.Fields.Number,
label: "Students Count",
readonly: true,
orderable: false
}
]
end Note that you should set If you have the requirement to make the count orderable, please let me know! We might be able to find a way to do that, too. I hope this helps @io2 🙏 |
Beta Was this translation helpful? Give feedback.
-
Wow, thanks for the quick response @Flo0807 mighty appreciated. The above works, however I am curious what if i have multiple counts to display? Sticking with the example above
Seems we'd very quickly end up with What other ways to achieve this are there? Thanks in advance |
Beta Was this translation helpful? Give feedback.
-
Discovered backpex on ElixirForum a few weeks ago, took it for a spin and very quickly became a convert. I appreciate it is still early days but my my have we needed something like this in the phoenix/elixir world for a while. 💯
Thanks for contributing this much needed resource 🥇
Right, I find myself now trying to use backpex to add an admin dashboard into a more complex application and find i need to display a column showing counts of children
has_many, embeds_many, many_to_many e.t.c
objects.Can't for the life of me figure out how to do so and haven't been to locate documentation or sample use case.
Please help?
Beta Was this translation helpful? Give feedback.
All reactions