Skip to content

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Repository files navigation

@simple-html/grid

A data grid for plain TypeScript or JavaScript apps, no framework needed. A Datasource holds your rows and knows the type of each field: it sorts, filters and groups them, keeps the selection, and tracks and validates the edits for a server. The grid shows it with virtual scrolling, pinned and stacked columns, header filters, a filter editor, grouping, editing and right click menus. Its only dependency is lit-html, which is installed with it.

The grid's config and the datasource's fields are plain JSON, so they can be stored - in a backend, say. Formatters and validators are named by id (registerFormatter(), registerValidator()), and the functions of a grid - readonly cells and css classes from the row, changes to the menus - are hooks, given next to the config.

Docs, guides and live samples: https://vegarringdal.github.io/simple-html/. The guides are also in this repo, see Guides below.

Features

The highlights. Every feature, each linked to its guide and a live sample: Feature overview (in this repo: guides/00-features.md).

  • Typed fields - text, numbers, dates, date and time, yes/no - sorted, filtered and edited by their type
  • Pinned and stacked columns: the user moves, resizes, hides and fits them to their content
  • Header filters with operators, a value list as in a spreadsheet, date and range pickers, filter as you type, and a filter editor for nested AND / OR groups
  • Sorting on several columns, grouping by drag and drop, aggregates in the group rows and a totals row
  • Ranges of cells: copy and paste to and from Excel, fill down, a summary of the selected cells
  • Editing with validation, undo and redo, change tracking and a save workflow - and permissions to add, change and delete rows
  • Lookups: a value picked from a select or a dropdown grid
  • Display formats, templates and links, colour rules - the user sets them in the grid config dialog
  • A detail form beside the rows, with the current row
  • Find (Ctrl+F) with match case and filter rows
  • Export to Excel (.xlsx) and CSV
  • Save and restore what the user changed: the layout, sorting, grouping, filters, formats
  • Live data: rows from a server merged by key, the user's edits kept
  • Two themes, light and dark; every text translatable; the keyboard, screen readers and touch
  • toSqlWhere(): the filter as a SQL WHERE for PostgreSQL, Oracle, SQLite and SQL Server, safe from injection

Install

The package is not on npmjs.com. The simplest: every release has the package as a tgz on its GitHub Release. Install it from its address - no token, no alias, the imports are @simple-html/grid:

npm install https://github.com/vegarringdal/simple-html/releases/download/v6.0.0/simple-html-grid-6.0.0.tgz

Or download the tgz, put it in your project and install the file (npm install ./libs/simple-html-grid-6.0.0.tgz).

It is also published to GitHub Packages as @vegarringdal/simple-html-grid (the scope of the repository's owner): install it under its usual name with an npm alias, so the imports stay @simple-html/grid. GitHub Packages needs a token that may read packages (a personal access token with read:packages), in an .npmrc next to your package.json:

@vegarringdal:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}
npm install @simple-html/grid@npm:@vegarringdal/simple-html-grid                # the last release
npm install @simple-html/grid@npm:@vegarringdal/simple-html-grid@6.0.0          # one version
npm install @simple-html/grid@npm:@vegarringdal/simple-html-grid@next           # the last dev build
npm install @simple-html/grid@npm:@vegarringdal/simple-html-grid@beta-<branch>  # the last beta of a branch

A release is published from master by hand (the publish workflow, see Releases). A dev build from any branch (the next workflow, <version>-next.<run>, npm tag next) and a beta from any branch (the beta workflow, <version>-beta.<branch>.<run>, npm tag beta-<branch>) are run from the Actions tab too. All of them only publish what passes the checks. See scripts/githubPackage.js.

Or install it from a git branch, the prepare script builds dist on install:

"dependencies": {
    "@simple-html/grid": "github:vegarringdal/simple-html#<branch>"
}

Or run npm run build:tgz in a clone and copy the tgz from build/ to your project:

"dependencies": {
    "@simple-html/grid": "file:./libs/simple-html-grid-X.Y.Z.tgz"
}

A first grid

import { createGrid, Datasource, type GridConfig } from '@simple-html/grid';
import '@simple-html/grid/grid.css';
import '@simple-html/grid/date.css'; // the date picker of date cells
// a second theme, the TreDeSpace UI colours: on with <html data-grid-theme="tredespace">
// import '@simple-html/grid/theme-tredespace.css';

