spotfiresoftware / spotfire-mods

Spotfire® Mods
https://spotfiresoftware.github.io/spotfire-mods/
Other
56 stars 41 forks source link

How to find row with elementId from dataView.allRows() #36

Closed hski-github closed 3 years ago

hski-github commented 3 years ago

In rows = dataView.allRows() for a row there is the attribute elementId, which should be used to identify an id. But how do I later find the row with the elementId from rows? rows.find(elementId)?

I want to use elementId as an attribute when rendering a row as SVG object or HTML div and then in an onClick event retrieve the elementId again to set marking.

Maybe you can add this information as an example to the Mod data views page. https://tibcosoftware.github.io/spotfire-mods/docs/using-the-api/mod-data-views/

hski-github commented 3 years ago

It is as simple as var row = rows.find( obj => { return obj.elementId() === elementId }); though I am not a real JavaScript developer and other would prefer different syntax.

Maybe useful to add to the documentation when writing about elementId().

ebrandin commented 3 years ago

I believe this is documented in the API documentation when stating "Calculates an element id for this row, suitable for identifying rows between data views."

The documentation also indicates that there is a difference when comparing rows from one single dataview. Although the approach in comparing the result of row.elementId() will work when identifying rows within the dataview, it is more effecient to use referential equality on rows in that case. That may for example come in handy if you want the intersection between the rows from leaf nodes from different hierarchies, for example X- and Color axis of a bar chart. (note that the example below will not work in all browsers since neither the arrow function nor 'includes' is available in IE11)

let xRows = xLeaf.rows();
let colorRows = colorLeaf.rows();
let intersection = xRows.filter(r => colorRows.includes(r));
hski-github commented 3 years ago

Okay, in general I would say adding more sample code to the API documentation would be useful and help newbees, but on the other hand this topic about use of elementId is quite normal and regular JavaScript topic, so I close this issue.