It seems like you’re encountering issues with handling images in a Laravel + Blade setup while working on a product table in JavaScript. Specifically, you’re having problems with image editing in the modal, where either some images don’t display or, on editing the same recmultiple times, it tries to redirect you or applies changes from the previous record to the next. To address this, you might want to check your image handling logic in the modal edit form and ensure that it properly updates and displays the images for the specific record being edited. Additionally, make sure that you’re handling image references uniquely for each product and not inadvertently affecting other records in the process.
<div class="container">
@include('shop.modal')
<div class="card mt-4">
<div>
<button class="btn btn-primary" id="addBoxButton">dodaj box</button>
<button class="btn btn-primary" id="addProductButton">dodaj produkt</button>
</div>
<div>
<div>
<div class="col-12" id="tableItem">
<table id="boxTable" class="table">
<thead>
<tr>
<th>Purchase Date</th>
<th>Cost Price</th>
<th>Additional Costs</th>
<th>Quantity</th>
<th>Action</th>
</tr>
</thead>
<tbody></tbody>
</table>
<table id="productTable" class="table" style="max-width: 200px">
<thead>
<tr>
<th>name</th>
<th>purchase_date</th>
<th>cost_price_product</th>
<th>additional_costs_product</th>
<th>condition_product</th>
<th>color_product</th>
<th>dictionary_product</th>
<th>new_product</th>
<th>photo</th>
<th>photo_product</th>
<th>Action</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<button type="submit" class="btn btn-primary">Wyślij</button>
</div>
</div>
</div>
<script>
$(document).ready(function () {
var box = [];
var boxTables = $('#boxTable').DataTable({
dom: 'rt',
});
var product = [];
var productTables = $('#productTable').DataTable();
var photos = [];
var rowId;
$("#addBoxButton").click(function () {
$("#modal-add-box").modal("show");
});
$("#add-box").click(function () {
var newBox = {
purchase_date_box: $("#purchase_date_box").val(),
cost_price_box: $("#cost_price_box").val(),
additional_costs_box: $("#additional_costs_box").val(),
Quantity_box: $("#Quantity_box").val(),
};
box.push(newBox);
boxTables.row.add([
newBox.purchase_date_box,
newBox.cost_price_box,
newBox.additional_costs_box,
newBox.Quantity_box,
'<div style="display: flex;"><button class="btn btn-success editBoxButton">Edytuj</button> <button class="btn btn-danger deleteBoxButton">Usuń</button></div>'
]).node().setAttribute('data-id', box.length - 1);
boxTables.draw();
$("#modal-add-box").modal("hide");
$("#purchase_date_box").val("");
$("#cost_price_box").val("");
$("#additional_costs_box").val("");
$("#Quantity_box").val("");
});
$('#boxTable').on('click', '.deleteBoxButton', function () {
rowId = $(this).closest('tr').data('id');
boxTables.row($(this).closest('tr')).remove().draw(false);
box.splice(rowId, 1);
rowId = null;
});
$('#boxTable').on('click', '.editBoxButton', function () {
rowId = $(this).closest('tr').data('id');
var selectedBox = box[rowId];
$("#purchase_date_box_edit").val(selectedBox.purchase_date_box);
$("#cost_price_box_edit").val(selectedBox.cost_price_box);
$("#additional_costs_box_edit").val(selectedBox.additional_costs_box);
$("#Quantity_box_edit").val(selectedBox.Quantity_box);
$("#modal-edit-box").modal("show");
});
$("#edit-box").click(function () {
var $this = $(this);
var editedBox = {
purchase_date_box: $("#purchase_date_box_edit").val(),
cost_price_box: $("#cost_price_box_edit").val(),
additional_costs_box: $("#additional_costs_box_edit").val(),
Quantity_box: $("#Quantity_box_edit").val(),
};
box[rowId] = editedBox;
boxTables.row(rowId).data([
editedBox.purchase_date_box,
editedBox.cost_price_box,
editedBox.additional_costs_box,
editedBox.Quantity_box,
'<div style="display: flex;"><button class="btn btn-success editBoxButton">Edytuj</button> <button class="btn btn-danger deleteBoxButton">Usuń</button></div>'
]).draw();
$("#modal-edit-box").modal("hide");
});
$("#addProductButton").click(function () {
$("#modal-add-product").modal("show");
});
$("#add-product").click(function () {
var newProduct = {
product_name: $("#name_product").val(),
product_purchase_date: $("#purchase_date_product").val(),
product_cost_price: $("#cost_price_product").val(),
product_additional_costs: $("#additional_costs_product").val(),
product_condition: $("#condition_product").val(),
product_color: $("#color_product").val(),
product_dictionary: $("#dictionary_product").val(),
product_new: $("#new_product").val(),
photos: []
};
const photoContainer = $('#thumbnail-container .thumbnail img');
const photos = [];
photoContainer.each(function () {
const originalSrc = $(this).data('original-src');
photos.push(originalSrc);
});
newProduct.photos = photos;
const photoStatus = photos.length > 0 ? "dodane " : "nie dodano";
product.push(newProduct);
productTables.row.add([
newProduct.product_name,
newProduct.product_purchase_date,
newProduct.product_cost_price,
newProduct.product_additional_costs,
newProduct.product_condition,
newProduct.product_color,
newProduct.product_dictionary,
newProduct.product_new,
newProduct.photos,
photoStatus,
'<div style="display: flex;"><button class="btn btn-success editProductButton">Edytuj</button> <button class="btn btn-danger deleteProductButton">Usuń</button></div>'
]).node().setAttribute('data-id', product.length - 1);
productTables.columns([8]).visible(false);
productTables.draw();
$("#modal-add-product").modal("hide");
$("#name_product").val("");
$("#purchase_date_product").val("");
$("#cost_price_product").val("");
$("#additional_costs_product").val("");
$("#condition_product").val("");
$("#color_product").val("");
$("#dictionary_product").val("");
$("#new_product").val("");
$('#thumbnail-container .thumbnail').remove();
});
$('#productTable').on('click', '.deleteProductButton', function () {
rowId = $(this).closest('tr').data('id');
productTables.row($(this).closest('tr')).remove().draw(false);
product.splice(rowId, 1);
rowId = null;
});
$('#productTable tbody').on('click', '.editProductButton', function (event) {
event.preventDefault();
rowId = $(this).closest('tr').data('id');
var selectedProduct = product[rowId];
$("#name_product_edit").val(selectedProduct.product_name);
$("#purchase_date_product_edit").val(selectedProduct.product_purchase_date);
$("#cost_price_product_edit").val(selectedProduct.product_cost_price);
$("#additional_costs_product_edit").val(selectedProduct.product_additional_costs);
$("#condition_product_edit").val(selectedProduct.product_condition);
$("#color_product_edit").val(selectedProduct.product_color);
$("#dictionary_product_edit").val(selectedProduct.product_dictionary);
$("#new_product_edit").val(selectedProduct.product_new);
photos = selectedProduct.photos;
$("#modal-edit-product").modal("show");
});
$("#edit-product").click(function () {
var $this = $(this);
var editedProduct = {
product_name: $("#name_product_edit").val(),
product_purchase_date: $("#purchase_date_product_edit").val(),
product_cost_price: $("#cost_price_product_edit").val(),
product_additional_costs: $("#additional_costs_product_edit").val(),
product_condition: $("#condition_product_edit").val(),
product_color: $("#color_product_edit").val(),
product_dictionary: $("#dictionary_product_edit").val(),
product_new: $("#new_product_edit").val(),
photos: []
};
const photoContainer = $('#thumbnail-container-edit .thumbnail img');
photoContainer.each(function () {
const originalSrc = $(this).data('original-src');
photos.push(originalSrc);
});
editedProduct.photos = photos;
const photoStatus = photos.length > 0 ? "dodane " : "nie dodano";
product[rowId] = editedProduct;
productTables.row(rowId).data([
editedProduct.product_name,
editedProduct.product_purchase_date,
editedProduct.product_cost_price,
editedProduct.product_additional_costs,
editedProduct.product_condition,
editedProduct.product_color,
editedProduct.product_dictionary,
editedProduct.product_new,
editedProduct.photos,
photoStatus,
'<div style="display: flex;"><button class="btn btn-success editProductButton">Edytuj</button> <button class="btn btn-danger deleteProductButton">Usuń</button></div>'
]).node().setAttribute('data-id', product.length - 1);
productTables.columns([8]).visible(false);
productTables.draw();
photos = [];
$("#modal-edit-product").modal("hide");
});
$('#thumbnail-container-edit').on('click', '.thumbnail img', function () {
const originalSrc = $(this).data('original-src');
displayLargeImage(originalSrc);
});
$('#thumbnail-container-edit').on('click', '.delete-button', function () {
$(this).parent().remove();
});
$('#photo_product_edit').on('change', function () {
displayThumbnailsEdit(this);
$(this).val('');
});
$('#photo_product').on('change', function () {
displayThumbnails(this);
$(this).val('');
});
$('#thumbnail-container').on('click', '.thumbnail img', function () {
const originalSrc = $(this).data('original-src');
displayLargeImage(originalSrc);
});
$('#thumbnail-container').on('click', '.delete-button', function () {
$(this).parent().remove();
});
$('body').on('click', '#largeImageModal .btn-secondary', function () {
$('#largeImageModal').modal('hide');
});
function displayLargeImage(src) {
const modalContent = `
<div class="modal fade" id="largeImageModal" tabindex="-1" role="dialog" aria-labelledby="largeImageModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-body">
<img src="${src}" class="img-fluid" alt="Large Image">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Zamknij</button>
</div>
</div>
</div>
</div>`;
$('#largeImageModal').remove();
$('body').append(modalContent);
$('#largeImageModal').modal('show');
}
function displayThumbnails(input) {
if (input.files && input.files.length > 0) {
const thumbnailContainer = $('#thumbnail-container');
const maxThumbnailSize = 50;
let thumbnailsWrapper = thumbnailContainer.find('.thumbnails-wrapper');
if (!thumbnailsWrapper.length) {
thumbnailsWrapper = $('<div class="thumbnails-wrapper"></div>');
}
for (let i = 0; i < input.files.length; i++) {
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.src = e.target.result;
img.onload = function () {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
let width = img.width;
let height = img.height;
if (width > height) {
if (width > maxThumbnailSize) {
height *= maxThumbnailSize / width;
width = maxThumbnailSize;
}
} else {
if (height > maxThumbnailSize) {
width *= maxThumbnailSize / height;
height = maxThumbnailSize;
}
}
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
const thumbnail = `
<div class="thumbnail">
<img src="${canvas.toDataURL('image/jpeg')}" data-original-src="${e.target.result}" alt="Thumbnail">
<button class="delete-button">X</button>
</div>`;
thumbnailsWrapper.append(thumbnail);
};
};
reader.readAsDataURL(input.files[i]);
}
thumbnailContainer.empty().append(thumbnailsWrapper);
}
}
function displayThumbnailsEdit(input) {
if (input.files && input.files.length > 0) {
const thumbnailContainer = $('#thumbnail-container-edit');
const maxThumbnailSize = 50;
thumbnailContainer.empty();
for (let i = 0; i < photos.length; i++) {
const thumbnail = `
<div class="thumbnail small-thumbnail">
<img src="${photos[i]}" alt="Thumbnail">
<button class="delete-button">X</button>
</div>`;
thumbnailContainer.append(thumbnail);
for (let i = 0; i < input.files.length; i++) {
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.src = e.target.result;
img.onload = function () {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
let width = img.width;
let height = img.height;
if (width > height) {
if (width > maxThumbnailSize) {
height *= maxThumbnailSize / width;
width = maxThumbnailSize;
}
} else {
if (height > maxThumbnailSize) {
width *= maxThumbnailSize / height;
height = maxThumbnailSize;
}
}
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
photos.push(canvas.toDataURL('image/jpeg'));
const thumbnail = `
<div class="thumbnail">
<img src="${canvas.toDataURL('image/jpeg')}" data-original-src="${e.target.result}" alt="Thumbnail">
<button class="delete-button">X</button>
</div>`;
thumbnailContainer.append(thumbnail);
};
};
reader.readAsDataURL(input.files[i]);
}
}
}
$('form').submit(function(event) {
event.preventDefault();
var boxData = boxTables.rows().data().toArray();
var productData = productTables.rows().data().toArray();
var formData = {
boxData: boxData,
productData: productData
};
$.ajax({
url: '/shop',
type: 'POST',
data: { _token: '{{ csrf_token() }}', formData: formData },
success: function(response) {
console.log('Wysłane')
console.log(response.aa);
},
error: function(error) {
console.log('nie wyslane')
// console.error(error);
}
});
});
});
</script>
And modal
<div class="modal" tabindex="-1" id="modal-add-product">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Dodaj produkt</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body after-ajax d-flex flex-column justify-content-center align-items-center">
<div class="form-group col-6 text-center mt-1">
<label for="name_product"><span class="text-danger">*</span>name:</label>
<input type="text" name="name_product" id="name_product" class="form-control" required>
</div>
<div class="form-group col-6 text-center mt-2">
<label for="purchase_date_product"><span class="text-danger">*</span>purchase_date:</label>
<input type="date" name="purchase_date_product" id="purchase_date_product" class="form-control" required>
</div>
<div class="form-group col-6 text-center mt-2">
<label for="cost_price_product"><span class="text-danger">*</span>cost_price_product:</label>
<input type="number" name="cost_price_product" id="cost_price_product" class="form-control" required>
</div>
<div class="form-group col-6 text-center mt-2">
<label for="additional_costs_product">additional_costs_product:</label>
<input type="number" name="additional_costs_product" id="additional_costs_product" class="form-control">
</div>
<div class="form-group col-6 text-center mt-2">
<label for="condition_product"><span class="text-danger">*</span>condition_product:</label>
<select name="condition_product" id="condition_product" class="form-control" required>
<option value="1">Excellent</option>
<option value="2">Good</option>
<option value="3">Fair</option>
</select>
</div>
<div class="form-group col-6 text-center mt-2">
<label for="color_product">color_product:</label>
<input type="text" name="color_product" id="color_product" class="form-control">
</div>
<div class="form-group col-6 text-center mt-2">
<label for="dictionary_product">dictionary_product:</label>
<input type="text" name="dictionary_product" id="dictionary_product" class="form-control">
</div>
<div class="form-group col-6 text-center mt-2">
<label class="form-check-label" for="new_product">new_product</label>
<input type="checkbox" name="new_product" id="new_product" class="form-check-input">
</div>
<div class="form-group col-6 text-center mt-2">
<label for="photo_product">photo_product:</label>
<input type="file" name="photo_product" id="photo_product" class="form-control" multiple accept="image/*">
</div>
<div id="thumbnail-container"></div>
</div>
<div class="modal-footer after-ajax">
<span class="btn btn-secondary" data-bs-dismiss="modal">anuluj</span>
<span type="submit" id="add-product" class="btn btn-primary">dodaj</span>
</div>
</div>
</div>
</div>
<div class="modal" tabindex="-1" id="modal-edit-product">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edytuj produkt</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body after-ajax d-flex flex-column justify-content-center align-items-center">
<div class="form-group col-6 text-center mt-1">
<label for="name_product_edit"><span class="text-danger">*</span>name:</label>
<input type="text" name="name_product_edit" id="name_product_edit" class="form-control" required>
</div>
<div class="form-group col-6 text-center mt-2">
<label for="purchase_date_product_edit"><span class="text-danger">*</span>purchase_date:</label>
<input type="date" name="purchase_date_product_edit" id="purchase_date_product_edit" class="form-control" required>
</div>
<div class="form-group col-6 text-center mt-2">
<label for="cost_price_product_edit"><span class="text-danger">*</span>cost_price_product:</label>
<input type="number" name="cost_price_product_edit" id="cost_price_product_edit" class="form-control" required>
</div>
<div class="form-group col-6 text-center mt-2">
<label for="additional_costs_product_edit">additional_costs_product:</label>
<input type="number" name="additional_costs_product_edit" id="additional_costs_product_edit" class="form-control">
</div>
<div class="form-group col-6 text-center mt-2">
<label for="condition_product_edit"><span class="text-danger">*</span>condition_product:</label>
<select name="condition_product_edit" id="condition_product_edit" class="form-control" required>
<option value="1">Excellent</option>
<option value="2">Good</option>
<option value="3">Fair</option>
</select>
</div>
<div class="form-group col-6 text-center mt-2">
<label for="color_product_edit">color_product:</label>
<input type="text" name="color_product_edit" id="color_product_edit" class="form-control">
</div>
<div class="form-group col-6 text-center mt-2">
<label for="dictionary_product_edit">dictionary_product:</label>
<input type="text" name="dictionary_product_edit" id="dictionary_product_edit" class="form-control">
</div>
<div class="form-group col-6 text-center mt-2">
<label class="form-check-label" for="new_product_edit">new_product</label>
<input type="checkbox" name="new_product_edit" id="new_product_edit" class="form-check-input">
</div>
<div class="form-group col-6 text-center mt-2">
<label for="photo_product_edit">photo_product:</label>
<input type="file" name="photo_product_edit" id="photo_product_edit" class="form-control" multiple accept="image/*">
</div>
<div id="thumbnail-container-edit"></div>
</div>
<div class="modal-footer after-ajax">
<span class="btn btn-secondary" data-bs-dismiss="modal">anuluj</span>
<span id="edit-product" class="btn btn-primary">edytuj</span>
</div>
</div>
</div>
</div>
Certainly, I can help you optimize your code for brevity and efficiency. If you could provide a snippet of the relevant code, especially the parts where you’re experiencing issues or believe it can be improved, I’ll do my best to offer suggestions for simplification and optimization.
Certainly! Please share the specific part of your code that you think is too extensive or where you’re encountering issues. I’ll help you streamline it.


