Giter VIP home page Giter VIP logo

svelte-data-grid's Introduction

npm

Svelte Data Grid

Svelte Data Grid is a svelte v3 component for displaying and editing any amount of data.

Features:

  • Excellent scrolling performance
  • ARIA attributes set on elements
  • Lightweight even when displaying a huge dataset due to implementation of a "virtual list" mechanism
  • Column headers remain fixed at the top of the grid
  • Custom components can be specified to control how individual table cells or column headers are displayed
  • Columns can be resized and reordered

Current Limitations:

  • Every row must have the same height and text cannot break onto the next line

Usage:

If using within Sapper:

npm install svelte-data-grid --save-dev

If using from inside a svelte component:

import DataGrid from "svelte-data-grid";
<DataGrid rows={myRows} allowColumnReordering={false} columns={myColumnDefinitions} on:columnOrderUpdated={saveNewColumnOrder}>

If using from outside svelte:

import DataGrid from "svelte-data-grid";
const grid = new DataGrid({
  target: document.querySelector('#my-grid-wrapper'),
  data: {
    rows: [ ... ],
    columns: [ ... ],
    allowResizeFromTableCells: true
  }
});

grid.$on('columnOrderUpdated', () => {
  const { columns } = grid.get();
  // save new column  order
});

To learn more about using DataGrid outside of svelte, read svelte's guide on how to interact with a svelte component. It is possible to integrate into any framework.

DataGrid requires 2 properties to be passed in order to display data: rows and columns.

columns is an array of objects containing at least 3 properties: display, dataName, and width. A svelte component can be specified in headerComponent and cellComponent if any custom cell behavior is required.

[
  {
    display: 'Fruit Name',  // What will be displayed as the column header
    dataName: 'fruitName',  // The key of a row to get the column's data from
    width: 300,             // Width, in pixels, of column
    disallowResize: true    // Optional - disables resizing this column
  },
  {
    display: 'Color',
    dataName: 'fruitColor',
    width: 600,
    myExtraData: 12345
  }
]

rows is an array of objects containing the data for each table row.

[
  {
    fruitName: 'Apple',
    fruitColor: 'Red'
  },
  {
    fruitName: 'Blueberry',
    fruitColor: 'Blue'
  },
  {
    fruitName: 'Tomato',
    fruitColor: 'Red'
  }
]

Editing Data

Version 2 added early support for editing data. Due to the lack of using a keyed each block to render the rows, maintaining focus on controls as the user scrolls is a tad wonky. This will be resolved in a future version.

Import the components:

import TextboxCell from 'svelte-data-grid/src/textbox-cell.svelte';
import SelectCell from 'svelte-data-grid/src/select-cell.svelte';
import CheckboxCell from 'svelte-data-grid/src/checkbox-cell.svelte';

Textbox Cell

Textbox cell will debounce the user input, only recording changes after 400ms has elapsed since the user stops typing.

{
  display: 'Name',
  dataName: 'name',
  width: 250,
  cellComponent: TextboxCell
}

Select Cell

SelectCell requires that you provide an options array in your cell definition:

{
  display: 'Eye Color',
  dataName: 'eyeColor',
  width: 75,
  cellComponent: SelectCell,
  options: [
    {
      display: 'Green',
      value: 'green'
    },
    {
      display: 'Blue',
      value: 'blue'
    },
    {
      display: 'Brown',
      value: 'brown'
    }
  ]
}

Checkbox Cell

CheckboxCell will set the checked state of the checkbox depending on the boolean value of the row's data.

{
  display: 'Active',
  dataName: 'isActive',
  width: 75,
  cellComponent: CheckboxCell
}

Custom Cell Components

Need to customize how your data is displayed or build more complex functionality into your grid? Specify cellComponent in your definition in the columns property.

Components will be passed the following properties:

  • rowNumber - The index of the row within rows
  • row - The entire row object from rows
  • column - The entire column object from columns

MyCustomCell.svelte

<script>
export let data = {
  colors: {
    Red: '#FF0000',
    Blue: '#0000FF'
  }
};
</script>

<div style="color: {colors[row.data[column.dataName]] || 'black'};">
  {row.data[column.dataName]}
</div>

Import the component

import MyCustomCell from './MyCustomCell.svelte';

columns option:

[
  {
    display: 'Fruit Color'
    dataName: 'fruitColor',
    width: 300,
    cellComponent: MyCustomCell
  }
]

Custom Header Components

Header components can also be specified in columns entries as the headerComponent property. Header components are only passed column, the column object from columns.

Options:

Svelte Data Grid provides a few options for controlling the grid and its interactions:

  • rowHeight - The row height in pixels (Default: 24)
  • allowResizeFromTableCells - Allow user to click and drag the right border of a table cell to resize the column (Default: false)
  • allowResizeFromTableHeaders - Allow user to click and drag the right border of a column header to resize the column (Default: true)
  • allowColumnReordering - Allow user to drag a column header to move that column to a new position (Default: true)
  • allowColumnAffix - Allow user to drag the double line to affix columns to the left side of the grid. See section below for caveats (Default: true if the browser is chrome, false otherwise)
  • __extraRows - If it is desired that the virtual list include more DOM rows than are visible, the number of extra rows can be specified in __extraRows (Default: 0)
  • __columnHeaderResizeCaptureWidth The width of the element, in pixels, placed at the right border of a column that triggers that column's resize. (Default: 20)

Events:

  • columnOrderUpdated - Fired when the user has dragged a column to a new position. The updated column order can be accessed from component.get().columns
  • columnWidthUpdated - Fired when a user has resized a column. The updated column width can be accessed from event.width and the column index can be accessed from event.idx

Column Affixing

