vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

936 lines
26 KiB

9 years ago
9 years ago
  1. <?js
  2. var self = this;
  3. var title = 'DataSet - vis.js - A dynamic, browser based visualization library.';
  4. ?>
  5. <?js= self.partial('tmpl/html-head.tmpl', title) ?>
  6. </head>
  7. <body onload="prettyPrint();">
  8. <?js= self.partial('tmpl/navbar.tmpl') ?>
  9. <div class="container full">
  10. <h1>DataSet</h1>
  11. <h2 id="Contents">Contents</h2>
  12. <ul>
  13. <li><a href="#Overview">Overview</a></li>
  14. <li><a href="#Example">Example</a></li>
  15. <li><a href="#Construction">Construction</a></li>
  16. <li><a href="#Methods">Methods</a></li>
  17. <li><a href="#Properties">Properties</a></li>
  18. <li><a href="#Subscriptions">Subscriptions</a></li>
  19. <li><a href="#Data_Manipulation">Data Manipulation</a></li>
  20. <li><a href="#Data_Selection">Data Selection</a></li>
  21. </ul>
  22. <h2 id="Overview">Overview</h2>
  23. <p>
  24. Vis.js comes with a flexible DataSet, which can be used to hold and
  25. manipulate unstructured data and listen for changes in the data.
  26. The DataSet is key/value based. Data items can be added, updated and
  27. removed from the DataSet, and one can subscribe to changes in the DataSet.
  28. The data in the DataSet can be filtered and ordered, and fields (like
  29. dates) can be converted to a specific type. Data can be normalized when
  30. appending it to the DataSet as well.
  31. </p>
  32. <h2 id="Example">Example</h2>
  33. <p>
  34. The following example shows how to use a DataSet.
  35. </p>
  36. <pre class="prettyprint lang-js">
  37. // create a DataSet
  38. var options = {};
  39. var data = new vis.DataSet(options);
  40. // add items
  41. // note that the data items can contain different properties and data formats
  42. data.add([
  43. {id: 1, text: 'item 1', date: new Date(2013, 6, 20), group: 1, first: true},
  44. {id: 2, text: 'item 2', date: '2013-06-23', group: 2},
  45. {id: 3, text: 'item 3', date: '2013-06-25', group: 2},
  46. {id: 4, text: 'item 4'}
  47. ]);
  48. // subscribe to any change in the DataSet
  49. data.on('*', function (event, properties, senderId) {
  50. console.log('event', event, properties);
  51. });
  52. // update an existing item
  53. data.update({id: 2, group: 1});
  54. // remove an item
  55. data.remove(4);
  56. // get all ids
  57. var ids = data.getIds();
  58. console.log('ids', ids);
  59. // get a specific item
  60. var item1 = data.get(1);
  61. console.log('item1', item1);
  62. // retrieve a filtered subset of the data
  63. var items = data.get({
  64. filter: function (item) {
  65. return item.group == 1;
  66. }
  67. });
  68. console.log('filtered items', items);
  69. // retrieve formatted items
  70. var items = data.get({
  71. fields: ['id', 'date'],
  72. type: {
  73. date: 'ISODate'
  74. }
  75. });
  76. console.log('formatted items', items);
  77. </pre>
  78. <h2 id="Construction">Construction</h2>
  79. <p>
  80. A DataSet can be constructed as:
  81. </p>
  82. <pre class="prettyprint lang-js">
  83. var data = new vis.DataSet([data] [, options])
  84. </pre>
  85. <p>
  86. After construction, data can be added to the DataSet using the methods
  87. <code>add</code> and <code>update</code>, as described in section
  88. <a href="#Data_Manipulation">Data Manipulation</a>.
  89. </p>
  90. <p>
  91. The parameter <code>data</code> is optional and is an Array with items.
  92. </p>
  93. <p>
  94. The parameter <code>options</code> is optional and is an object which can
  95. contain the following properties:
  96. </p>
  97. <table class="options">
  98. <tr>
  99. <th>Name</th>
  100. <th>Type</th>
  101. <th>Default value</th>
  102. <th>Description</th>
  103. </tr>
  104. <tr>
  105. <td>fieldId</td>
  106. <td>String</td>
  107. <td>"id"</td>
  108. <td>
  109. The name of the field containing the id of the items.
  110. When data is fetched from a server which uses some specific
  111. field to identify items, this field name can be specified
  112. in the DataSet using the option <code>fieldId</code>.
  113. For example <a href="http://couchdb.apache.org/"
  114. target="_blank">CouchDB</a> uses the field
  115. <code>"_id"</code> to identify documents.
  116. </td>
  117. </tr>
  118. <tr>
  119. <td>type</td>
  120. <td>Object.&lt;String,&nbsp;String&gt;</td>
  121. <td>none</td>
  122. <td>
  123. An object containing field names as key, and data types as
  124. value. By default, the type of the properties of items are left
  125. unchanged. Item properties can be normalized by specifying a
  126. field type. This is useful for example to automatically convert
  127. stringified dates coming from a server into JavaScript Date
  128. objects. The available data types are listed in section
  129. <a href="#Data_Types">Data Types</a>.
  130. </td>
  131. </tr>
  132. <tr>
  133. <td>queue</td>
  134. <td>Object | boolean</td>
  135. <td>none</td>
  136. <td>
  137. Queue data changes ('add', 'update', 'remove') and flush them at once.
  138. The queue can be flushed manually by calling
  139. <code>DataSet.flush()</code>, or can be flushed after a configured delay
  140. or maximum number of entries.
  141. <br>
  142. <br>
  143. When <code>queue</code> is true, a queue is created
  144. with default options. Options can be specified by providing an object:
  145. <ul>
  146. <li><code>delay: number</code><br>
  147. The queue will be flushed automatically after an inactivity of this
  148. delay in milliseconds. Default value is <code>null</code>.
  149. <li><code>max: number</code><br>
  150. When the queue exceeds the given maximum number
  151. of entries, the queue is flushed automatically.
  152. Default value is <code>Infinity</code>.
  153. </li>
  154. </ul>
  155. </td>
  156. </tr>
  157. </table>
  158. <h2 id="Methods">Methods</h2>
  159. <p>DataSet contains the following methods.</p>
  160. <table class="methods">
  161. <tr>
  162. <th>Method</th>
  163. <th>Return Type</th>
  164. <th>Description</th>
  165. </tr>
  166. <tr>
  167. <td>add(data [, senderId])</td>
  168. <td>Number[]</td>
  169. <td>Add one or multiple items to the DataSet. <code>data</code> can be a single item or an array with items. Adding an item will fail when there already is an item with the same id. The function returns an array with the ids of the added items. See section <a href="#Data_Manipulation">Data Manipulation</a>.</td>
  170. </tr>
  171. <tr>
  172. <td>clear([senderId])</td>
  173. <td>Number[]</td>
  174. <td>Clear all data from the DataSet. The function returns an array with the ids of the removed items.</td>
  175. </tr>
  176. <tr>
  177. <td>distinct(field)</td>
  178. <td>Array</td>
  179. <td>Find all distinct values of a specified field. Returns an unordered array containing all distinct values. If data items do not contain the specified field are ignored.</td>
  180. </tr>
  181. <tr>
  182. <td>flush()</td>
  183. <td>none</td>
  184. <td>Flush queued changes. Only available when the DataSet is configured with the option <code>queue</code>, see section <a href="#Construction">Construction</a>.</td>
  185. </tr>
  186. <tr>
  187. <td>forEach(callback&nbsp;[,&nbsp;options])</td>
  188. <td>none</td>
  189. <td>
  190. Execute a callback function for every item in the dataset.
  191. The available options are described in section <a href="#Data_Selection">Data Selection</a>.
  192. </td>
  193. </tr>
  194. <tr>
  195. <td>
  196. get([options] [, data])<br>
  197. get(id [,options] [, data])<br>
  198. get(ids [, options] [, data])
  199. </td>
  200. <td>Object | Array</td>
  201. <td>
  202. Get a single item, multiple items, or all items from the DataSet.
  203. Usage examples can be found in section <a href="#Getting_Data">Getting Data</a>. The available <code>options</code> are described in section <a href="#Data_Selection">Data Selection</a>. When a single id is requested and not found <code>null</code> is returned. When multiple ids are requested and not found an Array containing <code>null</code>s is returned.
  204. </td>
  205. </tr>
  206. <tr>
  207. <td>
  208. getDataSet()
  209. </td>
  210. <td>DataSet</td>
  211. <td>
  212. Get the DataSet itself. In case of a DataView, this function does not
  213. return the DataSet to which the DataView is connected.
  214. </td>
  215. </tr>
  216. <tr>
  217. <td>
  218. getIds([options])
  219. </td>
  220. <td>Number[]</td>
  221. <td>
  222. Get ids of all items or of a filtered set of items.
  223. Available <code>options</code> are described in section <a href="#Data_Selection">Data Selection</a>, except that options <code>fields</code> and <code>type</code> are not applicable in case of <code>getIds</code>.
  224. </td>
  225. </tr>
  226. <tr>
  227. <td>map(callback [, options])</td>
  228. <td>Array</td>
  229. <td>
  230. Map every item in the DataSet.
  231. The available options are described in section <a href="#Data_Selection">Data Selection</a>.
  232. </td>
  233. </tr>
  234. <tr>
  235. <td>max(field)</td>
  236. <td>Object | null</td>
  237. <td>
  238. Find the item with maximum value of specified field. Returns <code>null</code> if no item is found.
  239. </td>
  240. </tr>
  241. <tr>
  242. <td>min(field)</td>
  243. <td>Object | null</td>
  244. <td>
  245. Find the item with minimum value of specified field. Returns <code>null</code> if no item is found.
  246. </td>
  247. </tr>
  248. <tr>
  249. <td>off(event, callback)</td>
  250. <td>none</td>
  251. <td>
  252. Unsubscribe from an event, remove an event listener. See section <a href="#Subscriptions">Subscriptions</a>.
  253. </td>
  254. </tr>
  255. <tr>
  256. <td>on(event, callback)</td>
  257. <td>none</td>
  258. <td>
  259. Subscribe to an event, add an event listener. See section <a href="#Subscriptions">Subscriptions</a>.
  260. </td>
  261. </tr>
  262. <tr>
  263. <td>
  264. remove(id [, senderId])<br>
  265. remove(ids [, senderId])
  266. </td>
  267. <td>Number[]</td>
  268. <td>
  269. Remove one or multiple items by id or by the items themselves. Returns an array with the ids of the removed items. See section <a href="#Data_Manipulation">Data Manipulation</a>.
  270. </td>
  271. </tr>
  272. <tr>
  273. <td>
  274. setOptions(options)
  275. </td>
  276. <td>none</td>
  277. <td>
  278. Set options for the DataSet. Available options:
  279. <ul>
  280. <li>
  281. <code>queue</code><br>
  282. Queue data changes ('add', 'update', 'remove') and flush them at once.
  283. The queue can be flushed manually by calling
  284. <code>DataSet.flush()</code>, or can be flushed after a configured delay
  285. or maximum number of entries.
  286. <br>
  287. <br>
  288. When <code>queue</code> is true, a queue is created with default options.
  289. When <code>queue</code> is false, an existing queue will be flushed and removed.
  290. Options can be specified by providing an object:
  291. <ul>
  292. <li><code>delay: number</code><br>
  293. The queue will be flushed automatically after an inactivity of this
  294. delay in milliseconds. Default value is <code>null</code>.
  295. <li><code>max: number</code><br>
  296. When the queue exceeds the given maximum number
  297. of entries, the queue is flushed automatically.
  298. Default value is <code>Infinity</code>.
  299. </li>
  300. </ul>
  301. </li>
  302. </ul>
  303. </td>
  304. </tr>
  305. <tr>
  306. <td>
  307. update(data [, senderId])
  308. </td>
  309. <td>Number[]</td>
  310. <td>
  311. Update one or multiple existing items. <code>data</code> can be a single item or an array with items. When an item doesn't exist, it will be created. Returns an array with the ids of the removed items. See section <a href="#Data_Manipulation">Data Manipulation</a>.
  312. </td>
  313. </tr>
  314. </table>
  315. <h2 id="Properties">Properties</h2>
  316. <p>DataSet contains the following properties.</p>
  317. <table>
  318. <tr>
  319. <th>Property</th>
  320. <th>Type</th>
  321. <th>Description</th>
  322. </tr>
  323. <tr>
  324. <td>length</td>
  325. <td>Number</td>
  326. <td>The number of items in the DataSet.</td>
  327. </tr>
  328. </table>
  329. <h2 id="Subscriptions">Subscriptions</h2>
  330. <p>
  331. One can subscribe on changes in a DataSet.
  332. A subscription can be created using the method <code>on</code>,
  333. and removed with <code>off</code>.
  334. </p>
  335. <pre class="prettyprint lang-js">
  336. // create a DataSet
  337. var data = new vis.DataSet();
  338. // subscribe to any change in the DataSet
  339. data.on('*', function (event, properties, senderId) {
  340. console.log('event:', event, 'properties:', properties, 'senderId:', senderId);
  341. });
  342. // add an item
  343. data.add({id: 1, text: 'item 1'}); // triggers an 'add' event
  344. data.update({id: 1, text: 'item 1 (updated)'}); // triggers an 'update' event
  345. data.remove(1); // triggers an 'remove' event
  346. </pre>
  347. <h3 id="On">On</h3>
  348. <p>
  349. Subscribe to an event.
  350. </p>
  351. Syntax:
  352. <pre class="prettyprint lang-js">DataSet.on(event, callback)</pre>
  353. Where:
  354. <ul>
  355. <li>
  356. <code>event</code> is a String containing any of the events listed
  357. in section <a href="#Events">Events</a>.
  358. </li>
  359. <li>
  360. <code>callback</code> is a callback function which will be called
  361. each time the event occurs. The callback function is described in
  362. section <a href="#Callback">Callback</a>.
  363. </li>
  364. </ul>
  365. <h3 id="Off">Off</h3>
  366. <p>
  367. Unsubscribe from an event.
  368. </p>
  369. Syntax:
  370. <pre class="prettyprint lang-js">DataSet.off(event, callback)</pre>
  371. Where <code>event</code> and <code>callback</code> correspond with the
  372. parameters used to <a href="#On">subscribe</a> to the event.
  373. <h3 id="Events">Events</h3>
  374. <p>
  375. The following events are available for subscription:
  376. </p>
  377. <table>
  378. <tr>
  379. <th>Event</th>
  380. <th>Description</th>
  381. </tr>
  382. <tr>
  383. <td>add</td>
  384. <td>
  385. The <code>add</code> event is triggered when an item
  386. or a set of items is added, or when an item is updated while
  387. not yet existing.
  388. </td>
  389. </tr>
  390. <tr>
  391. <td>update</td>
  392. <td>
  393. The <code>update</code> event is triggered when an existing item
  394. or a set of existing items is updated.
  395. </td>
  396. </tr>
  397. <tr>
  398. <td>remove</td>
  399. <td>
  400. The <code>remove</code> event is triggered when an item
  401. or a set of items is removed.
  402. </td>
  403. </tr>
  404. <tr>
  405. <td>*</td>
  406. <td>
  407. The <code>*</code> event is triggered when any of the events
  408. <code>add</code>, <code>update</code>, and <code>remove</code>
  409. occurs.
  410. </td>
  411. </tr>
  412. </table>
  413. <h3 id="Callback">Callback</h3>
  414. <p>
  415. The callback functions of subscribers are called with the following
  416. parameters:
  417. </p>
  418. <pre class="prettyprint lang-js">
  419. function (event, properties, senderId) {
  420. // handle the event
  421. });
  422. </pre>
  423. <p>
  424. where the parameters are defined as
  425. </p>
  426. <table>
  427. <tr>
  428. <th>Parameter</th>
  429. <th>Type</th>
  430. <th>Description</th>
  431. </tr>
  432. <tr>
  433. <td>event</td>
  434. <td>String</td>
  435. <td>
  436. Any of the available events: <code>add</code>,
  437. <code>update</code>, or <code>remove</code>.
  438. </td>
  439. </tr>
  440. <tr>
  441. <td>properties</td>
  442. <td>Object&nbsp;|&nbsp;null</td>
  443. <td>
  444. Optional properties providing more information on the event.
  445. In case of the events <code>add</code>,
  446. <code>update</code>, and <code>remove</code>,
  447. <code>properties</code> is always an object containing a property
  448. <code>items</code>, which contains an array with the ids of the affected
  449. items. The <code>update</code> and <code>remove</code> events have an extra
  450. field <code>oldData</code> containing the original data of the items in the
  451. dataset before the items were updated or removed. The <code>update</code>
  452. event also contains a field <code>data</code> containing the changes:
  453. the properties of the items that are being updated.
  454. </td>
  455. </tr>
  456. <tr>
  457. <td>senderId</td>
  458. <td>String&nbsp;|&nbsp;Number</td>
  459. <td>
  460. An senderId, optionally provided by the application code
  461. which triggered the event. If senderId is not provided, the
  462. argument will be <code>null</code>.
  463. </td>
  464. </tr>
  465. </table>
  466. <h2 id="Data_Manipulation">Data Manipulation</h2>
  467. <p>
  468. The data in a DataSet can be manipulated using the methods
  469. <a href="#Add"><code>add</code></a>,
  470. <a href="#Update"><code>update</code></a>,
  471. and <a href="#Remove"><code>remove</code></a>.
  472. The DataSet can be emptied using the method
  473. <a href="#Clear"><code>clear</code></a>.
  474. </p>
  475. <pre class="prettyprint lang-js">
  476. // create a DataSet
  477. var data = new vis.DataSet();
  478. // add items
  479. data.add([
  480. {id: 1, text: 'item 1'},
  481. {id: 2, text: 'item 2'},
  482. {id: 3, text: 'item 3'}
  483. ]);
  484. // update an item
  485. data.update({id: 2, text: 'item 2 (updated)'});
  486. // remove an item
  487. data.remove(3);
  488. </pre>
  489. <h3 id="Add">Add</h3>
  490. <p>
  491. Add a data item or an array with items.
  492. </p>
  493. Syntax:
  494. <pre class="prettyprint lang-js">var addedIds = DataSet.add(data [, senderId])</pre>
  495. The argument <code>data</code> can contain:
  496. <ul>
  497. <li>
  498. An <code>Object</code> containing a single item to be
  499. added. The item must contain an id.
  500. </li>
  501. <li>
  502. An <code>Array</code> containing a list with items to be added. Each item must contain an id.
  503. </li>
  504. </ul>
  505. <p>
  506. After the items are added to the DataSet, the DataSet will
  507. trigger an event <code>add</code>. When a <code>senderId</code>
  508. is provided, this id will be passed with the triggered
  509. event to all subscribers.
  510. </p>
  511. <p>
  512. The method will throw an Error when an item with the same id
  513. as any of the added items already exists.
  514. </p>
  515. <h3 id="Update">Update</h3>
  516. <p>
  517. Update a data item or an array with items.
  518. </p>
  519. Syntax:
  520. <pre class="prettyprint lang-js">var updatedIds = DataSet.update(data [, senderId])</pre>
  521. The argument <code>data</code> can contain:
  522. <ul>
  523. <li>
  524. An <code>Object</code> containing a single item to be
  525. updated. The item must contain an id.
  526. </li>
  527. <li>
  528. An <code>Array</code> containing a list with items to be updated. Each item must contain an id.
  529. </li>
  530. </ul>
  531. <p>
  532. The provided properties will be merged in the existing item.
  533. When an item does not exist, it will be created.
  534. </p>
  535. <p>
  536. After the items are updated, the DataSet will
  537. trigger an event <code>add</code> for the added items, and
  538. an event <code>update</code>. When a <code>senderId</code>
  539. is provided, this id will be passed with the triggered
  540. event to all subscribers.
  541. </p>
  542. <h3 id="Remove">Remove</h3>
  543. <p>
  544. Remove a data item or an array with items.
  545. </p>
  546. Syntax:
  547. <pre class="prettyprint lang-js">var removedIds = DataSet.remove(id [, senderId])</pre>
  548. <p>
  549. The argument <code>id</code> can be:
  550. </p>
  551. <ul>
  552. <li>
  553. A <code>Number</code> or <code>String</code> containing the id
  554. of a single item to be removed.
  555. </li>
  556. <li>
  557. An <code>Object</code> containing the item to be deleted.
  558. The item will be deleted by its id.
  559. </li>
  560. <li>
  561. An Array containing ids or items to be removed.
  562. </li>
  563. </ul>
  564. <p>
  565. The method ignores removal of non-existing items, and returns an array
  566. containing the ids of the items which are actually removed from the
  567. DataSet.
  568. </p>
  569. <p>
  570. After the items are removed, the DataSet will
  571. trigger an event <code>remove</code> for the removed items.
  572. When a <code>senderId</code> is provided, this id will be passed with
  573. the triggered event to all subscribers.
  574. </p>
  575. <h3 id="Clear">Clear</h3>
  576. <p>
  577. Clear the complete DataSet.
  578. </p>
  579. Syntax:
  580. <pre class="prettyprint lang-js">var removedIds = DataSet.clear([senderId])</pre>
  581. <p>
  582. After the items are removed, the DataSet will
  583. trigger an event <code>remove</code> for all removed items.
  584. When a <code>senderId</code> is provided, this id will be passed with
  585. the triggered event to all subscribers.
  586. </p>
  587. <h2 id="Data_Selection">Data Selection</h2>
  588. <p>
  589. The DataSet contains functionality to format, filter, and sort data retrieved via the
  590. methods <code>get</code>, <code>getIds</code>, <code>forEach</code>, and <code>map</code>. These methods have the following syntax:
  591. </p>
  592. <pre class="prettyprint lang-js">
  593. DataSet.get([id] [, options]);
  594. DataSet.getIds([options]);
  595. DataSet.forEach(callback [, options]);
  596. DataSet.map(callback [, options]);
  597. </pre>
  598. <p>
  599. Where <code>options</code> is an Object which can have the following
  600. properties:
  601. </p>
  602. <table class="properties">
  603. <tr>
  604. <th>Name</th>
  605. <th>Type</th>
  606. <th>Required</th>
  607. <th>Description</th>
  608. </tr>
  609. <tr>
  610. <td>fields</td>
  611. <td>String[&nbsp;] | Object.&lt;String,&nbsp;String&gt;</td>
  612. <td>no</td>
  613. <td>
  614. An array with field names, or an object with current field name and
  615. new field name that the field is returned as.
  616. By default, all properties of the items are emitted.
  617. When <code>fields</code> is defined, only the properties
  618. whose name is specified in <code>fields</code> will be included
  619. in the returned items.
  620. </td>
  621. </tr>
  622. <tr>
  623. <td>type</td>
  624. <td>Object.&lt;String,&nbsp;String&gt;</td>
  625. <td>no</td>
  626. <td>
  627. An object containing field names as key, and data types as value.
  628. By default, the type of the properties of an item are left
  629. unchanged. When a field type is specified, this field in the
  630. items will be converted to the specified type. This can be used
  631. for example to convert ISO strings containing a date to a
  632. JavaScript Date object, or convert strings to numbers or vice
  633. versa. The available data types are listed in section
  634. <a href="#Data_Types">Data Types</a>.
  635. </td>
  636. </tr>
  637. <tr>
  638. <td>filter</td>
  639. <td>Function</td>
  640. <td>no</td>
  641. <td>Items can be filtered on specific properties by providing a filter
  642. function. A filter function is executed for each of the items in the
  643. DataSet, and is called with the item as parameter. The function must
  644. return a boolean. All items for which the filter function returns
  645. true will be emitted.
  646. See section <a href="#Data_Filtering">Data Filtering</a>.</td>
  647. </tr>
  648. <tr>
  649. <td>order</td>
  650. <td>String | Function</td>
  651. <td>no</td>
  652. <td>Order the items by a field name or custom sort function.</td>
  653. </tr>
  654. <tr>
  655. <td>returnType</td>
  656. <td>String</td>
  657. <td>no</td>
  658. <td>Determine the type of output of the get function. Allowed values are <code>'Array' | 'Object'</code>.
  659. The default returnType is an Array. The Object type will return a JSON object with the ID's as keys.</td>
  660. </tr>
  661. </table>
  662. <p>
  663. The following example demonstrates formatting properties and filtering
  664. properties from items.
  665. </p>
  666. <pre class="prettyprint lang-js">
  667. // create a DataSet
  668. var data = new vis.DataSet();
  669. data.add([
  670. {id: 1, text: 'item 1', date: '2013-06-20', group: 1, first: true},
  671. {id: 2, text: 'item 2', date: '2013-06-23', group: 2},
  672. {id: 3, text: 'item 3', date: '2013-06-25', group: 2},
  673. {id: 4, text: 'item 4'}
  674. ]);
  675. // retrieve formatted items
  676. var items = data.get({
  677. fields: ['id', 'date', 'group'], // output the specified fields only
  678. type: {
  679. date: 'Date', // convert the date fields to Date objects
  680. group: 'String' // convert the group fields to Strings
  681. }
  682. });
  683. </pre>
  684. <h3 id="Getting_Data">Getting Data</h3>
  685. <p>
  686. Data can be retrieved from the DataSet using the method <code>get</code>.
  687. This method can return a single item or a list with items.
  688. </p>
  689. <p>A single item can be retrieved by its id:</p>
  690. <pre class="prettyprint lang-js">
  691. var item1 = dataset.get(1);
  692. </pre>
  693. <p>A selection of items can be retrieved by providing an array with ids:</p>
  694. <pre class="prettyprint lang-js">
  695. var items = dataset.get([1, 3, 4]); // retrieve items 1, 3, and 4
  696. </pre>
  697. <p>All items can be retrieved by simply calling <code>get</code> without
  698. specifying an id:</p>
  699. <pre class="prettyprint lang-js">
  700. var items = dataset.get(); // retrieve all items
  701. </pre>
  702. <h3 id="Data_Filtering">Data Filtering</h3>
  703. <p>
  704. Items can be filtered on specific properties by providing a filter
  705. function. A filter function is executed for each of the items in the
  706. DataSet, and is called with the item as parameter. The function must
  707. return a boolean. All items for which the filter function returns
  708. true will be emitted.
  709. </p>
  710. <pre class="prettyprint lang-js">
  711. // retrieve all items having a property group with value 2
  712. var group2 = dataset.get({
  713. filter: function (item) {
  714. return (item.group == 2);
  715. }
  716. });
  717. // retrieve all items having a property balance with a value above zero
  718. var positiveBalance = dataset.get({
  719. filter: function (item) {
  720. return (item.balance > 0);
  721. }
  722. });
  723. </pre>
  724. <h3 id="Data_Types">Data Types</h3>
  725. <p>
  726. DataSet supports the following data types:
  727. </p>
  728. <table class="datatypes">
  729. <tr>
  730. <th>Name</th>
  731. <th>Description</th>
  732. <th>Examples</th>
  733. </tr>
  734. <tr>
  735. <td>Boolean</td>
  736. <td>A JavaScript Boolean</td>
  737. <td>
  738. <code>true</code><br>
  739. <code>false</code>
  740. </td>
  741. </tr>
  742. <tr>
  743. <td>Number</td>
  744. <td>A JavaScript Number</td>
  745. <td>
  746. <code>32</code><br>
  747. <code>2.4</code>
  748. </td>
  749. </tr>
  750. <tr>
  751. <td>String</td>
  752. <td>A JavaScript String</td>
  753. <td>
  754. <code>"hello world"</code><br>
  755. <code>"2013-06-28"</code>
  756. </td>
  757. </tr>
  758. <tr>
  759. <td>Date</td>
  760. <td>A JavaScript Date object</td>
  761. <td>
  762. <code>new Date()</code><br>
  763. <code>new Date(2013, 5, 28)</code><br>
  764. <code>new Date(1372370400000)</code>
  765. </td>
  766. </tr>
  767. <tr>
  768. <td>Moment</td>
  769. <td>A Moment object, created with
  770. <a href="http://momentjs.com/" target="_blank">moment.js</a></td>
  771. <td>
  772. <code>moment()</code><br>
  773. <code>moment('2013-06-28')</code>
  774. </td>
  775. </tr>
  776. <tr>
  777. <td>ISODate</td>
  778. <td>A string containing an ISO Date</td>
  779. <td>
  780. <code>new Date().toISOString()</code><br>
  781. <code>"2013-06-27T22:00:00.000Z"</code>
  782. </td>
  783. </tr>
  784. <tr>
  785. <td>ASPDate</td>
  786. <td>A string containing an ASP Date</td>
  787. <td>
  788. <code>"/Date(1372370400000)/"</code><br>
  789. <code>"/Date(1198908717056-0700)/"</code>
  790. </td>
  791. </tr>
  792. </table>
  793. </div>
  794. <?js= self.partial('tmpl/html-foot.tmpl') ?>