I tried checking whether the image file on the array was on the server and then displayed the image.
array in the database
images in file explorer
// Edit button
<button class="btnEdit" data-toggle="modal" data-target="#modalEdit" data-images="{{ $product->images }}">Edit</button>
// Edit Product
$('#products-table').on('click', '.btnEdit', function() {
var imageArray = $(this).data('images');
var baseUrl = window.location.origin;
$(imageArray).each(function () {
var imageUrl = baseUrl + '/img/product-img/' + this;
// check to the server
$.get(imageUrl).done( function() {
// show images
$('#modalEdit #btnUploadImage').before(`
<div class="image-box" data-modal-id="#modalEdit" data-original-background="` + imageUrl + `" style="background-image: url('` + imageUrl + `');">
<div class="actions">
<a class="btnSetThumbnail">
<i class="mdi mdi-radiobox-blank"></i>
</a>
<a class="btnEditImage"data-toggle="modal" data-target="#modalEditImage">
<i class="mdi mdi-pencil-outline"></i>
</a>
<a class="btnRemoveImage">
<i class="mdi mdi-delete-outline"></i>
</a>
</div>
</div>
`);
});
});
});
I have tried several times, some have appeared in the right order,
and some have appeared in the wrong order.
if I delete this code $.get(imageUrl).done();
, the image appears in the correct order, but I have to check the file.
I also have the same code for the product view function, it works well, all images appear in the right order, how do I fix this?
// Show Product
$('#products-table').on('click', '.btnShow', function() {
var imageBox = $('#modalShow .image-box-static');
var imageArray = $(this).data('images');
var baseUrl = window.location.origin;
$(imageArray).each(function (i) {
var imageUrl = baseUrl + '/img/product-img/' + this;
$.get(imageUrl).done( function() {
$(imageBox[i]).css('background-image', 'url(' + this.url + ')');
$(imageBox[i]).show();
}).fail( function() {
$(imageBox[i]).css('background-image', 'url(' + baseUrl + '/img/product-img/image-not-found.png)');
$(imageBox[i]).show();
});
});
});