模态将计数器重置为1

时间:2018-09-19 07:25:30

标签: php laravel

即使位于foreach循环中,也不确定如何重置自身。这是我的刀片视图中的摘录:

<tbody>
    <?php $counter = 1; ?>
    @foreach ($guidelines as $guideline)
    <tr>
        <td class="text-center">{{ $counter }}</td>
        <td>{{ $guideline->description }}</td>
        <td>
            <a href="/general-guidelines/{{ $guideline->id }}/edit" class="btn btn-warning btn-sm"><i class="far fa-edit"></i></a> 
            <button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal"><i class="far fa-trash-alt"></i></button> 
            <div class="modal fade" id="myModal" role="dialog">
                <div class="modal-dialog modal-sm">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title">Confirm Delete</h4>
                        </div>

                        <div class="modal-body">
                            <p>Are you sure you want to delete this?</p>
                        </div>

                        <div class="modal-footer">
                            <a href="/general-guidelines/{{ $guideline->id }}/delete" class="btn btn-danger">Confirm</a>
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
        </td>
        <?php $counter++; ?>
    </tr>
    @endforeach
</tbody>

编辑可以与/general-guidelines/1/edit/general-guidelines/2/edit/general-guidelines/5/edit等一起使用。但是/delete总是以/1/delete结尾

2 个答案:

答案 0 :(得分:2)

首先,因为您只有一个模态(#myModal),并且您的按钮始终引用(#myModal),所以它将始终打开第一个模态。尝试解决方案#myModal{{ $loop->index }}

第二,您可以在foreach循环中使用$ loop-> iteration代替$ counter。

尝试一下。

<tbody>
@foreach ($guidelines as $guideline)
<tr>
    <td class="text-center">{{ $counter }}</td>
    <td>{{ $guideline->description }}</td>
    <td>
        <a href="/general-guidelines/{{ $guideline->id }}/edit" class="btn btn-warning btn-sm"><i class="far fa-edit"></i></a> 
        <button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#myModal{{ $loop->index }}"><i class="far fa-trash-alt"></i></button> 
        <div class="modal fade" id="myModal{{ $loop->index }}" role="dialog">
            <div class="modal-dialog modal-sm">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">Confirm Delete</h4>
                    </div>

                    <div class="modal-body">
                        <p>Are you sure you want to delete this?</p>
                    </div>

                    <div class="modal-footer">
                        <a href="/general-guidelines/{{ $guideline->id }}/delete" class="btn btn-danger">Confirm</a>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
    </td>
</tr>
@endforeach

答案 1 :(得分:0)

with for循环会创建多个模态,但是对于for循环生成的所有模态,不能使用相同的id'myModal'。 id应该是唯一的。