简体   繁体   English

如何使用 laravel 获得模态确认删除显示数据?

[英]How to get modal confirm delete showing the data using laravel?

i want to get value id from my table or my db, when i tried to delete one of the row (one of the data).当我尝试删除其中一行(数据之一)时,我想从我的表或数据库中获取值 id。 I tried to expand my endforeach and it's just make the html become so weird.我试图扩展我的endforeach ,它只是让 html 变得如此奇怪。 Um for more explaining here is my images.嗯更多解释这里是我的图像。

Images confirm modal图像确认模态

在此处输入图像描述

yeah i just want to confirm the user if it clicked the delete button it will show a modal "are you sure you want to delete this?".So, the problem is, i don't know how to get the value id when i clicked the next delete button from the seccond row delete button, delete button from the third row delete button, delete button from the fourth row delete button (if there is a row bellow it) and so on.是的,我只是想确认用户是否单击了删除按钮,它将显示模式“您确定要删除它吗?”。所以,问题是,我不知道如何获取值 id 当我从第二行删除按钮单击下一个删除按钮,从第三行删除按钮单击删除按钮,从第四行删除按钮单击删除按钮(如果下面有一行)等等。

here is my modal code这是我的模态代码

Modal Delete模态删除

<div class="m-2">
<div  class="modal fade h-50" id="modalDelete" tabindex="-1" role="dialog" aria- 
    labelledby="deleteModal" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteModal">Change Department Status</h5>
            </div>
            <div class="modal-body">
                <p id="question">Are You sure want to delete {{$ideaprogram[0]->showidea_id}}?</p>
            </div>
            <div class="modal-footer">
                <a id="deleteData">
                    <button type="button" class="btn btn-success">Yes</button>
                </a>
                <button type="button" class="btn btn-danger" data-dismiss="modal">No</button>
            </div>
        </div>
    </div>
</div>
</div>

i'm just make the value of index $ideaprogram[0]->showidea_id like that.我只是像这样使索引$ideaprogram[0]->showidea_id的值。 Using zero (0) the hardcode.使用零 (0) 硬代码。 for example if i want to delete the next one (the next index) which is 1, how can i change it the value into 1 or change it the value into 2 if i want to delete the next index and so on.例如,如果我想删除下一个(下一个索引)为 1,我如何将它的值更改为 1,或者如果我想删除下一个索引,我如何将它的值更改为 2,依此类推。 Basically i want the function more like when i clicked the delete button from some row, it will show the id program from that row.基本上我想要 function 更像是当我从某行单击删除按钮时,它将显示该行的 id 程序。 I'm using laravel and php at the moment.我目前正在使用 laravel 和 php 。 I don't know how to make it happen.我不知道如何实现它。 Can someone help me?有人能帮我吗?

Set data in modal on delete button click:在删除按钮单击时以模式设置数据:

assuming that you are showing your data in table, then give attribute data-programid to delete button.假设您在表中显示数据,然后将属性 data-programid 赋予删除按钮。

@foreach($programs as $program)
<tr>
<td>{{$program->name}}</td>
<td><button class="btn btn-danger deleteProgram" data-programid="{{$program->id}}">Delete</button></td>  
</tr>
@endforeach

now we set data in modal and show modal, when user click on deleteProgram button class javascript现在我们在模态中设置数据并显示模态,当用户单击 deleteProgram 按钮 class javascript

<script>
$(document).on('click','.deleteProgram',function(){
    var programID=$(this).attr('data-programid');
    $('#app_id').val(programID); 
    $('#question').append(programID+' ?');
    $('#applicantDeleteModal').modal('show'); 
});
</script>

your modal:你的模态:

<div id="applicantDeleteModal" class="modal modal-danger fade" tabindex="-1" role="dialog" aria-labelledby="custom-width-modalLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog" style="width:55%;">
        <div class="modal-content">
             <form action="{{route()}}" method="POST" class="remove-record-model">
               {{ method_field('delete') }}
               {{ csrf_field() }}

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h5 class="modal-title text-center" id="custom-width-modalLabel">Change Department Status</h5>
            </div>
            <div class="modal-body">
                <h4 id="question">Are You sure want to delete </h4>
                <input type="hidden" name="applicant_id" id="app_id">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-danger waves-effect remove-data-from-delete-form">Delete</button>
            </div>

             </form>
        </div>
    </div>
</div>

I use like this all time.我一直都这样用。

<td>

    <ul style="list-style-type: none;">
          <li><i class="fa fa-edit"></i> <a href="{{ url('') }}/en/admin/role/edit/{{ $role->id }}"> Edit </a></li>
          <li><i class="fa fa-trash"></i>
          <button data-toggle="modal" data-target="#modal{{ $role->id }}">Delete</button>
          </li>
    </ul>

</td>

<!-- Modal -->
<div class="modal fade" id="modal{{ $role->id }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
   <div class="modal-dialog" role="document">
      <div class="modal-content">
          <div class="modal-header bg-danger">
              <h5 class="modal-title" id="exampleModalLabel">You are about to delete the role {{ $role->title }}</h5>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
              </button>
          </div>
          <div class="modal-body text-danger">Once you delete, it cannot be undone.
          </div>
          <div class="modal-footer">
               <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
               <form method="POST" action="{{ url('') }}/en/admin/role/delete/{{ $role->id }}">
                    {{ method_field('delete') }}
                    {{ csrf_field() }}
                    <button type="submit" class="btn btn-danger">Confirm</button>
               </form>
          </div>
       </div>
    </div>
 </div>

Actually i want an answer that give me a solutions more like AA Noman since it will be good for a little line of code, so i tought it possible, but after trying the suggested solutions, and not give me that i want, so i think i can solve it like this.实际上,我想要一个答案,给我一个更像 AA Noman 的解决方案,因为它对一小行代码很有用,所以我认为这是可能的,但是在尝试了建议的解决方案之后,并没有给我我想要的,所以我想我可以这样解决。

script.js脚本.js

function deleteModal(data){
     document.getElementById('question').innerHTML = `Are you sure to delete ${data.showidea_id} ?`;
     document.getElementById('deleteData').setAttribute('href',`/program_idea/${data.showidea_id}/deleteData`);
     $('#modalDelete').modal('show'); 
}

and my HTML are like this而我的 HTML 是这样的

index.blade.php index.blade.php

@foreach($ideaprogram as $dataload)
        <tr>
            <td class="text-center">
                <button type="button" class="btn btn-danger btn-sm deleteProgram" data-toggle="modal" 
                       onclick="deleteModal({{$dataload}})">
                    <i class="far fa-trash-alt"></i>
                </button>
        </tr>
 @endforeach

anyway thank you for everyone that answer my questions无论如何,谢谢所有回答我问题的人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM