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

tabled

v0.3.6

Published

Backbone UI component for building interactive tables.

Downloads

64

Readme

Tabled

Backbone UI component for building interactive tables.

##Usage

var Tabled = require('tabled');
var mycollection = new Backbone.Collection([ // can be any backbone collection
    { name: "andy", age: 24 },
    { name: "scott", age: 26 },
    { name: "tevya", age: 32 }
]);
var mycolumns = [
    { id: "name", key: "name", label: "Name" },
    { id: "age", key: "age", label: "Age" }
]; // see below for all column options

// This returns a backbone view
var table = new Tabled({
    collection: mycollection,
    columns: mycolumns
    // more options are available (see options section below)
});

// Treat the same as any other view:
table.$el.appendTo($my_el);
table.setElement(".some-element");

##Options

There are several special options you can pass to Tabled besides the collection and columns:

###id (optional)

The id of this table instance. Used to store information about the table in localStorage for table attributes you want saved between sessions, eg. when setting the save_width option to true. All data stored in localStorage is opt-in and only takes up a single key in localStorage per table. The key will be tabled.[ID] on localStorage.

###adjustable_width (default: true)

Boolean Set to false if you'd like the table not to have an adjustable width.

###save_state (default: false)

Boolean Saves the state of the table each time the user changes it. This means that column and row sort orders and widths of the columns will be saved between sessions.

###table_width (default: 'auto')

Int or String Set an explicit default width in pixels for this table. If this is set to 'auto' (the default value) the table will be rendered at the available width inside of its container.

WARNING: If the table is hidden on initialization, a table_width of 'auto' will result in a width of 0, in which case the table will automatically be rendered with a width of [number of columns] * [min width of columns].

Also note that you cannot set a width to something less than the sum of the minimum column widths (the width will simply be calculated as if the width was 0 as noted earlier).

###min_column_width (default: 20)

Int The default minimum column width in pixels that any one column can be. This can be overridden on a per-column basis by setting the same key on the column object.

###row_sorts (default: []) Array Specify the sort precedence of columns by providing an array of column ids. For example, passing an array like this: ["last_name", "first_name", "age"] will sort the data first by last_name, then first_name, then age. The sort directions of these columns should be set using the sort_value option on the column object (see below).

###col_sorts (default: []) Array Specify the column order by their ids in an array.

Example: ["id", "last_name", "first_name"]

##Columns

tabled expects an array of column objects to be past to it via the columns key . The following are all the keys possible on a column object

###id

String unique id for the column

###key

String the main attribute of each model that this column will display or use in its format function

###label

String the name of the column that will be displayed at the top of the table

###sort (optional)

String or Function Allows the column to be sortable by clicking on the column header. If this is a string, it should refer to a predefined sort method (currently 'number' and 'string'). If it is a function, it should be in the form shown below:

sort: function(model1, model2) {
    // return value < 0 if model1 ranks higher than model2
    // return value > 0 if model2 ranks higher than model1
    // return 0 if model1 and model2 are of the same rank
}

This function defines the ascending order, and the descending order is defined by reversing the order of the arguments.

###sort_value (optional)

String (a|d) If you want to set an initial sort direction for a column, set this option to either 'a' for ascending or 'd' for descending. To set multiple column sorts in a specific order, use this in conjunction with the sort key on the options of the main tabled view (see above).

###filter (optional)

String or Function Adds a text field at the top of the column where the user can type values that will filter the list. If filter is a string, this should refer to a predefined filter method (currently 'number' and 'like'). If it is a function, it should be in the following format:

filter: function( filter_field_value, raw_row_value, formatted_row_value, row_model ) {
    // return truthy value to include this row
    // return falsy value to exclude
}
  • filter_field_value: the text contained in the filter filed
  • raw_row_value: the value of model.get(column.key)
  • formatted_row_value: the value outputted by the format function, if there. If there is no format function, this will be the same as raw_row_value
  • row_model: the model that the filter is being applied to

This function should return true if the row should be included in the filtered set, and false if it should not be.

###format (optional)

Function or String If a function, it should return a string, DOM element, or jQuery object to be used as the value of the row. Receives the value of model.get(column.key) and the model itself as arguments:

format: function(value, model) {
    // return something here
    // note: under the hood, this value is added with $().append()
}

If it is a string, it should be one of the predefined formatting functions: ####'select'

The cells in that column will be checkboxes that, when checked or unchecked, will change the value of the key on the model to true or false, respectively. This will then emit appropriate change events on the model and collection of data. Consider the following column definition object:

// ...in column definitions...
{ id: "selector", select: true, key: "selected" , label: "" }

Each row of data will have a column with a checkbox, and when one is clicked, the model's selected attribute will be changed to true (or false if unchecking), eg. model.set('selected', true);

##Examples

To run the examples, do the following:

npm install .
jam install
make all

Then, simply view the index.html page of the example you want to see in a browser.