I want to update SpaceBars @index
native helper when I sort the items displayed within an #each
iteration. I guess that the correct way to do it is to sort the cursor I use in the #each
.
我想在#each迭代中显示的项目排序时更新SpaceBars @index本机助手。我想这样做的正确方法是对我在#each中使用的光标进行排序。
The cursor comes from an Items
collection. The array that I use to sort them belongs to another collection document (Projects
), and it stores an ordered list of each published item id.
光标来自Items集合。我用来对它们进行排序的数组属于另一个集合文档(Projects),它存储每个已发布项目id的有序列表。
After I drop an item at its new position, I update each item index in the project document array using the sortable stop
function. To do so, I $pull
it out first, and then $push
it at a specific position.
将项目放在新位置后,我使用可排序停止功能更新项目文档数组中的每个项目索引。为此,我首先将其拉出,然后将其推到特定位置。
stop: function(e, ui) {
var project = Projects.findOne();
// get the dragged html element and the one before and after it
el = ui.item.get(0)
before = ui.item.prev().get(0)
after = ui.item.next().get(0)
console.log(Blaze.getData(el)._id); //gets the id of the item dropped
if(!before) {
//if it was dragged into the first position
console.log(after.dataset.index); //gets the index of the next element
Projects.update({_id: project._id},
{$pull:
{
items:Blaze.getData(el)._id
}
})
Projects.update({_id: project._id},
{$push:
{
items:
{
$each:[Blaze.getData(el)._id],
$position:0
}
}
})
} else if(!after) {
//if it was dragged into the last position
Projects.update({_id: project._id},
{$pull:
{
items:Blaze.getData(el)._id
}
})
Projects.update({_id: project._id},
{$push:
{
items: Blaze.getData(el)._id
}
})
} else
Projects.update({_id: project._id},
{$pull:
{
items:Blaze.getData(el)._id
}
})
Projects.update({_id: project._id},
{$push:
{
items:
{
$each:[Blaze.getData(el)._id],
$position:after.dataset.index -1
}
}
})
}
As you might see, I use the index property of my item in the above function (e.g. $position:after.dataset.index -1
). This is what I want to update by sorting my cursor.
正如您可能看到的,我在上面的函数中使用了item的index属性(例如$ position:after.dataset.index -1)。这是我想通过排序我的光标来更新。
To summarize it, on one hand, I have an ordered (using index) array of ids; Example:
总而言之,一方面,我有一个有序的(使用索引)数组的id;例:
items : {
Id2131,
Id4233,
Id2243,
Id2331,
Id2455,
Id2123
}
and on the other hand, I have a publication with all the ids matching the array, but not in the right order.
另一方面,我有一个出版物,其中所有ID都与数组匹配,但顺序不正确。
How can I sort my cursor Items.find()
following the order of my other document array?
如何按照我的其他文档数组的顺序对我的光标Items.find()进行排序?
1 个解决方案
#1
0
I just realized how to do it. Here is how I proceeded.
我刚刚意识到该怎么做。这是我的进展方式。
The helper feeding my #each
iteration does not return simple Items.find()
anymore but a sorted array version of it made with an dedicated function. It now returns sortItems(Items.find())
提供我的#each迭代的帮助器不再返回简单的Items.find(),而是使用专用函数生成的排序数组版本。它现在返回sortItems(Items.find())
And the sortItems
function looks like this:
sortItems函数如下所示:
var sortItems= function(cursor) {
if(!cursor) {
return [];
}
var raw = cursor.fetch();
var project = Projects.findOne()
var sorted = [];
_.each( project.items, function(id){
_.each( raw, function(item){
if (item._id === id) {
sorted.push(item);
}
})
})
return sorted;
};
#1
0
I just realized how to do it. Here is how I proceeded.
我刚刚意识到该怎么做。这是我的进展方式。
The helper feeding my #each
iteration does not return simple Items.find()
anymore but a sorted array version of it made with an dedicated function. It now returns sortItems(Items.find())
提供我的#each迭代的帮助器不再返回简单的Items.find(),而是使用专用函数生成的排序数组版本。它现在返回sortItems(Items.find())
And the sortItems
function looks like this:
sortItems函数如下所示:
var sortItems= function(cursor) {
if(!cursor) {
return [];
}
var raw = cursor.fetch();
var project = Projects.findOne()
var sorted = [];
_.each( project.items, function(id){
_.each( raw, function(item){
if (item._id === id) {
sorted.push(item);
}
})
})
return sorted;
};