Skip to content Skip to sidebar Skip to footer

Visjs Timeline: Sorting Items In Timeline

I'm using vis-js timeline library for building a timeline. I need to sort the elements in each group. Groups are like in example here. I saw that I can do this using the 'order opt

Solution 1:

You can order items in the timeline by providing a function for order option in timeline configurations. You can define it when initiating the timeline

var options = {
  order: function(a,b){
    return b.id-a.id;
  },
  editable: true
};

or after initialization

timeline.setOptions({
  order: function(a,b){
    return b.id-a.id;
  },
});

order function will be called with two parameters and they are the item objects which are going to be compared. You can implement any logic here. You only need to return a integer value back. If the return value is less than 0 then the second item (item passed to b) will be ordered first and if the return value is greater than or equal to 0 then the first item (item passed to a) will be ordered first and second item will be ordered second. This is a working demo.

Post a Comment for "Visjs Timeline: Sorting Items In Timeline"