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

edittable

v0.1.2

Published

A simple record table for react with editable fields and a REST API interface

Downloads

8

Readme

Edit Table

A simple React table to edit tabular data either with a REST API or through variables and functions:

Example of the table

Another simple example:

import EditTable from './edittable.jsx';

var data = [
  { id: '1', name: 'Test1', email: '[email protected]', description: 'Bla 1' },
  { id: '2', name: 'Test2', email: '[email protected]', description: 'Bla 2' },
  { id: '3', name: 'Test3', email: '[email protected]', description: 'Bla 3' }
];

class DataExample extends React.Component {
  render () {
    var fields = { name: 'Name *', email: 'Email *', description: 'Description' };
    return <EditTable name="users" url="/api/users" fields={fields} data={data} />;
  }
}

Fields

There are many fields that can be used by default and new ones can be created easily. This is the most important configurable part of your app. They will manipulate and display your data for better integration. Full list of properties for reference:

var fields = {
  first: { // any name here
    header: 'first',       // For the table header
    name: 'first',         // The field (and data key) name
    placeholder: 'first',  // The text to show as placeholder
    type: 'text',          // For validation and manipulation
    required: false,       // Self-explanatory. Parses the 'header *' if omitted
    readonly: false,       // Only display it, but cannot edit it
    validate: (val, cb, self) => cb(true)  // Validates the data as it's edited
    display: (val, cb, self) => cb(val),   // Transformation for displaying data
  }
};

Read the full fields documentation to know how to use them properly:

Fields Documentation

Data format

The data retrieved should have a particular format: an array of elements, where each element is an object with keys as the field name and value as the value for that instance. Each element is required to have a unique id (the format of it doesn't matter):

var data = [
  { id: '1', name: 'Test1', email: '[email protected]', description: 'Bla 1' },
  { id: '2', name: 'Test2', email: '[email protected]', description: 'Bla 2' },
  { id: '3', name: 'Test3', email: '[email protected]', description: 'Bla 3' },
  { id: '4', name: 'Test4', email: '[email protected]', description: 'Bla 4' }
];

This format is the one expected in case you pass down the data as a property or in case you retrieve it through the API.

API

By default it is a simple REST API:

var api {
  get: function(callback){
    this.ajax(this.url, 'GET', {}, callback);
  },
  post: function(data, callback){
    this.ajax(this.url, 'POST', data, callback);
  },
  put: function(id, data, callback){
    this.ajax(this.url + '/' + id, 'PUT', data, callback);
  },
  delete: function(id, callback){
    this.ajax(this.url + '/' + id, 'DELETE', {}, callback);
  }
};

Use Cases (Examples)

Edit Table is really flexible, and while it was initially thought for a simple REST API now it's configurable deeply. Let's start with the default and easy case though:

REST API

For a simple REST API you can pass only the url:

import EditTable from './edittable.jsx';

class SimpleApi extends React.Component {
  render () {
    var fields = { name: 'Name *', email: 'Email *', description: 'Description' };
    return <EditTable url="/api/users" fields={fields} />;
  }
}

To make it token-based, you can do it just passing the token (code simplified):

var token = cookies('token');  // Using http://github.com/franciscop/cookies.js
<EditTable url="/api/users" token={token} fields={fields} />;

And to use your own authentication method, you would overwrite the method auth within the API:

var auth = function(data, callback){
  data = data || {};
  if (cookies('token')) {   // Using http://github.com/franciscop/cookies.js
    data.authtoken = cookies('token');
  }
  callback(data);
}
<EditTable url="/api/users" auth={auth} fields={fields} />;

Local Manipulation

We also support not using a REST API. For instance, let's store the data within a variable (no persistence):

import EditTable from './edittable.jsx';

class UserList extends React.Component {
  constructor(){
    super();
    this.state = { data: [
      { id: '1', name: 'Test1', email: '[email protected]', description: 'Bla 1' },
      { id: '2', name: 'Test2', email: '[email protected]', description: 'Bla 2' },
      { id: '3', name: 'Test3', email: '[email protected]', description: 'Bla 3' },
      { id: '4', name: 'Test4', email: '[email protected]', description: 'Bla 4' }
    ]};
  }

  update(data){
    this.setState({ data: data });
  }

  render () {
    var fields = { name: 'Name *', email: 'Email *', description: 'Description' };
    var data = this.state.data;
    var update = this.update.bind(this);
    return <EditTable fields={fields} data={data} update={update} />;
  }
}

Bake your own

To make your own persistence model, just overwrite the API. This is the footprint of them:

var api = {
  get: function(callback){ /* write your own */ },
  post: function(data, callback){ /* write your own */ },
  put: function(id, data, callback){ /* write your own */ },
  delete: function(id, callback){ /* write your own */ }
};
<EditTable fields={fields} api={api} />;