dimanche 19 janvier 2020

pass checkbox array values to bootstrap modals LARAVEL

i want to edit checkbox array modal bootstrap in laravel ? how can i display 'checked by user that have values in store_user' when i press edit button and how to pass checkbox array values. :'(

i have 3 tables

  1. users : id, ...

  2. stores : id, ....

  3. store_user : id, user_id, store_id.

i use many to many relationship.

my index.blade.php

<table id="dtBasicExample" class="table table-striped table-bordered" cellspacing="0" width="100%">
      <thead>
        <tr> 
          <th class="th-sm">No
          </th>
          <th class="th-sm">Name
          </th>
          <th class="th-sm">Position
          </th>
          <th class="th-sm">Outlet
          </th>
          <th class="th-sm">Email
          </th>
          <th class="th-sm">Phone
          </th> 
          <th class="th-sm">Edit
          </th>
          <th class="th-sm">Delete
          </th>
        </tr>

      </thead>
      <tbody> 
        @foreach($users as $user)
        <tr style="text-align: center;"> 
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>

          @if($user->profile()->count('phone') > 0)
          <td></td> 
          @else
          <td>-</td>
          @endif
          <!-- Edit -->
          <td>
            <div class="form-group"> 
              <button type="button" data-stores="" data-id="" data-name="" data-roleid="" 
                data-phone="@if($user->profile()->count('phone') > 0)@endif" class="btn btn-outline-primary block btn-md" data-toggle="modal" data-target="#sendfriend">
                Edit
              </button> 

              <!-- Modal -->
              <div class="modal fade text-xs-left" id="sendfriend" tabindex="-1" role="dialog" aria-labelledby="myModalLabel33" aria-hidden="true"> 
                <div class="modal-dialog" role="document">
                  <div class="modal-content">
                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                      </button>
                      <label class="modal-title text-text-bold-600" id="myModalLabel33">Edit Form</label>
                    </div>
                    <form action="" method="post">
                      @csrf 

                      <div class="modal-body">

                          <input type="hidden" name="iduser"> 
                          <label>Name: </label>
                          <div class="form-group">
                            <input name="name" type="text" placeholder="Name" class="form-control" id="name">
                          </div>

                          <label>Phone Number: </label>
                          <div class="form-group">
                            <input name="phone" type="text" placeholder="Phone Number : (62)" class="form-control" id="phone">
                          </div>
                          <div class="form-group">
                              <label for="stores">Assign Store</label>
                              @foreach($stores as $store)
                              <div class="checkbox">
                                  <label><input id="tes" type="checkbox" name="stores[]" value=""> 
                                     @if($user->id == 'item')
                                       
                                    @endif 
                                  </label>
                              </div>
                              @endforeach
                          </div>
                          <label for="role_id">Access</label> 
                          <div class="form-group">
                            <select name="role_id" class="form-control" data-toggle="tooltip" data-trigger="hover" data-placement="top" data-title="Priority">
                            <i class="icon-key2"></i> 
                              @foreach($roles as $role)
                              <option id="roleid" value=""
                                @if($role->id == 'item')
                                  selected
                                @endif
                              ></option>
                              @endforeach 
                            </select>
                          </div>
                      </div>
                      <div class="modal-footer">
                        <button type="submit" class="btn btn-outline-primary waves-effect waves-light">Save</button> 
                        <button type="button" class="btn btn-outline-primary" data-dismiss="modal">Cancel!</button> 
                      </div>
                    </form>
                  </div>
                </div>
              </div>
            </div>
          </td> 

          <!-- Delete -->
          <td>
            <div class="form-group">
              <button type="button" data-id="" class="btn btn-outline-danger block btn-md" data-toggle="modal" data-target="#sendfriend2">
                Delete
              </button> 

              <!-- Modal -->
              <div class="modal fade text-xs-left" id="sendfriend2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel33" aria-hidden="true"> 
                <div class="modal-dialog" role="document">
                  <div class="modal-content">
                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                      </button>
                      <label class="modal-title text-text-bold-600" id="myModalLabel33">Delete Form</label>
                    </div>
                    <form action="" method="post">
                      @csrf 
                        <div class="modal-body">
                          <input type="hidden" name="iduser2"> 
                          <div class="alert alert-danger" role="alert">
                           <span class="text-bold-600">Well done!</span> Are you sure you want to delete this item ? 
                          </div> 
                      </div>
                      <div class="modal-footer">
                      <button type="submit" class="btn btn-outline-danger">Delete!</button> 
                      <button type="button" class="btn btn-outline-primary" data-dismiss="modal">Cancel!</button> 
                      </div>
                    </form>
                  </div>
                </div>
              </div>
            </div>
          </td> 
        </tr> 
        @endforeach 

      </tbody>
      <tfoot>
        <tr>
          <th>No
          </th>
          <th>Name
          </th>
          <th>Position
          </th>
          <th>Outlet
          </th>
          <th>Email 
          </th>
          <th>Phone
          </th> 
          <th>Edit
          </th>
          <th>Delete
          </th>
        </tr>
      </tfoot>
      </table>

<script type="text/javascript">
  $('#sendfriend').on('show.bs.modal', function (event) {

  var button = $(event.relatedTarget) // Button that triggered the modal
  var id = button.data('id') // Extract info from data-* attributes 
  var name = button.data('name') // Extract info from data-* attributes
  var roleid = button.data('roleid') // Extract info from data-* attributes
  var phone = button.data('phone') // Extract info from data-* attributes
  var modal = $(this)
  modal.find('.modal-body input[name="iduser"]').val(id) 
  modal.find('.modal-body input[name="name"]').val(name) 
  modal.find('.modal-body input[name="phone"]').val(phone) 
  modal.find('.modal-body select').val(roleid) 

}) 
$('#sendfriend2').on('show.bs.modal', function (event) {

  var button = $(event.relatedTarget) // Button that triggered the modal
  var id = button.data('id') // Extract info from data-* attributes 
  var modal = $(this)
  modal.find('.modal-body input[name="iduser2"]').val(id) 
})   
</script>   



Aucun commentaire:

Enregistrer un commentaire