sortContext controls the global sort, and isn't per relationship
See original GitHub issueWhen defining a sortable list of items, and a sortContext, it doesn’t work as expected. When you sort the items, it changes across all relationships, not just the one your viewing. For example:
/**
* Work Item Model
* ===============
*/
var WorkItem = new keystone.List('WorkItem', {
sortable: true,
sortContext: 'WorkCategory:items',
map: { name: 'title' },
autokey: { path: 'slug', from: 'title', unique: true }
});
WorkItem.add({
title: { type: Types.Text, required: true, index: true, initial: true },
description: { type: Types.Markdown, initial: true },
slug: { type: Types.Text }
}, 'Assets', {
image: { type: Types.CloudinaryImage, note: 'Must be > 2048px on the long edge'},
fullImage: { type: Types.CloudinaryImage },
thumbnail: { type: Types.CloudinaryImage, note: 'Must be > 800px square'},
mediaUrl: { type: Types.Url, label: 'Media URL' },
mediaInfo: { type: Types.Embedly, from: 'mediaUrl' }
}, 'Categorization', {
category: { type: Types.Relationship, ref: 'WorkCategory', many: true },
client: { type: Types.Relationship, ref: 'Client' }
});
/**
* Work Category Model
* ===================
*/
var WorkCategory = new keystone.List('WorkCategory', {
sortable: true,
autokey: { path: 'slug', from: 'name', unique: true }
});
WorkCategory.add({
name: { type: Types.Text, required: true, index: true, initial: true },
description: { type: Types.Markdown, initial: true },
slug: { type: Types.Text },
isVisible: { type: Boolean, initial: true, index: true }
});
WorkCategory.relationship({ path: 'items', ref: 'WorkItem', refPath: 'category'});
Now when you add an item to the WorkItem
list, and associate it with two WorkCategories—say one called “Digital” and one called “Print”—and then visit those categories in the admin, you see the items that are related, and they are orderable. However, if I change the order on one (“Digital”) it changes the order on the other (“Print”)! The UI implies that you’re changing the ordering for that category, but in fact you’re changing it globally.
Is this working as intended? The ability to order per-relationship is really what I’m looking for, but it seems that there’s one global ordering and that’s it.
Issue Analytics
- State:
- Created 9 years ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
This isn’t going to change in v4, will be different in v5 - see keystonejs/keystone-5#1002
This has been perplexing to me. AFAIK The sorting of relationships systemwide has been broken since at least I have used keystone (end of 2014).
I think this call can be greatly simplified. If you want a list that is globally sorted then thats what the sortContext is for, the main list view of that Model then becomes sortable which I think is the current behavior. However this should not be confused with a relationship.
The biggest issue is that the
react-select
component that visualizes the many-to-many relationships. It allows no way to sort anything which borders on the insane at this point.All we need is just to make some kind of sortable list in place of that or at the very least add Drag & Drop to that component.
Mongo has absolutely no concept of a relationship. Mongoose ODM just sticks an array of ObjectId()s where there is a relationship. The only thing that sorts these is insertion order (really). unless your front end api is performing some kind of sort the default will be the insertion order.
@wmertens, so the virtual populate api is a great feature of mongoose because it just goes in, reads the object id and inserts the matching record in its place; perfect.
The main issue here, still, is there is no working UI to do any sorting for the admin user.