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();
                    });
                }
            }
        )
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码