npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zambezi/simple-table

v1.0.1

Published

D3 components to build simple HTML tables from your data.

Downloads

5

Readme

Simple Table

D3 components to build simple HTML tables from your data.

Installation

You can npm i @zambezi/simple-table the component and can check the 'examples' folder for usage.

The simplest case

If you have an array of objects, dataElements, and a target DOM node that supports <tr>s and <td>s,

<table class='target'></table>
var table = createTable()
d3.select('table.target').datum(dataElements).call(table)

The component will create one row for each of the elements, and one cell for each of its attributes. By default it'll try to represent each of those attributes as a string.

Further configuration

You can configure which columns you want to be displayed.

  • The key property is used to extract the attribute for the column.
  • The format function will be called once with each row object, or with the row attribute if key was also defined.
  • The className property, if defined, will set the className on the cells it owns. This can also be set to a function for dynamic styling. The function will be invoked with a single argument of the cell's value.
var table = createTable().columns(
      [
        { key: 'name' }
      , { key: 'email', className: 'text-light-secondary' }
      , { format: (d) => d.address.city }
      , { key: 'price', format: d3.format('.3f') }
      ]
    )

d3.select('table.target').datum(
  [
    {
      name: 'Álvaro'
    , email: '[email protected]'
    , address: { city: 'London' }
    , price: 234234,23433223
    }
  , {
      name: 'Ignacio'
    , email: '[email protected]'
    , address: { city: 'London' }
    , price: 111111,234234234
    }
  ]
).call(table)

Table headers

You can show table headers like this:

  • enable headers
  • put a label property for each column definition
table = createTable()
    .displayHeaders(true)
    .columns(
      [
        { key: 'name', label: 'Name' }
      , { key: 'email', label: 'Email', className: 'text-light-secondary' }
      , { format: (d) => d.address.city, label: 'City' }
      , { key: 'price', label: 'Price', format: d3.format('.3f') }
      ]
    )

Selection

The simple-table component has the concept of selected rows. You can define which rows are selected by using its selected getter/setter. This property takes an array of objects subset of the data bound on the DOM node. For each of the rows which are also in the selected collection, the table will add the is-selected class. It's up to you to define the style for your particular case.

Events

When you click on a table row, the component will dispatch a select event. You can use that to manipulate the selected collection and redraw the table.

The logic of how this event should be interpreted is up to the client.

var target = d3.select('table.target').datum(data)
  , table = createTable().on('select', addToSelection)

draw()

function draw() {
  target.call(table)
}

function addToSelection(row) {
  table.selected([ row ]) // Basic single selection
                          // You can potentially append to this list instead
                          // to achieve multiple selection.
  draw()
}

Style

The simple-table component does not provide styles. You should, if necessary, define yours.