npm.io
0.2.1 • Published 3 years ago

easy-tables

Licence
ISC
Version
0.2.1
Deps
0
Size
83 kB
Vulns
0
Weekly
0

Easy Tables

A UI Library For Creating Html Tables With Ease. And Making Your Existing Html Tables Responsive With Sorting And Selecting Functionality.

Table of content :

Getting Started :

<script defer src="https://unpkg.com/easy-tables@0.2.0/dist/production.bundle.js"></script>

Usage: Existing Tables:

1. Using Attributes:

2. Using JavaScript:

Usage: Create New Tables:

Initiate the table:

Define a table instance like the following:

const myTable = new easytables.Table(
    wrapper :str,
    rows :array or :object,
    options :object,
)

1. The "wrapper" parameter :

the css selector for the created container. for example : "#my-container"

2. The "rows" parameter :

the rows parameter is used to represent the data of each row. it could be an array of objects in case you had the data locally. it can also be an object containing the endpoint and callbacks to fetch the rows array from an API. Check out how to Fetch "rows" data from an API.

the "rows" parameter is an array of objects. each object represents a single table row, defined as following :

rows = [
    {
        column key : cell value,
        ...
    },
    ...
]

for example :

rows = [
    {
        "first name" : "Bill",
        "last name" : "Gates",
        "username" : "billgates",
    },
    {
        "first name" : "Larry",
        "last name" : "Page",
        "username" : "larrypage",
    },
    ...
]
3.The "options" parameter : the options parameter is used to set different options for the table. the parameter can be set directly or by calling an API. Check out how to Fetch "options" from an API.
The "options" properties are listed below:

Table headers types:

The value of the "headers" property of the "options" depends on the type of the header. The default type is "text". The header types are :

1. text : is the basic header type for data displayed as normal text. the "text" header options are :

type : optional, text : optional, filter : optional, data : optional, sort : optional, render : optional

Example :

headers: {
    "name": {
        type: "text",
        text: "Full Name",
        sort: false,
        filter: (console.log("filtering by name .."))
    }
}
2. bold : similer to "text" but displayed in bold font. takes the same header options as "text" :

type : required, text : optional, filter : optional, data : optional, sort : optional, render : optional

Example :

headers: {
    "company": {
        type: "bold",
        text: "Company Name",
        sort: true,
        filter: (console.log("filtering by company .."))
    }
}
3. image : displays a circular image. uses the data from the "rows" parameter as a source for the image. "data" option is false by default. takes the following options:

type : required, text : optional, render : optional, data : optional

Example :

headers: {
    "avatar": {
        type: "image",
        text: "Avatar"
    }
}
4. label : displays a colored label image. the color can be set dynamically using the "colorCode" option which sets the color based on the value of the column. takes the following options:

type : required, text : optional, filter : optional, sort : optional, render : optional, data : optional, colorCode : optional, color : optional

Example :

headers: {
    "state": {
        type: "label",
        colorCode: [
            // if the value of the label cell is "equal" to "online" the color of the label will be "green"
            {
                "condition" : "equal",
                "value" : "online",
                "color" : "green"
            },

            // if the value of the label cell is "equal" to "online" the color of the label will be "green"
            {
                "condition" : "equal",
                "value" : "offline",
                "color" : "red"
            },
        ],
        color: "blue"
    }
}
5. button : renders a button that calls the function set by "callback" option when clicked. "data" option is false by default. takes the following options:

type : required, text : optional, render : optional, data : optional, color : optional, value. : optional

Example :

headers: {
    "delete": {
        type: "button",
        color: "red",
        value: "click to delete",
        callback: ()=>{console.log("deleting ... ")}
    }
}
6. html : renders html template with variables. "data" option is false by default. takes the following options:

type : required, text : optional, render : optional, data : optional, template : required

Example :

headers: {
    "card": {
        text: "Info card",
        type: "html",
        template: "<h1>Name: \${data['name']}</h1><h2>Age: \${data['age']}</h2>"
    }
}

and the variable's values can be set for each row by setting the the value of the column key to inside the "rows" parameter to an object. where each property of the object represents a variable.

For the previous example the rows parameter should be like this:

rows = [
    {
        "card" : {
            name: "Bill",
            age: "19"
        }
    }
]