This feature works well on Chrome because Chrome's scroll events are not fired asynchronously from the scroll action. Firefox, Edge, and IE all fire scroll events after the overflow container has scroll on screen. This causes a jittery effect that we cannot easily work around while providing a cross-browser solution.

To fix the jitteriness on Firefox, a setting in about:config can be changed to turn off APZ. Set layers.async-pan-zoom.enabled to false. Obviously this is not a solution we can reasonably ask users to try, so I'm looking for other solutions.

Bugs? Suggestions?

Feedback is always appreciated. Feel free to open a GitHub issue if DataGrid doesn't work the way you expect or want it to.

svelte-data-grid's People

Contributors

bighamster avatar bsssshhhhhhh avatar floer32 avatar thecodejack avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

svelte-data-grid's Issues

Svelte 3

Hi,

any plan to port this library to Svelte 3?

Can I take a stab at a scrolling workaround for non-Chrome-based browsers?

The README mentions,

Column Affixing

This feature works well on Chrome because Chrome's scroll events are not fired asynchronously from the scroll action. Firefox, Edge, and IE all fire scroll events after the overflow container has scroll on screen. This causes a jittery effect that we cannot easily work around while providing a cross-browser solution.

To fix the jitteriness on Firefox, a setting in about:config can be changed to turn off APZ. Set layers.async-pan-zoom.enabled to false. Obviously this is not a solution we can reasonably ask users to try, so I'm looking for other solutions.

Can I give a shot at helping? It seems like there are some cross-browser methods that some comparable libraries have implemented.

(Off top of head, one example that is coupled to react is here - but obviously don't want to couple to React - and another example is clusterize - but don't want to depend on another library. Maybe there is no 1 perfect approach, but maybe only 1 fallback is needed for the non-chrome-based browsers...? That'd be sweet. Maybe I'm dreaming though!)

I'm curious to try it out, because I've had to do proprietary versions of some similar functionality. In my career so far I've focused on backend more than frontend, so it could be a good exercise for me. Only thing is - it'd be good to get a better understanding of the problem first. Are there specific lines in the source, that would be a good entrypoint if I tried a fix?

Input on Enter, Jump cells on Enter, Add new row

This is fundamental to tables.

  1. When you are done entering data, submit it with pressing Enter.

  2. When you press Enter, you jump cells as if you pressed Tab. If you use any other key, you edit the data in the cell.

  3. If you are on the last row, on the last cell, pressing enter adds a new row.

Error when using with SvelteKit

As I said in the title, I got an error when trying to use this with SvelteKit:

Uncaught (in promise) SyntaxError: import not found: default edit-history.js:1:7
    instance http://localhost:3000/node_modules/svelte-data-grid/src/main.svelte:131
    AsyncFunctionThrow self-hosted:699

It chocked on the deep-diff import.
Any advice?

Cell Borders not showing

The cell borders do not show.

It looks like it is a problem with the difference between border-box and content-box.

What I can figure out, is that the top position of each row overlays the bottom border of the previous row.

The vertical column lines also do not show.

Any help with this?

Improvements

Hi @bsssshhhhhhh

Thanks for releasing v3.0. I have listed some of features and changes that can make the svelte-data-grid more useful. Probably bit of selfish from myself while adding features but would love if we can have following with the data-grid.

API Suggestions

  • Config based default editors rather asking developers to import the editor and set it via column config
  • Fix edit issues
  • Add column type(number, string)
  • Check of adding more grid features like
    • Sorting
    • Grouping
    • Filtering
    • Pagination vs async data load on scroll
  • More accessibility features

Infra Suggestion

  • Update documentation
  • Spit out Virtual List component
  • Testing setup
  • Check for possibility of applying external styles easily

If possible, I would love to be one of collaborators to take this forward.

Table is not updating when the rows values changed

Hi,

I'm currently using library. On initial load the rows will be an empty []. After http call i get the data but the data row data is not getting updated. How to solve this issue?

Thanks for this awesome library

No rows displayed and bugs

Newish to svelte but learning fast

I get the following warnings that seem to prevent rows displaying (cols display just great)

(!) Plugin svelte: Src has unused export property '__affixedRowIndices'. If it is for external reference only, please consider using `export const __affixedRowIndices`
node_modules\svelte-data-grid\src\index.svelte
140:   export let __columnHeaderResizeCaptureWidth = 20; // The width of the area on column borders that can be clicked to resize the column
141:   /**** Do not modify any of the data variables below ****/
142:   export let __affixedRowIndices = []; // DO NOT MODIFY DIRECTLY. The row indices to affix to the top of the grid
                  ^
143:   export let __affixedColumnIndices = []; // DO NOT MODIFY DIRECTLY. The column indices to affix to the left side of the grid
144:   export let __affixingRow = false; // DO NOT MODIFY DIRECTLY. Whether a row affix operation 
is in progress
(!) Plugin svelte: Src has unused export property '__columnAffixLineLeft'. If it is for external reference only, please consider using `export const __columnAffixLineLeft`
node_modules\svelte-data-grid\src\index.svelte
146:   export let __rowActionLineTop = 0; // DO NOT MODIFY DIRECTLY. The 'top' position of the row action line
147:   export let __rowAffixLineTop = 0; // DO NOT MODIFY DIRECTLY. The 'top' position of the row 
affix line
148:   export let __columnAffixLineLeft = 0; // DO NOT MODIFY DIRECTLY. The 'left' position of the column affix line
                  ^
149:   export let __columnDragging = false; // DO NOT MODIFY DIRECTLY. Whether a column is being dragged
150:   export let __columnIndexBeingDragged = null; // DO NOT MODIFY DIRECTLY. The column index that is being dragged

svelte 3.31.2
node 15.3.0

I would rather not go tinkering and mess up this great rep

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.