Sunday, January 2, 2022

Load selected check boxes and Get select check boxs values | Javascript | Thymeleaf

<form class="form">
<div class="card-body">
<div class="mb-15">
<div class="table-responsive"
style="height: 400px; overflow-y : scroll;">
<table class="table table-head-custom table-head-bg table-borderless table-vertical-center">
<thead>
<th class="sm-width">Brand Image</th>
<th class="sm-width">Brand Name</th>
<th class="sm-width">Action</th>
</thead>
<tbody>
<tr th:if="${allAndSelectedBrands != null }"
th:id="${brand.id}"
th:each="brand,iterator : ${allAndSelectedBrands}">
<td><img th:src="${brand.imageUrl}" width="50">
</td>
<td><span th:text="${brand.name}"></span></td>
<td><input type="checkbox" th:checked="${brand.selected} ? 'checked'" th:value="${brand.id}" th:id="${brand.id}" name="brands"></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="card-footer">
<button type="button" onclick="saveBrands()" class="btn btn-primary mr-2">Save</button>
</div>
</form>
function saveBrands() {

var ids = [];
$.each($("input[name='brands']:checked"), function(){
ids.push($(this).val());
});
var id = '[' + ids.join(", ") + ']';
var mid = document.getElementById("mid").value;
console.log(id);
console.log(mid);
$.ajax({
type: "POST",
dataType: 'json',
url: saveBrandsUrl + '?mid='+ mid,
headers: {
"Authorization":authorizationToken,
"Content-Type":"application/json",
},
data: id,
success: function (status) {
alert('Success');
window.location.reload();
},
error: function(xhr, textStatus, error){
var data=xhr.responseText;
var jsonResponse = JSON.parse(data);
console.log(jsonResponse.message);
alert('Message: ' +jsonResponse.message);
},
})

}



No comments:

Post a Comment