lundi 22 avril 2019

Laravel - Opening modal in separate view

I created a view 'edit.blade.php' that contains a modal dialog and is opened via jQuery:

<!-- Modal -->
<div class="modal fade right" id="modalEditUser" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" >Edit user</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        @if ($errors->any())
        <div class="alert alert-danger">
          <ul>
              @foreach ($errors->all() as $error)
                <li></li>
              @endforeach
          </ul>
        </div><br />
        @endif
        <form method="post" action="">
          @method('PATCH')
          @csrf
          <div class="form-group">
            <label for="name">User Name:</label>
            <input type="text" class="form-control" name="name" value="" />
          </div>
      </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
          <button type="submit" class="btn btn-primary">Save user changes</button>
        </div>
      </form>
    </div>
  </div>
</div>
<!-- Modal -->

<script>

$(document).ready(function() {
  $('#modalEditUser').modal('show');  
    });
</script>

The view is called from the 'index.blade.php'

<td><a id="openModal" href="" class="btn btn-primary"><i class="fas fa-user"></i></a></td>

And in controller class, the edit method looks like this:

 public function edit($id)
    {
        $user = User::find($id);

        return view('admin.edit', compact('user'));
    }

The problem is that once I click the edit button on the index page, the edit page opens up blank and the modal shows up. What I'm trying to accomplish is to open the modal on the index page, without redirecting to edit page.

How can I open the edit page that contains the modal without redirecting to the edit page?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire