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

solenya-tables

v1.2.7

Published

* A databound table with filtering, sorting, and paging for solenya. * A MasterDatail component for routed drill downs (e.g. drilldown from a customer table to a particular customer)

Downloads

6

Readme

Solenya Tables

  • A databound table with filtering, sorting, and paging for solenya.
  • A MasterDatail component for routed drill downs (e.g. drilldown from a customer table to a particular customer)

Installation

Assuming you have solenya installed, you will need the following npm packages:

solenya-tables
bootstrap
popper.js

Currently solenya-tables superficially depends on bootstrap for styling & menus. That dependency will be removed in the future.

Table Usage

The usage of Table<T> is as follows:

export class TableSample extends Component
{
    @transient table = new PresidentTable ()
}

class PresidentTable extends Table<President>
{
    @Type (() => President) results?: President[]

    attached() {
        this.doLoad()
    }   

    async load (query: ITableQuery) {   
        return presidents
    }

    view() {
        const o = new President()
        return super.view ({css: tableStyle, guideObject: o, columns:
        [
            { prop: () => o.name, sortable: true },
            { prop: () => o.age, label: "Age Inaugurated", sortable: true }
        ]})    
    }
}

Where President is defined as:

class President extends Component
{    
    name = ""
    age?: number    
}

The @Type decorator tell's the class-transformer serialization package the type of the array. The guideObject gives Table<T> metadata about T so it can automatically detect column information like label values. This is necessary because unlike in Java or C#, the type of an array is only known at compile time.

You need to implement the load method on table to get data into your table. It takes an ITableQuery object and returns a promise to an ITableResult<T> object. Their definitions arre:

export interface ITableQuery
{
    from: number
    pageSize: number
    search?: string
    sort?: string
}

export interface ITableResult<T>
{
    total: number
    results?: T[]
}

The Table<T> type maintains its current page, search filter, and sort values. These values will be fed to the ITableQuery object that's passed to the load method.

Column

The Column type allows you to customize the property that's displayed using the display field. If we added a date field to President, then we could display that date with another column, as follows:

columns: [
    ...
    {
        prop: () => o.date,
        display: p => "" + p.date.getFullYear(),
        sortable: true
    }
]

We can let users filter the table by a column value by giving them options on that column to filter by. In this example, we can add a party field to President, and then let the user filter presidents by party options :

columns: [
    ...
    {
        prop: () => o.party,
        sortable: true,
        options: parties.map(p => ({
            label: p.party,
            value: "is:"+p.party
        }))
    }
}

When the user chooses the option, the load method will be called, where the search field on ITableQuery is set to the value you gave for that option.

Here we perform the filtering locally using the arrayToTableResult convenience method. This will automatically apply sorting and paging (you must provide the filtering). If the result set was large, we'd instead need to perform the filtering, sorting, and paging on the server.

class PresidentTable {
    ...    
    async load (query: ITableQuery)
    {        
        return this.arrayToTableResult (presidents, r => r.filter (this.search))
    }
}

class President {
    ... 
    // would instead be implemented on server if results set was large
    filter (search?: string)
    {
        if (! search)
            return true

        if (search.startsWith ("is:"))
            return this.party == search.substring ("is:".length)

        const reg = new RegExp (search, "i")
        return reg.test (this.name)
    }
}

Master Detail

It's very common to want to drill down on a particular row, where the route is updated accordingly. We can do that using a MasterDetail component, as follows:

export class TableSample extends MasterDetail<President>
{
    @transient table = new PresidentTable ()

    async getItem (name: string) {
        return presidents.find (p => p.routeName == name)
    }  
}

Where we augment President as follows:

class President extends Component implements IRouted
{    
    @transient router: Router = new Router (this)
    @transient get routeName() { return encodeURIComponent (this.name.replace (/ /g, "_")) }

    @Label("Inaugurated")
    @Type(() => Date) date!: Date
    name = ""
    party = ""    
    @Label("Age Inaugurated") age?: number    

    view() {
        return (
            div({ class: "card" },
                div({ class: "card-header" },
                    this.router.parent!.navigateLink("", "Go to table")
                ),
                div({ class: "card-body" },
                    labeledValue (this, () => this.name),
                    labeledValue (this, () => this.party),
                    labeledValue (this, () => this.age),
                    labeledValue (this, () => this.date, x => x.getFullYear())                    
                )
            )
        )
    }
}

MasterDetail uses Solenya's composable router. This means the relationship between the master (table of presidents) and detail (particular president) is encapsulated in your component. You can then insert that component within another route, without having to alter global configuration for your routes.

For convenience, the MasterDetail can display a search box that shows its current search value:

export class TableSample extends MasterDetail<President>
{
    ...
    view() {
        return super.view({
            showSearchBox: true
        })
    }
}

Table without a Table

Table<T> provides the logic of filtering, paging, and sorting over a set of objects of type T. By default, its view method provides an HTML table view of its state, but you can override its view method to provide a non table UI.

class PresidentTable {
    ...    
    view() : VElement {
        // provide your own view
    }
}

PageSize

You can specify the pageSize when creating a Table<T>:

    new Table<President> ({ pageSize: 20 })