Data Manipulation methods of DataSet In Vis Js
Posted By : Manisha Kirodiwal | 30-May-2018
Vis.js contains a dataset that we can use to manipulate and hold the unstructured data and listen for the changes done on the data. DataSet is key / value-based JSON object.Using some methods we can perform some operations on data sets. Data elements can be added, updated and removed from DataSet, and we also can subscribe to changes in DataSet.
Data Manipulation
We can manipulate data in the DataSet using methods add, update and remove. The DataSet can be set to blank using the clear method.
// create a DataSet var data = new vis.DataSet(); // add items data.add([ {id: 1, text: 'item 1'}, {id: 2, text: 'item 2'}, {id: 3, text: 'item 3'} ]); // update an item data.update({id: 2, text: 'item 2 (updated)'}); // remove an item data.remove(3);
Add
Using Add method we can add a data item/an array of items.
Syntax:
var addIds = DataSet.add(data [, senderId])
The data argument can contain:
A single Object containing an item which we want to be added. The item should contain an id.
An Array which contains a list of items which we want to be added. Each item should contain an id.
When all the items are added to the DataSet after that DataSet triggers an event add. When we provide a senderId, this senderId will be sent with the triggered event to all the subscribers.
When an item with the duplicate id as any of the added items already exists then this method will throw an error.
Update
Using Update method we can update a data item/an array of items.
Syntax:
var updatedIds = DataSet.update(data [, senderId])
The data argument can contain:
A single Object containing an item which we want to be added. The item should contain an id.
An Array which contains a list of items which we want to be added. Each item should contain an id.
These provided properties will be merged into the current item. But if the targeted item does not exist in the list then new will be created.
When all the items are updated to the DataSet after that DataSet triggers an event update. When we provide a senderId, this senderId will be sent with the triggered event to all the subscribers.
Remove
Using Remove method we can delete a data item/an array of items.
Syntax:
var removedIds = DataSet.remove(id [, senderId])
The argument id can be:
A number or string containing the identifier for a single item to be removed.
A single Object containing an item which we want to be deleted. The item should contain an id.
An Array which contains a list of items which we want to be added. Each item should contain an id.
When the items are removed from the DataSet, after that DataSet triggers an event remove. When we provide a senderId, this senderId will be sent with the triggered event to all the subscribers.
Clear
Using Clear method we can delete/clear all the DataSet.
Syntax:
var removedIds = DataSet.clear([senderId])
When the items are removed from the DataSet, after that DataSet triggers an event remove for all removed items. When we provide a senderId, this senderId will be sent with the triggered event to all the subscribers.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Manisha Kirodiwal
Manisha is a Web Application developer and she is good in working with Angular, TypeScript , JavaScript and Jquery. In free time she loves to dance and cooking.