scandiweb-products/static/index.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-07-27 06:59:59 -04:00
const loadItems = () => {
2022-07-28 08:16:55 -04:00
$.ajax(
'product',
{
success: data => {
const boxes = data.map(product =>
`<div class="product">
<input type="checkbox" class="delete-checkbox" value="${product.id}">
<p>
${product.sku}<br>
${product.name}<br>
${product.price} $<br>
${product.attribute}
</p>
</div>`
);
$('#products').html(boxes.join('\n'));
},
error: jqXHR => alert(jqXHR.responseText),
}
)
2022-07-27 06:59:59 -04:00
}
2022-07-27 11:44:08 -04:00
2022-07-28 08:16:55 -04:00
loadItems();
2022-07-27 11:44:08 -04:00
2022-07-28 08:16:55 -04:00
$('#delete-product-btn').on('click', () => {
let values = [];
const checkboxes = document.querySelectorAll('input[class="delete-checkbox"]:checked');
checkboxes.forEach(checkbox => values.push(checkbox.value));
2022-07-29 05:32:45 -04:00
if(values.length) {
$.ajax(
`product?id=${values.join(',')}`,
{
method: 'DELETE',
success: loadItems,
error: jqXHR => alert(jqXHR.responseText),
}
)
} else {
alert('Please select a product.');
}
2022-07-28 08:16:55 -04:00
});