/home/mip/mip/app/Modules/Client/Views/product/includes/js-modal-category.blade.php
<!-- Brand js -->
<script type="text/javascript">
var category_datatable;
$( document ).ready( function() {
$('#addbutton_category').click( function() {
var name = $('#category_name').val();
if(name=='') {
swal("Error!", "Name is required!", "error");
$('#category_name').focus();
return false;
} else {
$("#hide_onclick_modal").hide();
$("#show_onclick_modal").show();
$.ajax({
type: "post",
url: "/client/modals/category/store",
data: {
name: $("#category_name").val()
},
success: function(result) {
var table = $('#categoryTable').DataTable();
table.ajax.reload();
$("#hide_onclick_modal").show();
$("#show_onclick_modal").hide();
$('#selectCategory').append($('<option>', {
value: result.id,
text: result.name,
selected: true
}));
$('#categoryModal').modal('hide');
},
error :function( xhr ) {
if( xhr.status === 422 ) {
var errors = JSON.parse(xhr.responseText);
alert(errors['name'][0]);
} else {
swal("Error!", "Error occured please try again.", "error");
}
$("#hide_onclick_modal").show();
$("#show_onclick_modal").hide();
}
});
}
});
});
function initializeCategory()
{
category_datatable = 1;
table = $('#categoryTable').DataTable({
"processing": true,
"serverSide": true,
ajax: {
url:'/client/modals/category/get-category-data',
type: 'POST',
},
"bAutoWidth": false,
"iDisplayLength": 10,
columns: [
{data: 'name', name: 'name', className:'editable'},
{data: 'action', name: 'action', orderable:false, searchable:false, width:'60px', className:'text-center'},
],
fnDrawCallback: function ( oSettings ) {
$('#categoryTable tbody td.editable').editable('/client/modals/category/update', {
callback: function( sValue, y ) {
table.draw();
},
onsubmit: function(settings, original) {
if ((original.revert == $('input',this).val()) || $('input',this).val() == "") {
original.reset();
$(original).parents('tr').find('td.hide_me').show();
return false;
}
},
submitdata: function (value, settings) {
var id = $(this).parents('tr').find('a.btn-edit').data('id');
return {
'id':id
};
},
onerror: function(settings, original, xhr) {
if( xhr.status === 422 ) {
var errors = JSON.parse(xhr.responseText);
alert(errors['name'][0]);
original.reset();
$(original).parents('tr').find('td.hide_me').show();
} else {
swal("Error!", "Error occured please try again.", "error");
}
},
onblur:'submit',
event:'custom_event',
height:"30px",
width:'100%',
name : 'name'
});
$("#categoryTable tbody td").on("click", 'a.btn-edit', function() {
$(this).parents('tr').find('td.editable').trigger("custom_event");
$(this).parent('td').addClass('hide_me').hide();
});
$("#categoryTable tbody td").on("click", 'a.btn-delete', function() {
var action = $(this).data('action');
swal({
title: 'Are you sure you want to delete?',
text: "This will be permanently deleted",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes'
})
.then(function () {
$.ajax({
type: "GET",
url: action,
dataType: 'json',
success: function(data) {
if(data.success == "yes") {
table.draw();
swal("Deleted!", "Category successfuly deleted!", "success");
$("#selectCategory option[value='"+data.id+"']").remove();
}else{
swal("Error!", "Cannot delete it is being used in database.", "error");
}
},
error :function( jqXhr ) {
swal('Unable to delete.', 'Please try again.', 'error')
}
});
})
});
}
});
$('#category_name').val('');
$('#categoryModal').modal({'backdrop':'static'});
}
function doModalAddCategory()
{
if(category_datatable != 1) {
initializeCategory();
}
else {
$('#category_name').val('');
$('#categoryModal').modal({'backdrop':'static'});
}
}
</script>
<!-- End of Brand js -->