// the fields that are not text, with their type
const datasource = new Datasource({
    key: 'id',
    fields: { age: 'number', born: { type: 'date', label: 'Born' } }
});
datasource.setData([
    { id: 1, name: 'Ann', age: 36, born: new Date(1990, 4, 17) },
    { id: 2, name: 'Bob', age: 41, born: new Date(1985, 0, 2) }
]);

// where the columns go, and the options of a column by field name
const config: GridConfig = {
    columns: { name: { label: 'Name' } },
    layout: {
        center: [
            { fields: ['name'], width: 150 },
            { fields: ['age'], width: 80 },
            { fields: ['born'], width: 110 }
        ]
    }
};

// the grid fills the container, so it needs a height: <div id="grid" style="height: 400px"></div>
const gridInterface = createGrid(document.getElementById('grid')!, { config, datasource });

gridInterface.updateConfig({ readonly: false }); // the grid is readonly by default
// a function is a hook, not part of the config: here a css class on the cells of the young
gridInterface.updateHooks({ cellClass: (row, field) => (field === 'age' && row.age < 18 ? { input: 'young' } : null) });
const state = gridInterface.getState(); // what the user changed: sorting, filters, column order ...
gridInterface.destroy(); // done with it

Guides

  1. Getting started
  2. Grid config
  3. State
  4. Datasource
  5. Editing
  6. Filtering
  7. Sorting and grouping
  8. Formatting
  9. Events
  10. Menus
  11. Migrating from v5
  12. Texts

Migrating from v5

v6 changes the API. MIGRATION2V6.md lists every breaking change with code before and after, and CHANGELOG.md what is new in 6.0. The migration guide shows the ten changes almost every app needs.

Development

git clone https://github.com/vegarringdal/simple-html
cd simple-html
npm i
npm start # the docs and samples site on http://localhost:8080
  • PORT=8090 npm start serves it on another port
  • npm test runs the tests, npm run check:docs checks the guides, the JSDoc and their examples
  • npm run build builds dist, npm run build:tgz the tgz
  • publishing and the docs site: see Releases below
  • how the code is built and why, and what is known not to work yet: DESIGN.md

Releases

Every change goes to master through a pull request. Its title is a conventional commit, the pull request check (.github/workflows/pr.yml, scripts/prTitle.js) fails until it starts with one of fix:, feat:, fix!: / feat!: (something breaks), chore:, docs:, test:, refactor:, ci:, build:, perf: or style:. A scope is fine (feat(grid): a find bar). The tests run on it as on every push (checks.yml).

The version is set by hand in package.json (and package-lock.json), in a release pull request - see Making a release below. A release is then one workflow, publish (publish.yml), run by hand from the Actions tab on master. It pushes no commit:

  • the checks
  • the GitHub Release v<version> with the package as a tgz (npm pack, the name @simple-html/grid) and the CHANGELOG.md entries since the release before as its notes (scripts/releaseNotes.js). The release makes the tag v<version> on the commit, and stops when the tag is on another commit
  • the package to GitHub Packages with the npm tag latest. Stops when that version is published already

A pull request to master runs it as a dry run (release dry run, release-dry-run.yml): its own steps, nothing tagged, released or published, so a release that would fail fails in the pull request. The run's summary lists the files of the tgz. A version published already is a warning there.

Making a release

The version changes only in a release pull request: the version check of pr.yml (scripts/prVersion.js) fails a pull request that changes it unless it comes from a branch of this repository (not a fork) named release/<version>, with the title chore: <version>, a final version higher than master's, and package-lock.json at the same version. A release/... branch that does not change the version fails too. So a version is not bumped by accident.

In a clone of vegarringdal/simple-html:

git switch master && git pull
git switch -c release/6.0.1
npm version 6.0.1 --no-git-tag-version   # package.json and package-lock.json, no git tag
git commit -am "chore: 6.0.1" && git push -u origin release/6.0.1

Open a pull request from release/6.0.1 to master with the title chore: 6.0.1. When its checks and release dry run are green, merge it, then run publish (Actions -> publish -> Run workflow, on master). It makes the release v6.0.1 with the tgz and its tag, and publishes to GitHub Packages. The release notes are the changelog's entries since the release before.

The docs site

.github/workflows/pages.yml puts the docs site (npm run build:demo) on the gh-pages branch:

The sidebar of the site says what it is built from: the version, the branch, the commit and its date, and a dev badge on a branch's docs that links to the released ones.

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages