Issue
I'm trying to delete multiple items in my table by using a checkbox and a delete button. I've tried searching how to do this in different forums but can see the solution. This is my last resort.
This is my Ajax Code:
$('#delete').on('click', function(e){
// For selecting data in tables
var selected = []; // for getting the checkbox value
$('input[type="checkbox"]').each(function() {
if ($(this).get(0).checked) {
selected.push($(this).attr('value'));
}
});
var formData = new FormData();
formData.append("assetID", selected);
$.ajax({
url: "delete-asset",
type: "POST",
data: formData,
dataType: 'JSON',
enctype : 'multipart/form-data',
processData : false,
contentType : false,
success : function(data) {
if (data.status == 1) {
openAlertDialog("Success", data.message, "Continue","manage-assets");
} else {
openAlertDialog("Error", data.message, "Continue", "manage-assets");
}
},
error : function(data) {
openAlertDialog("Error", data.message, "Continue", "manage-assets");
},
});
});
This is my controller
@RequestMapping(value = "/delete-asset", method = RequestMethod.POST)
public @ResponseBody String deleteAsset(@ModelAttribute AssetCategory assetCategory) {
JsonObject result = new JsonObject();
assetService.deleteAssetByID(assetCategory.getAssetID());
result.addProperty("result", "Success");
result.addProperty("status", 1);
result.addProperty("message", "Asset Deleted!");
return "result.toString()";
}
Right now I'm trying to delete one item before proceeding to delete multiple items. I'm having an error 405 and can't proceed due to this problem.
Solution
If you just write this.value you should get the value of the checked item.
try this code to fetch all items.
var selectedItem = new Array();
$('input[name="Item"]:checked').each(function() {
selectedItem.push(this.value);
});
Before sending the data stored in array to controller, just try to alert the data to see if its working or not,
I hope this will work
Answered By - Nihal shetty
Answer Checked By - Pedro (JavaFixing Volunteer)