dongliao1948 2015-11-05 12:34
浏览 130
已采纳

通过AJAX结果访问DataSet

I am using visjs timeline to make a timeline and I have a button that redraws that timeline on click event. The redrawing is done by getting data from timeline.php using AJAX but the data received doesn't work in DataSet!

Here is what I've got so far:

The function that is triggered when I click on button:

function redrawTL(){
    $.ajax({
        type: "POST", url: "ajax/timeline.php", data: {},
        success: function(data){
            redraw(data);
        }
    });
}

The function that should redraw the timeline assuming I already have set the variables items and tl

function redraw(data){
    console.log(data);
    items = new vis.DataSet([ data ]); // This doesn't!!
    tl.destroy();
    tl = new vis.Timeline(container, items, options);
}

So my question is why it doesn't work?! Knowing that the data is shown in console.

Data is supposed to be filled with the following data:

{id: 0, content: '07:10:12',   start: today.clone().add(25606, 'seconds'), 'className': 'tmstyle'}, 
{id: 1, content: '00:40:06',   start: today.clone().add(56563, 'seconds'), 'className': 'arretNU'}, 
{id: 2, content: '00:01:07',   start: today.clone().add(63312, 'seconds'), 'className': 'arretNU'}, 
{id: 3, content: '00:00:16',   start: today.clone().add(63389, 'seconds'), 'className': 'arretNU'}

And if I make the redraw function static it works fine.

  • 写回答

1条回答 默认 最新

  • dongtao4787 2015-11-11 14:29
    关注

    After one day of trying i finally came up with the following solution:

                function redrawTL(){
                    $.ajax({
                        type: "POST",url: "ajax/timeline.php", data: {},
                        success: function(data){
                            var result = JSON.parse(JSON.parse(JSON.stringify(data)));
                            items.clear();
                            items.add(result);
                            timeline.fit();
                        }
                    });
                }
    

    It works only if i use JSON.parse over another one!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line