dsyrdwdbo47282676 2019-07-05 05:43
浏览 128
已采纳

具有甜蜜警报的数据删除记录不起作用

I'm writing a crm script. But i have a problem. Customers are in the datatable. I want to delete with confirmation from database and datatable. I write some codes. And put in foreach. But codes only work at page 1. How can i fix it?

My button on datatable:

<form id="mulksilme-<?php echo $mulkid; ?>" method="POST">
    <input type="text" style="display: none;" name="mulkklavus" value="<?php echo $mulkid; ?>">
    <button id="sa-warning" type="submit" class="btn btn-info">
        <i class="ti-trash"> </i>
    </button>
</form>

My javascript(this code generating for each row):

$('#myTable').dataTable({
    "drawCallback": function (settings) {
        $("#mulksilme-<?php echo $mulkid; ?>").on("submit", function (e) {
            e.preventDefault();
            Swal.fire({
                title: 'Emin Misiniz?',
                text: "Mülk sonsuza dek silinecektir!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Evet, silinsin!',
                cancelButtonText: 'Hayır, vazgeç!'
            }).then((result) => {
                    if (result.value) {
                        $.ajax({
                            url: "sil.php",
                            type: "POST",
                            data: new FormData(this),
                            contentType: false,
                            processData: false,

                        })
                        location.reload();
                    }
                }
            )
        });
    }
});
  • 写回答

1条回答 默认 最新

  • dsq2015 2019-07-05 06:03
    关注

    Passing the ID from the server-side will not work in this case. You have to fetch the ID per row on the client-side.

    Example

    <table id="data-table" class="table table-striped table-bordered dt-responsive nowrap dataTable no-footer dtr-inline collapsed">
        <thead>
        <tr>
            <th>Username</th>
            <th>E-Mail/th>
            <th>First name</th>
            <th>Last name/th>
            <th>Role</th>
            <th>Enabled</th>
            <th>Created at</th>
            <th>Action</th>
        </tr>
        <tfoot></tfoot>
    </table>
    
    const table = $('#data-table').DataTable({
        'processing': true,
        'serverSide': true,
        'language': {
            'url': __('js/datatable-english.json')
        },
        'ajax': {
            'url': 'users/list',
            'type': 'POST'
        },
        'columns': [
            {'data': 'username'},
            {'data': 'email'},
            {'data': 'first_name'},
            {'data': 'last_name'},
            {'data': 'role'},
            {'data': 'enabled'},
            {'data': 'created_at'},
            {
                'orderable': false,
                'searchable': false,
                'data': null,
                'render': function (data, type, row, meta) {
                    return '<button type="button" class="btn btn-info">Edit</button>';
                }
            }
        ],
    });
    
    $('#data-table tbody').on('click', 'button', table, function () {
        const data = table.row($(this).parents('tr')).data();
        //alert('Edit user: ' + data.id);
    
        Swal.fire({
            title: 'Emin Misiniz?',
            text: "Mülk sonsuza dek silinecektir!",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Evet, silinsin!',
            cancelButtonText: 'Hayır, vazgeç!'
        }).then(function (result) {
                if (result.value) {
                    $.ajax({
                        url: "sil.php",
                        type: "POST",
                        contentType: 'application/json',
                        dataType: 'json',
                        data: JSON.stringify(data)
                    }).done(function (result) {
                        alert('done');
                        //location.reload();
                    });
                }
            }
        )
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?