У меня проблема с удалением новых элементов в crud, для старых данных это работает, но для вновь добавленных элементов это не так. Вот мой код
форма
@foreach ($campaigns as $campaign)
<tr id="campaign{{ $campaign->campaign_id }}">
<td id="campaign_no">{{ $loop->iteration }}</td>
<td id="td-campaign-{{ $campaign->campaign_id }}">{{ $campaign->campaign_name }}</td>
<td>{{ $campaign->created_at }}</td>
<td><span class="badge badge-success">{{ $campaign->status }}</span></td>
<td>
<button class="btn btn-icon btn-warning btn-icon-style-1 btn-xs"><span class="btn-icon-wrap"><i class="icon-rocket"></i></span></button>
<button class="btn btn-icon btn-secondary btn-icon-style-1 btn-xs open-modal" value="{{ $campaign->campaign_id }}" ><span class="btn-icon-wrap"><i class="fa fa-pencil"></i></span></button>
<button class="btn btn-icon btn-danger btn-icon-style-1 btn-xs delete-modal" data-toggle="modal" data-target="#purchaseModal" value="{{ $campaign->campaign_id }}"><span class="btn-icon-wrap"><i class="fa fa-trash"></i></span></button>
</td>
</tr>
@endforeach
модальный:
<div class="modal fade" id="purchaseModal" tabindex="-1" role="dialog" aria-labelledby="purchaseLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="purchaseLabel">Delete Confirmation</h4>
</div>
<div class="modal-body">
Are you sure?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-danger delete-link" id="delete-now" value="">Delete</button>
</div>
</div>
</div>
js добавить элемент:
$("#btn-save").click(function (e) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': jQuery('meta[name="csrf-token"]').attr('content')
}
});
e.preventDefault();
var formData = {
campaign_name: jQuery('#campaign_name').val(),
};
var state = jQuery('#btn-save').val();
var type = "POST";
var link_id = jQuery('#link_id').val();
var ajaxurl = 'campaign/save';
if (state == "Update") {
type = "POST";
ajaxurl = 'campaign/update/'+link_id;
//alert('update detected');
}
$.ajax({
type: type,
url: ajaxurl,
data: formData,
dataType: 'json',
success: function (data) {
if (state == "add") {
var link = '<tr id="link' + data.id + '">';
link += '<td>' + $('#increment').val() + '</td><td>' + data.campaign_name + '</td><td>Few moments ago</td><td><span class="badge badge-success">' + data.status + '</span></td>';
link += '<td><button class="btn btn-icon btn-warning btn-icon-style-1 btn-xs"><span class="btn-icon-wrap"><i class="icon-rocket"></i></span></button> <button class="btn btn-icon btn-secondary btn-icon-style-1 btn-xs"><span class="btn-icon-wrap"><i class="fa fa-pencil"></i></span></button> <button class="btn btn-icon btn-danger btn-icon-style-1 btn-xs delete-modal" data-toggle="modal" data-target="#purchaseModal" value="'+data.id+'"><span class="btn-icon-wrap"><i class="fa fa-trash"></i></span></button> </td>';
jQuery('#campaign-list').append(link);
$.toast({
text: '<i class="jq-toast-icon ti-check-box"></i><p>Added successfully!</p>',
position: 'bottom-right',
loaderBg:'#7a5449',
class: 'jq-has-icon jq-toast-success',
hideAfter: 3500,
stack: 6,
showHideTransition: 'fade'
});
} else {
$.get('campaign/edit/' + link_id, function (data) {
$('#td-campaign-'+link_id).html(data[0].campaign_name);
$.toast({
text: '<i class="jq-toast-icon ti-check-box"></i><p>Changes saved!</p>',
position: 'bottom-right',
loaderBg:'#7a5449',
class: 'jq-has-icon jq-toast-success',
hideAfter: 3500,
stack: 6,
showHideTransition: 'fade'
});
// $("#campaign" + link_id).replaceWith(newx);
})
}
jQuery('#modalFormData').trigger("reset");
jQuery('#linkEditorModal').modal('hide');
},
error: function (data) {
alert('Failed');
console.log('Error:', data);
}
});
});
js удалить элемент:
jQuery('.delete-modal').click(function () {
var link_id = $(this).val();
alert(link_id); //the return is null, but for old item its not.
jQuery('#delete-now').val(link_id);
});
////----- DELETE a link and remove from the page -----////
jQuery('.delete-link').click(function () {
var link_id = $(this).val();
alert(link_id); //the return is null, but for old item its not.
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': jQuery('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "DELETE",
url: 'campaign/delete/' + link_id,
success: function (data) {
console.log(data);
$("#campaign" + link_id).remove();
jQuery('#purchaseModal').modal('hide');
$.toast({
text: '<i class="jq-toast-icon ti-check-box"></i><p>Deleted successfully!</p>',
position: 'bottom-right',
loaderBg:'#7a5449',
class: 'jq-has-icon jq-toast-success',
hideAfter: 3500,
stack: 6,
showHideTransition: 'fade'
});
location.reload();
},
error: function (data) {
console.log('Error:', data);
}
});
});
маршрут:
Route::delete('/campaign/delete/{link_id?}', function ($link_id) {
$campaign = Campaign::where('campaign_id',$link_id)->delete();
return Response::json($campaign);
});
У меня предупреждение о записи и ничего не отображается для значения id, что не так с кодом?
Laravel - это бесплатный веб-фреймворк с открытым исходным кодом PHP, созданный Тейлором Отвеллом и предназначенный для разработки веб-приложений по архитектурному шаблону модель-представление-контроллер и основанный на Symfony.