how to add alert on-click button Submit datatables yajra ? Laravel

拟墨画扇 提交于 2020-01-16 09:05:18

问题


I have a table data , this table have function delete button . this button is successfully delete but i need to add alert on-click confirmation to delete like this :

<a href="delete.php?id=<?=$row['id']; ?>" 
onclick="return confirm('Anda yakin mau menghapus item ini ?')">[Hapus]</a>

but i using datatales serverside i dont know where i put this onclick

its my function delete

 public function indexDataTables_pns()
{

    $pns = Data_pns::with('users','master_golongan','master_jabatan')->get();


    return Datatables::of($pns)->addIndexColumn()

    ->addColumn('Nama', function ($pns) {
        return '<a href="/pns/'.$pns->id.'" target="_blank">'.$pns->users->nama.'</a>';   
    })
    ->editColumn('edit', function ($pns) {
        return '<a href="/edit_pns/'.$pns->id.'" target="_blank" class="btn btn-xs btn-success"><i class="glyphicon glyphicon-edit"></i></a>';
    })
    ->editColumn('hapus', function ($pns) {

        // THIS START HERE FOR DELETE FUNCTION

        $c = csrf_field();
        $m = method_field('DELETE');
    return "<form action='/delete/$pns->id' method='POST'>
            $c
            $m

            <button style='margin-left:10px; width: 30px;' type='submit'

                    class='btn btn-xs btn-danger delete'>
                <i class='glyphicon glyphicon-remove-circle'></i>
            </button>
        </form>";
    })
    ->rawColumns(['Nama' => 'Nama','hapus' => 'hapus','action' => 'action','edit'=>'edit'])
    ->make(true);

}

where i can put this onclick and this message ?


回答1:


you can use it on your delete form's button like

<button style='margin-left:10px; width: 30px;' type='submit'
 class='btn btn-xs btn-danger delete' onclick='return confirm("Anda yakin mau 
 menghapus item ini ?")'>
    <i class='glyphicon glyphicon-remove-circle'></i>
</button>

or add a class to your form and use confirmation before submitting the form

<form action='/delete/$pns->id' method='POST' class='delete-form'>

now add this script in your view file

<script>
    $('.delete-form').submit(function(event){
        if(!confirm('Anda yakin mau menghapus item ini ?')){
            event.preventDefault();
        }
    });
</script>


来源:https://stackoverflow.com/questions/58705295/how-to-add-alert-on-click-button-submit-datatables-yajra-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!