Data Grid Sorting

Has anyone figured out a way to add sort buttons by chance to the grid layout? I wasn’t sure if I could just set this up with CSS for now and have it do a function call to sort the data grid or if there was a native way in grid to do this, I don’t see it if there is one.

Thanks

If by grid layout, you mean a repeating panel with some column names? If so, yes, it’s easy. Here’s my column headers (Label component) for a repeating panel:

image

When the user clicks on a column name, it sets the sort icon accordingly and then I simply sort the items in the panel - no need to re-query the database. Here’s the sort operation:

self.repeating_panel.items = sorted([r for r in self.repeating_panel.items], key = lambda x: x[sort_field], reverse = (sort_order == ‘desc’))

sort_field and sort_order are variables that I set before this line of code.

Does that help?

2 Likes

Exactly what I was looking for saved me a lot of time having to do it.

Thanks

For the benefit of anyone finding this later, I assume you are either using links instead of lablels, or labels wrapped in a link? The label component has no click event in Anvil.

David and all, my apologies. Yes, I’m using Links, not Labels.

So stupid question how are you setting up the links then cause when using a Datagrid I don’t see a way to change the headers. I guess my only option would be to have to create these using repeating panels with a flow panel on top with the column links I guess?

A datagrid from what I can see just creates the repeating panel for you with the columns on top using the grid.

I guess another option would be giving a dropdown for them to sort by and when they click that it would update the repeating panel data.

Reminder: You are using a beta feature that is not yet generally available :slight_smile:

We’re working on it…

No problem at all thats why I asked before I did anything I’m fine waiting :). I love it btw saves me so much time instead of having to do the panels and columns.

I also finally am getting better with components and building these as components and dropping them in on multiple pages is a lifesaver. :slight_smile:

thenerdypython,

In my first response, I asked if you were using a repeating panel but calling it a “data grid”. I didn’t know you were actually using the beta Data Grid :slight_smile: I’m looking forward to seeing the new Data Grid component. I have multiple forms with lists/“grids” and I’ve been able to accomplish all I need with the repeating panels, so I’m curious to see how the new Data Grid component is going to make it even easier.

How is the situation right now? Is it possible to make sorting of columns in a Data grid?