simple-datatables

Upgrading

From 8.0.x to 9.0.x

const dt = new DataTable(myTable, {
    data: {
        headings: ["Name"],
        data: [["John Doe"]],
    },
});

const names = dt.data.data.map((row) => row[0].text);

Do:

new DataTable(myTable, {
    data: {
        headings: ["Name"],
        data: [
            {
                cells: ["John Doe"],

                // It is now possible to add custom attributes to the row, with:
                // attributes: {
                //     class: "my-row",
                //     style: "background-color: pink"
                // },
            },
        ],
    },
});

const names = dt.data.data.map((row) => row.cells[0].text);
rowRender: (row, tr, index) => {
    // old, in 8.0.x:
    const firstCell = row[0];

    // new, in 9.0.x:
    const firstCell = row.cells[0];
    row.attributes.class = "my-row";
};

From 7.1.x to 8.0.x

From 7.0.x to 7.1.x

From 6.0.x to 7.0.x

html has to be used for text that can contain HTML tags that should be rendered, whereas string will escape all tags. The type is used to determine how to order items in the column when sorting. Setting the type on the table’s configuration will set a default value.

From 5.0.x to 6.0.x:

Version 6.0 is the biggest update to simple-datatables since version 1.0. I do not expect similar changes in the next few years.

Instead of:

DataTable("#table", {
    data,
    perPage: 25,
    columns: [
        {
            select: 1,
            render: (data, cell, row) =>
                data +
                "<button data-row='" +
                row.dataIndex +
                "'>Buy Now</button>",
        },
    ],
});

Do now:

DataTable("#table", {
    data,
    perPage: 25,
    columns: [
        {
            select: 1,
            render: (data, td, dataIndex, cellIndex) =>
                data + "<button data-row='" + dataIndex + "'>Buy Now</button>",
        },
    ],
});

And instead of:

new DataTable("table", {
    data: {
        headings: ["Checked"].concat(Object.keys(data[0])),
        data: data.map((item) => [false].concat(Object.values(item))),
    },
    columns: [
        {
            select: 0,
            render: (value) =>
                `<input type="checkbox" ${value === "true" ? "checked" : ""}>`,
        },
        {
            select: 1,
            render: (value, td, tr) => {
                tr.dataset.name = value;
                return value;
            },
        },
    ],
});

Do now:

new DataTable("table", {
    data: {
        headings: ["Checked"].concat(Object.keys(data[0])),
        data: data.map((item) => [false].concat(Object.values(item))),
    },
    rowRender: (rowValue, tr, _index) => {
        if (!tr.attributes) {
            tr.attributes = {};
        }
        tr.attributes["data-name"] = rowValue[1].data;
        return tr;
    },
    columns: [
        {
            select: 0,
            render: (value, _td, _rowIndex, _cellIndex) =>
                `<input type="checkbox" ${value === "true" ? "checked" : ""}>`,
        },
    ],
});

Instead of:

document.querySelector(".dataTable-wrapper");

do now:

document.querySelector(".datatable-wrapper");

Note: Class names are now configurable. So if you rely on the old class names for some reason, you can configure simple-datatables to use the old class names.

Instead of:

const tr = document.querySelector("tr");
const index = tr.dataIndex;
const row = dataTable.data[index];

do now:

const tr = document.querySelector("tr");
const index = parseInt(tr.dataset.index);
const row = dataTable.data.data[index];

Instead of:

dataTable.sortColumn(...)

do now:

dataTable.column.sort(...)

Instead of:

new DataTable(t, {
    data,
    filters: { Job: ["Assistant", "Manager"] },
    columns: [
        {
            select: 4,
            type: "date",
            format: "MM/DD/YYYY",
        },
    ],
});

do now:

new DataTable(t, {
    data,
    columns: [
        {
            select: 1,
            filter: ["Assistant", "Manager"],
        },
        {
            select: 4,
            type: "date",
            format: "MM/DD/YYYY",
        },
    ],
});

Instead of:

...
let newRows = [
    ["column1", "column2", "column3", "column4", ...],
    ["column1", "column2", "column3", "column4", ...],
    ["column1", "column2", "column3", "column4", ...],
    ["column1", "column2", "column3", "column4", ...],
    ...
];
dataTable.rows.add(newRows);

do now:

...
let newRows = [
    ["column1", "column2", "column3", "column4", ...],
    ["column1", "column2", "column3", "column4", ...],
    ["column1", "column2", "column3", "column4", ...],
    ["column1", "column2", "column3", "column4", ...],
    ...
];
dataTable.insert({data: newRows});

Instead of:

dataTable.init(newOptions);

do now:

datatable.destroy();
datatable = new simpleDatatables.DataTable("#tablename", newOptions);

And instead of:

dataTable.init({ data: newHeadingsAndData });

do now:

// Reset table headings data
dataTable.data = {
    data: [],
    headings: [],
};
dataTable.hasHeadings = false;
dataTable.hasRows = false;
dataTable.insert(newHeadingsAndData);

From 4.0.x to 5.0:

Instead of:

import { DataTable } from "simple-datatables";
const dataTable = new DataTable("#myTable");
dataTable.table.focus();

do now:

import { DataTable } from "simple-datatables";
const dataTable = new DataTable("#myTable");
dataTable.dom.focus();

Instead of:

import { DataTable } from "simple-datatables";
const dataTable = new DataTable("#myTable");
dataTable.export({ type: "csv", download: true, columnDelimiter: ";" });

do now:

import {
    DataTable,
    exportCSV, // or exportJSON, exportSQL
} from "simple-datatables";
const dataTable = new DataTable("#myTable");
exportCSV(dataTable, { download: true, columnDelimiter: ";" });

Instead of:

import { DataTable } from "simple-datatables";
const dataTable = new DataTable("#myTable");
dataTable.import({ type: "csv", columnDelimiter: ";", data: "..." });

do now:

import {
    DataTable,
    convertCSV, // or convertJSON
} from "simple-datatables";
const dataTable = new DataTable("#myTable");
const convertedData = convertCSV({ columnDelimiter: ";", data: "..." });
dataTable.insert(convertedData);
import { DataTable } from "simple-datatables";
const dataTable = new DataTable("#myTable");
dataTable.on("datatable.selectrow", function ({ row, event }) {
    event.preventDefault();
    row.classList.add("selected");
});

Do now:

import { DataTable } from "simple-datatables";
const dataTable = new DataTable("#myTable");
dataTable.on("datatable.selectrow", function (row, event) {
    event.preventDefault();
    row.classList.add("selected");
});

From 3.x to 4.0: