A draggable Bootstrap grid that will save the order for future usage. The drag feature is implemented with gridstrap.js.
The concept being on page load get the grid order from the database via an Ajax GET call and then on dragging (interacting with #basic-grid) the new grid order is sent to the database via an Ajax POST.
For persistence, MySQL is used with PHP. See the code at the bottom of this page.
The javascript used is stock standard and seen here before, except for this function:
function moveElement(move, toBeBefore) {
$(move).insertBefore(toBeBefore);
}
This moves an element before another stated element. It is this function that is used on page load to move the grid around to the saved order. This is done before gridstrap.js is initialized with:
function gridDragInit() {
$("#basic-grid").gridstrap();
}
The full functions:
$(document).ready(function () {
getOrder();
});
function moveElement(move, toBeBefore) {
$(move).insertBefore(toBeBefore);
}
function getOrder() {
$.ajax({
type: "GET",
url: "https://peachpuff.srv3r.com/api/index.php",
data: { drag_grid: "" },
success: function (data) {
//console.log(data[0]["element"]);
moveElement(data[2]["element"], data[3]["element"]); //3 before 4
moveElement(data[1]["element"], data[2]["element"]); //2 before 3
moveElement(data[0]["element"], data[1]["element"]); //1 before 2
setTimeout(gridDragInit(), 2);
}
});
}
function gridDragInit() {
$("#basic-grid").gridstrap();
}
function updateOrder(number, element) {
$.ajax({
type: "POST",
url: "https://peachpuff.srv3r.com/api/index.php",
data: { drag_grid: "", number: number, element: element },
success: function (result) {
console.log(result);
}
});
}
function currentOrder() {
var listItems = document.querySelector("#basic-grid").children;
var listArray = Array.from(listItems);
var counter = 0;
listArray.forEach((item) => {
counter++;
if (item.id.includes("col")) {
updateOrder(counter, "#" + item.id);
//console.log(item.id + " " + counter);
}
});
}