Javascript 单击修改vis.js数据集中的项目内容

Javascript 单击修改vis.js数据集中的项目内容,javascript,Javascript,我们已创建包含以下项目的数据集: var items = new vis.DataSet([ {id: 1, content: 'item 1', start: moment('8:00', "hh:mm"), end: moment('9:00', "hh:mm")}, {id: 2, content: 'item 2', start: moment('9:00', "hh:mm")}, {id: 3, content: 'item 3', start: moment('10:00', "hh

我们已创建包含以下项目的数据集:

var items = new vis.DataSet([
{id: 1, content: 'item 1', start: moment('8:00', "hh:mm"), end: moment('9:00', "hh:mm")},
{id: 2, content: 'item 2', start: moment('9:00', "hh:mm")},
{id: 3, content: 'item 3', start: moment('10:00', "hh:mm")},
{id: 4, content: 'item 4', start: moment('11:00', "hh:mm")}
现在我们想要做的是能够点击
item1
来改变它的内容;我已尝试使用函数
dataset.update

items.on('*', function (event, properties, senderId) {
   var itemId = timeline.getSelection(); 
 items.update({id: timeline.getSelection(), content: 'updated', start: moment('9:00', "hh:mm")}); 
});
但是我们得到了这个错误:

项目2中缺少属性“内容”


我没有测试您的代码片段,但问题可能是“timeline.getSelection”返回一个ID数组,而“items.update”只接受单个对象或对象数组作为“data”参数

解决方案1: 您需要循环选择数组,为每个条目创建一个数据对象,并将其传递给“items.update”

解决方案2: 准备一个对象数组,并将该数组传递给“items.update”

参考: -visjs.org/docs/timeline/index.html#Methods->getSelection() -->更新(数据[,发送者ID])