Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

m2m list sorting uses PK instead of displaytemplate fields when no sort field is configured #9927

Closed
3 tasks done
paulboudewijn opened this issue Nov 19, 2021 · 8 comments
Closed
3 tasks done
Labels

Comments

@paulboudewijn
Copy link
Contributor

Preflight Checklist

Describe the Bug

When no sort field is configured for a relation, the PK is used as the default sort field instead of using the field(s) used in the display template. This results in a seemingly random list of items in the m2m interface.

I have a PR ready.

To Reproduce

  1. Create a 'book' collection with the 'title' and 'subtitle' fields
  2. Create an 'author' collection
  3. Create a m2m relation between 'author' and 'book', without sortfield and interface displaytemplate
  4. Note that the m2m interface list 'books' on the 'author' collection is sorted by the PK field
  5. Add displaytemplate {{book_id.title}} - {{book_id.subtitle}} to the interface
  6. Note that the m2m interface list is still sorted in the PK field, although title and subtitle alphabetically could suggest otherwise
  7. Add a sort field sort to the relation
  8. Note that the interface list is sorted on this column (as expected)

Errors Shown

No response

What version of Directus are you using?

9.0.1

What version of Node.js are you using?

v16.13.0

What database are you using?

MSSQL

What browser are you using?

Chrome

What operating system are you using?

Windows

How are you deploying Directus?

running locally

@Oreilles
Copy link
Contributor

This isn't a bug: the primary key is used so that when no sort field is defined, the items appear in creation order.
Using the template instead is an interesting idea, but the discussions are a better fit for this kind of request.
I will close this issue, but feel free to submit it again in the discussion section.

@paulboudewijn
Copy link
Contributor Author

paulboudewijn commented Nov 19, 2021

This isn't a bug: the primary key is used so that when no sort field is defined, the items appear in creation order.
Using the template instead is an interesting idea, but the discussions are a better fit for this kind of request.

Well, the following line tells me it was intended to use the (first) displaytemplate field when the sortField wasn't defined.

const sort = ref<Sort>({ by: relation.value.sortField || fields.value[0], desc: false });

But this line prevents that:

if (sField === null || sort.value.by !== sField) return items.value;

Looks like a bug to me, but it's now discussion #9929

@rijkvanzanten
Copy link
Member

@Oreilles thoughts?

@Oreilles
Copy link
Contributor

Well, the following line tells me it was intended to use the (first) displaytemplate field when the sortField wasn't defined.
But this line prevents that:

That seems right... Was using the first field template as a default sort field actually intended ? Using the primary key to use insertion order instead makes more sense to me. Also, if no manual sort is set, shouldn't the sort be done in the api ? That would explain line 22..

@paulboudewijn
Copy link
Contributor Author

Using the primary key to use insertion order instead makes more sense to me.

What benefit does a user have to see m2m items sorted on a identity value instead of alphabetically on the name of this item?

Also, if no manual sort is set, shouldn't the sort be done in the api ?

Yes absolutely, that's also how I initially thought it would work.

@rijkvanzanten
Copy link
Member

What benefit does a user have to see m2m items sorted on a identity value instead of alphabetically on the name of this item?

None at all, but it does make sense as the default for "sort field = null". That being said, I think it'd be a great optimization if we could default the sortField option to be the first field in the display template, so it's explicit, but still have the logic default to whatever the default is in the API when the sortField option is set to null

@paulboudewijn
Copy link
Contributor Author

we could default the sortField option to be the first field in the display template, so it's explicit

By this you mean if SortField is empty and a display template is set, SortField gets automatically filled with the first field used in the display template? I was under the impression SortField was used to store the sort order after manually resorting the items via drag-n-drop. Feels tricky to me to use the same field to select a sorting field in a (related) table from which you don't want the value to be overwritten after drag-n-drop.

whatever the default is in the API

This default would then be the sort field as configured in a preset of the junction table used in the m2m relation?

@rijkvanzanten
Copy link
Member

By this you mean if SortField is empty and a display template is set, SortField gets automatically filled with the first field used in the display template? I was under the impression SortField was used to store the sort order after manually resorting the items via drag-n-drop. Feels tricky to me to use the same field to select a sorting field in a (related) table from which you don't want the value to be overwritten after drag-n-drop.

Ah yeah my bad. I was confusing this with an option to set the display sort-value for when you don't have a manual drag-and-drop sort field configured under the relationship settings, which is currently not a thing yet but something I'd envision could be part of the sprint to add #9135

@rijkvanzanten rijkvanzanten reopened this Nov 22, 2021
@directus directus locked and limited conversation to collaborators Apr 14, 2022
@rijkvanzanten rijkvanzanten converted this issue into discussion #12771 Apr 14, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

3 participants