-
-
Notifications
You must be signed in to change notification settings - Fork 628
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
Implement multi-column row keys for ui.table #4105
base: main
Are you sure you want to change the base?
Conversation
It is now possible to specify a list of keys as row-key property.
Because otherwise if row_key is changed later through setter, handle_selection() would still use the outdated value from __init__().
This will still fail, if concatenated cell values are the same for different rows, e.g. ('a', 'b') and ('ab', ''). But this is already an improvement, please feel free to build on this. |
Thanks for this pull request, @me21! How should
In Python |
Summary_columns and summary_rows are just my variable names copied from my project. The meaning of columns and rows parameters is unchanged. The code assumes there are columns named "date" and "name". And their values are strings.
Ok, then I guess we'll need an object attribute to store the user assigned value to the row_key property... It's already done in the PR. |
Now I think we could convert the row_key argument of ui.table to list unconditionally and avoid type checking in handle_selection... just an optimization. |
I was wondering about their content in order to understand the problem with the existing implementation. But I got it now: Some of QTable's internals require a unique row key. In the following example, a single "Alice" can't be selected because the key value of both "Alice" rows is identical. columns = [
{'name': 'name', 'label': 'Name', 'field': 'name'},
{'name': 'age', 'label': 'Age', 'field': 'age'},
]
rows = [
{'name': 'Alice', 'age': 18},
{'name': 'Alice', 'age': 19},
{'name': 'Bob', 'age': 21},
{'name': 'Carol'},
]
ui.table(columns=columns, rows=rows, row_key='name', selection='multiple') Combining multiple columns into a row key allows to keep them selectable and sortable despite the lack of a unique column. I'll have to think about the current approach of simply string-concatenating columns since it breaks in edge cases like you described. And it will definitely break for completely identical rows. In this case the user needs to add unique IDs anyway. |
Sure. String-concatenating columns was my quick and dirty fix that worked for my use case. To make it more foolproof, it's possible to string-concatenate them with a separator that is unlikely to be contained in cell values (some non-printable character? \u0000?). |
@me21 After thinking about it once again, we're still sceptical about this API change. If users are aware of the problem and would benefit from multi-column row keys, they have already two options:
We would suggest to add two new demos to the table documentation, one for each of these two solutions. We hope that this helps the users to find the right approach for their application. |
Unfortunately, the first solution has issues with row deselection, as the code that handles deselection doesn't play well with row-key as a function. So the only solution would be to add hidden unique attribute. |
It is now possible to specify a list of keys as row-key property. Example:
In the current version, it's possible to work with multiple columns as composite table key as:
but it doesn't handle deselection properly, as row_key is still equal to 'id' if not specified in the arguments list...