Headers options:

type: is an optional property that represents the type of the cell which can be one of the Table headers types:. default value is "text".
text: is an optional property. used to set the text of the column header. if not set the column's key will be used.
filter: is an optional function. if set the a filter button will be added to the column head. when clicked the function will be called.
sort: is an optional property which specify whether to enable sorting by this column or not. default value is true.
data: determinate whether the column value is dynamic data or static for every row. if false the column is considered static and the "sort" and "filter" options are disabled by default. default value is true.
render: is an optional property which specify whether to render this column or not. default value is true.
value: is an optional property specific for the columns with static value like "button" to set the label of the button element. when not set the column text or key is used.
colorCode: is an optional property specific for the columns with the type of "label". which is an array that can be used to set the color of the label dynamically using a pre-defined conditions.

Example :

headers: {
    "state": {
        type: "label",
        colorCode: [
            // if the value of the label cell is "equal" to "online" the color of the label will be "green"
            {
                "condition" : "equal",
                "value" : "online",
                "color" : "green"
            },

            // if the value of the label cell is "equal" to "online" the color of the label will be "green"
            {
                "condition" : "equal",
                "value" : "offline",
                "color" : "red"
            },
        ],
        color: "blue"
    }
}
color: is an optional property specific for the columns with colors like "label" and "button". it's used to set the color statically when the "colorCode" is not set.
template: is an optional property specific for the columns with the type of "html". it represents a html template that gets rendered at the cells with the type "html". the template may contain variables using the following format \${data['variable_name']}.

Example :

headers: {
    "card": {
        text: "Info card",
        type: "html",
        template: "<h1>Name: \${data['name']}</h1><h2>Age: \${data['age']}</h2>"
    }
}

and the variable's values can be set for each row by setting the the value of the column key to inside the "rows" parameter to an object. where each property of the object represents a variable.

For the previous example the rows parameter should be like this:

rows = [
    {
        "card" : {
            name: "Bill",
            age: "19"
        }
    }
]

Fetching data from an API:

you can fetch the "rows" or "options" parameters from an API where the response should be the value of the parameter with the required structure shown above. to fetch the data from an API, set the parameter's value to an object representing an endpoint of an API, defined as following :

rows = {
    url: string|required,
    onsuccess: function|optional,
    onerror: function|optional,
}

onsuccess: called once the request is successful. takes the response content as a singular parameter. the function is expected to return the "rows" array. should be used when you need to process the response to fit the required structure of the "rows" parameter.

onerror: called once the request is not successful. takes the response object returned by the fetch request as a singular parameter.

for example:

rows = {
    url: "https://www.example.com/api/data"
    onsuccess: (data) => {console.log(data); return data}
    onerror: (response) => {console.error(`Fetching Data Failed. code: ${response.status}`)}
}

Updating the content of the table:

you can update the "rows" or "options" parameters using the Update method to render the same table with different data. the update function takes the same rows or options parameters from The Table initialization and can also be updated the same way by using json data or by Fetching data from an API

for example:

myTable.update(
    rows : [
        {id: 1, name: "Edward"},
        ...
    ]
    options : {
        uniqueID: "id",
        headers:{
            name: { text: "FullName"}
        }
    }
)

its not required to update both of the rows and options parameters. if only one of them is the updated the older version of the other onw will still be used. for example if you want to fetch the next page of rows you can update the rows only and the same options from The Table initialization will still be used to render the table.

for example:

myTable.update(
    rows : {
        url: "www.mydatasource.com/api?page=2"
    }
)

Retrieving the selected rows:

Selected rows can be retrieved by calling the "getSelectedRows" function which returns an array of the unique identifiers of the selected rows.

Accessing the class instance of a table initiated by attributes:

In the case of an existing table initiated by attributes where you have no access to the initiated class instance you can access the class instance using the property "tableInstance" of the table container node object. for example :

// if the html structure is like following
<html>
    <body>
        <div table-container>
            <table>....</table>
        </div>
    </body>
</html>

// the class instance will be :
const tableContainerNode = document.querySelector("[table-container]")
const myTableInstance = tableContainerNode.tableInstance 
const selectedRows = myTableInstance.getSelectedRows()