document.addEventListener('DOMContentLoaded', () => {
const columns = [
{ key: 'id', label: 'ID', type: 'number', sortable: true, visible: true },
{ key: 'image', label: 'Avatar', type: 'image', width: 50, height: 50, borderRadius: 60, sortable: false,
visible: true },
{ key: 'tree', label: 'Tree Structure', type: 'tree', sortable: false, visible: true },
{ key: 'name', label: 'Name', type: 'string', sortable: true, columnSearch: true, visible: true, editable:
true },
{ key: 'age', label: 'Age', type: 'number', sortable: true, columnSearch: true, visible: true },
{ key: 'city', label: 'City', type: 'string', sortable: false, visible: true },
{ key: 'birthdate', label: 'Birth Date', type: 'date', sortable: true, visible: true },
{
key: 'status',
label: 'Status',
type: 'string',
component: 'badge',
align: 'center',
mapping: {
active: 'success',
inactive: 'danger'
},
sortable: true,
visible: true
},
{
key: 'deliverystatus',
label: 'Delivery Status',
type: 'string',
align: 'center',
mapping: {
ondelivery: 'ondelivery',
delivery: 'delivery'
},
sortable: true,
visible: true
},
{ key: 'actions', label: 'Actions', align: 'center', sortable: false, visible: true }
];
const data = [
{
id: 1,
image: 'https://placehold.co/400',
name: 'John Doe',
age: 28,
city: 'New York',
status: "active",
deliverystatus: "delivery",
birthdate: "2021-9-24",
tree: {
child1: {
id: 11,
name: 'John Child 1',
age: 25,
city: 'Brooklyn',
status: "active",
deliverystatus: "ondelivery",
birthdate: "2024-1-15",
tree: {
grandchild1: {
id: 111,
name: 'John Grandchild 1',
age: 22,
city: 'Manhattan',
status: "inactive",
deliverystatus: "delivery",
birthdate: "2027-3-10"
}
}
},
child2: {
id: 12,
name: 'John Child 2',
age: 30,
city: 'Queens',
status: "active",
deliverystatus: "delivery",
birthdate: "2020-6-20"
}
}
},
{
id: 2,
image: 'https://placehold.co/400',
name: 'Jane Smith',
age: 35,
city: 'Los Angeles',
status: "inactive",
deliverystatus: "ondelivery",
birthdate: "2019-12-5",
tree: {
child1: {
id: 21,
name: 'Jane Child 1',
age: 18,
city: 'Hollywood',
status: "active",
deliverystatus: "delivery",
birthdate: "2026-8-15"
}
}
},
{
id: 3,
image: 'https://placehold.co/400',
name: 'Bob Johnson',
age: 42,
city: 'Chicago',
status: "active",
deliverystatus: "delivery",
birthdate: "2018-4-12",
// No tree property - this will be a leaf node
},
{
id: 4,
image: 'https://placehold.co/400',
name: 'Bob Johnson',
age: 42,
city: 'Chicago',
status: "active",
deliverystatus: "delivery",
birthdate: "2018-4-12"
// No tree property - this will be a leaf node
},
{ id: 1, image: 'https://placehold.co/400', name: 'John', age: 28, city: 'New York', status: "active",
deliverystatus: "delivery", birthdate: "2021-9-24" },
{ id: 1, image: 'https://placehold.co/400', name: 'John', age: 28, city: 'New York', status: "active",
deliverystatus: "delivery", birthdate: "2021-9-24" },
{ id: 2, image: 'https://placehold.co/400', name: 'Alice', age: 24, city: 'Los Angeles', status: "inactive",
deliverystatus: "ondelivery", birthdate: "2021-7-24" },
{ id: 1, image: 'https://placehold.co/400', name: 'John', age: 28, city: 'New York', status: "active",
deliverystatus: "delivery", birthdate: "2021-9-24" },
{ id: 1, image: 'https://placehold.co/400', name: 'John', age: 28, city: 'New York', status: "active",
deliverystatus: "delivery", birthdate: "2021-9-24" },
{ id: 2, image: 'https://placehold.co/400', name: 'Alice', age: 24, city: 'Los Angeles', status: "inactive",
deliverystatus: "ondelivery", birthdate: "2021-7-24" },
{ id: 1, image: 'https://placehold.co/400', name: 'John', age: 28, city: 'New York', status: "active",
deliverystatus: "delivery", birthdate: "2021-9-24" },
{ id: 1, image: 'https://placehold.co/400', name: 'John', age: 28, city: 'New York', status: "active",
deliverystatus: "delivery", birthdate: "2021-9-24" },
{ id: 2, image: 'https://placehold.co/400', name: 'Alice', age: 24, city: 'Los Angeles', status: "inactive",
deliverystatus: "ondelivery", birthdate: "2021-7-24" },
{ id: 1, image: 'https://placehold.co/400', name: 'John', age: 28, city: 'New York', status: "active",
deliverystatus: "delivery", birthdate: "2021-9-24" },
{ id: 1, image: 'https://placehold.co/400', name: 'John', age: 28, city: 'New York', status: "active",
deliverystatus: "delivery", birthdate: "2021-9-24" },
{ id: 2, image: 'https://placehold.co/400', name: 'Alice', age: 24, city: 'Los Angeles', status: "inactive",
deliverystatus: "ondelivery", birthdate: "2021-7-24" },
];
const menuData = [
{
label: 'Edit',
icon: '',
labelWithIcon: true,
class: 'btn btn--ghost d-flex align-items-center',
onClick: (rowId) => {
alert(`Edit clicked for row ID: ${rowId}`);
}
},
{
label: 'View',
icon: '',
class: 'btn btn--ghost',
onClick: (rowId) => {
alert("we need to make sure this workds.")
}
},
{
label: 'Delete',
icon: '',
class: 'btn btn--ghost',
attributes: [{
key: 'data-modal-target',
value: 'default-modal'
}],
onClick: (rowId) => {
event.preventDefault();
}
},
]
$('#dataTable').DataTable({
search: true,
print: true,
data,
columns,
actionMenus: menuData,
primaryAction: {
label: 'Add New Data',
icon: '+',
class: 'btn btn-pumpkin',
onClick: () => {
alert("test test");
}
}
});
});