import { RowSelection } from "gridjs/plugins/selection";
export function initGridTable(column, data, paginate, url, permission) {
let selection = {
id: 'id',
name: 'Select',
plugin: {
component: RowSelection
}
};
column.push(selection);
if (permission.length !== 0) {
if (permission.includes("EDIT") || permission.includes("DELETE")) {
let actions = {
id: "id",
name: "Actions",
sort: false,
className: "justify-center",
formatter: (cell, row, id) =>
Gridjs.html(`
${
permission.includes("EDIT")
? `
`
: ""
}
${
permission.includes("DELETE")
? `
Are you sure
you want to delete this item ?
confirm
`
: ""
}
`),
};
column.push(actions);
}
}
return {
table: null,
config: {
columns: column,
data: data,
sort: true,
search: true,
pagination: {
enabled: true,
limit: paginate,
},
},
reloadTable() {
if (this.table) {
this.table.destroy();
}
this.table = new Gridjs.Grid(this.config).render(this.$root);
},
init() {
this.table = new Gridjs.Grid(this.config).render(this.$root);
},
deleteItem(url, cell) {
const csrfToken = document.querySelector(
'meta[name="csrf-token"]'
).content;
fetch(`${url}/${cell}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
"X-CSRF-TOKEN": csrfToken,
},
}).then((response) => {
// console.log(response);
this.$notification({
text: "Item deleted successfully",
variant: "success",
});
window.location.href = url;
})
.catch((error) => {
this.$notification({
text: error,
variant: "error",
});
});
},
editItem() {
this.$notification({ text: "Item edit action", variant: "info" });
},
};
}