[英]nodejs: save data of sortable menu with javascript and send it to mongodb
解决了!!
我有一个弹出窗口,其中包含使用javascript制成的可排序菜单。 菜单的元素属于猫鼬模式,我想根据菜单中的新订单列表更新数据库。 让我解释得更好。 我有这个猫鼬的模式:
({
title: { type: String },
POI: [{
POIname: { type: String },
POIlocation: { type: String },
POItext: { type: String }
}]
})
我将数组POI从Express传递到客户端,
#popupbox
#riordinabox
#divhead
span#exitbox(onclick="closepopupbox('popupbox')", title="EXIT") X
p Trascina il POI per riordinare la lista
#divlistaPOI
each POI in POIs
.divpopupbox(id="#{POI.POIname}", draggable="true", ondragenter="dragenter(event)" ondragstart="dragstart(event)")
p #{POI.POIname}
#divfooter
如您所见,.POIname元素创建菜单。 允许用户更改列表的顺序并保存新的顺序。 例如,默认顺序为:
({
title: "sometitle",
POI: [{
POIname: "jump",
POIlocation: table,
POItext: "jump on the table"
}],
[{
POIname: "run",
POIlocation: street,
POItext: "run on the street"
}],
[{
POIname: "turnR",
POIlocation: everywhere,
POItext: "turn right"
}]
})
并且可排序菜单显示:
用户操作后,新订单列表为:
现在我想获取新订单并在数据库中设置相同的订单,因此我可以看到
({
title: "sometitle",
POI: [{
POIname: "run",
POIlocation: street,
POItext: "run on the street"
}],
[{
POIname: "turnR",
POIlocation: everywhere,
POItext: "turn right"
}],
[{
POIname: "jump",
POIlocation: table,
POItext: "jump on the table"
}]
})
我知道哪一步先到。
有任何想法吗?
谢谢!
@YanMayatskiy,我们快结束了。 我做了这段代码:
router.get('/:name/orderlist/:list', function(req, res, next) {
Itinerario.findOne({name: req.params.name}, function(err, itinerario){
if (err) throw err;
var temp = [];
var array = JSON.parse(req.params.list);
res.send(array);
for (i=0; i<array.length; i++){
if (itinerario.POI[i].POIname === array[i]){
temp[i] = itinerario.POI[i];
} else {
var j = 0;
while (true){
if( itinerario.POI[i].POIname === array[j] ){
break;
}
j++
}
temp[j] = itinerario.POI[i];
}
}
for (i=0; i<temp.length; i++){
itinerario.POI[i] = temp[i];
}
itinerario.save(function (err, itinerario){
if (err) throw err;
console.log(itinerario);
})
})
});
它可以正常工作,但是有问题! 最后,当我登录“ itinerario”模型时,可以在提示符下看到步骤的新顺序:
({
title: "sometitle",
POI: [{
POIname: "run",
POIlocation: street,
POItext: "run on the street"
}],
[{
POIname: "turnR",
POIlocation: everywhere,
POItext: "turn right"
}],
[{
POIname: "jump",
POIlocation: table,
POItext: "jump on the table"
}]
})
但是,如果我在mongo提示符下执行find()方法,我仍然会看到步骤的旧顺序!
({
title: "sometitle",
POI: [{
POIname: "jump",
POIlocation: table,
POItext: "jump on the table"
}],
[{
POIname: "run",
POIlocation: street,
POItext: "run on the street"
}],
[{
POIname: "turnR",
POIlocation: everywhere,
POItext: "turn right"
}]
})
实际上,刷新页面后,我又得到了旧订单。 它不会将新订单保存在数据库中!!!!
我要疯了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.