diff --git a/HISTORY.md b/HISTORY.md index 54eafdab..29866937 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -6,7 +6,8 @@ http://visjs.org ### DataSet -- Added property `length` holding the total number of items in the DataSet. +- Added property `length` holding the total number of items to the `DataSet` + and `DataView`. ## 2015-01-16, version 3.9.1 diff --git a/docs/dataview.html b/docs/dataview.html index 3046391f..159ad720 100644 --- a/docs/dataview.html +++ b/docs/dataview.html @@ -20,6 +20,8 @@
  • Overview
  • Example
  • Construction
  • +
  • Methods
  • +
  • Properties
  • Getting Data
  • Subscriptions
  • Data Policy
  • @@ -152,6 +154,106 @@ var data = new vis.DataView(dataset, options) +

    Methods

    + +

    DataView contains the following methods.

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    MethodReturn TypeDescription
    + get([options] [, data])
    + get(id [,options] [, data])
    + get(ids [, options] [, data]) +
    Object | Array | DataTable + Get a single item, multiple items, or all items from the DataView. + Usage examples can be found in section Getting Data, and the available options are described in section Data Selection. If parameter data is provided, items will be appended to this array or table, which is required in case of Google DataTable. +
    + getDataSet() + DataSet + Get the DataSet to which the DataView is connected. +
    + getIds([options]) + Number[] + Get ids of all items or of a filtered set of items. + Available options are described in section Data Selection, except that options fields and type are not applicable in case of getIds. +
    off(event, callback)none + Unsubscribe from an event, remove an event listener. See section Subscriptions. +
    on(event, callback)none + Subscribe to an event, add an event listener. See section Subscriptions. +
    + setDataSet(data) + none + Replace the DataSet of the DataView. Parameter data can be a DataSet or a DataView. +
    + + +

    Properties

    + +

    DataView contains the following properties.

    + + + + + + + + + + + + + + + + + +
    PropertyTypeDescription
    lengthNumberThe number of items in the DataView.
    +

    Getting Data

    @@ -165,8 +267,8 @@ var items = view.get();

    Data of a DataView can be filtered and formatted again, in exactly the same way as in a DataSet. See sections - Data Filtering and - Data Formatting for more + Data Manipulation and + Data Selection for more information.

    diff --git a/lib/DataView.js b/lib/DataView.js index f56e151e..c5fbdfb8 100644 --- a/lib/DataView.js +++ b/lib/DataView.js @@ -14,6 +14,7 @@ var DataSet = require('./DataSet'); function DataView (data, options) { this._data = null; this._ids = {}; // ids of the items currently in memory (just contains a boolean true) + this.length = 0; // number of items in the DataView this._options = options || {}; this._fieldId = 'id'; // name of the field containing id this._subscribers = {}; // event subscribers @@ -50,6 +51,7 @@ DataView.prototype.setData = function (data) { } } this._ids = {}; + this.length = 0; this._trigger('remove', {items: ids}); } @@ -67,6 +69,7 @@ DataView.prototype.setData = function (data) { id = ids[i]; this._ids[id] = true; } + this.length = ids.length; this._trigger('add', {items: ids}); // subscribe to new dataset @@ -277,6 +280,8 @@ DataView.prototype._onEvent = function (event, params, senderId) { break; } + this.length += added.length - removed.length; + if (added.length) { this._trigger('add', {items: added}, senderId); } diff --git a/test/DataView.test.js b/test/DataView.test.js index b0afbf62..43110ba2 100644 --- a/test/DataView.test.js +++ b/test/DataView.test.js @@ -28,6 +28,7 @@ describe('DataView', function () { {id: 2, content: 'Item 2', group: 2}, {id: 3, content: 'Item 3', group: 2} ]); + assert.equal(group2.length, 2); // test filtering the view contents assert.deepEqual(group2.get({ @@ -51,19 +52,46 @@ describe('DataView', function () { groups.update({id:2, content: 'Item 2 (changed)'}); assert.equal(groupsTriggerCount, 1); assert.equal(group2TriggerCount, 1); + assert.equal(group2.length, 2); groups.update({id:5, content: 'Item 5 (changed)'}); assert.equal(groupsTriggerCount, 2); assert.equal(group2TriggerCount, 1); + assert.equal(group2.length, 2); // detach the view from groups group2.setData(null); assert.equal(groupsTriggerCount, 2); assert.equal(group2TriggerCount, 2); + assert.equal(group2.length, 0); groups.update({id:2, content: 'Item 2 (changed again)'}); assert.equal(groupsTriggerCount, 3); assert.equal(group2TriggerCount, 2); + + // test updating of .length property + group2.setData(groups); + assert.equal(group2.length, 2); + + // add a new item + groups.add({id: 6, content: 'Item 6', group: 2}); + assert.equal(group2.length, 3); + + // change an items group to 2 + groups.update({id: 4, group: 2}); + assert.equal(group2.length, 4); + + // change an items group to 1 + groups.update({id: 4, group: 1}); + assert.equal(group2.length, 3); + + // remove an item + groups.remove(2); + assert.equal(group2.length, 2); + + // remove all items + groups.clear(); + assert.equal(group2.length, 0); }); });