In our users table, there is an edit modal that needs to retrieve specific user data from the table list and display it within the modal's input fields. This requires using Laravel logic to edit the data and submit it back to the controller as a form.
Table list
<div id='recipients' class="p-8 mt-6 rounded shadow bg-white">
<table id="users" class=" dt-responsive display nowrap" style="width:100%; padding-top: 1em; padding-bottom: 1em;">
<thead>
<tr>
<th data-orderable="false">Username </th>
<th data-orderable="false">Email</th>
<th data-orderable="false">Role </th>
<th data-orderable="false">Active</th>
<th data-orderable="false"></th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<input type="hidden" id="way-id" value="{{ $user->id }}"/>
<td id="editable">{{ $user->name }}</td>
<td id="editable">{{ $user->email }}</td>
<td id="editable">
@foreach ($user->getRoleNames() as $roleNames)
{{ $roleNames }}
@endforeach
</td>
<td id="editable">
<label class="switch mb-3">
<input type="checkbox" id="active-user" />
<div class="slider"></div>
</label>
</td>
<td>
<button type="button" class="btn btn-default btn-edit js-edit" id="{{$user->id}}"><img
class="mb-1 duration-300 ml-4 inset-0 h-6 w-6" src="/../../img/grey-edit.svg" alt="edit"
onclick="(function(){var listInput = document.getElementById('way-id').value;
document.getElementById('testInout').value = listInput;
})();toggleModal('modal-edit-user'); "></button>
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="flex justify-between mt-4 mb-4">
<button type="button"
class="shadow float-left md:shadow-lg rounded-full font-bold px-10 bg-gray-500 text-white">
<img class="inline-block mb-2 mt-2 w-4 h-4 mr-2" src="../../img/Icon-white-search.png" alt="search icon">
<a class="no-underline inline-block text-white " onclick="toggleModal('modal-user-types')">User Types</a>
</button>
<button type="button"
class="shadow float-right md:shadow-lg rounded-full font-bold px-10 grantors-btn text-white">
<img class="inline-block mb-2 mt-2 w-4 h-4 mr-2" src="../../img/Icon-plus-white.png" alt="plus icon">
<a class="no-underline inline-block text-white " onclick="toggleModal('modal-new-user')">New User</a>
</button>
</div>
</div>
Edit User modal
<div class="hidden overflow-x-hidden overflow-y-auto fixed inset-0 z-50 outline-none focus:outline-none justify-center items-center"
id="modal-edit-user">
... (content continues)
</div>
<!--footer-->
</div>
The calling modal
<script type="text/javascript">
//
function toggleModal(modalID) {
document.getElementById(modalID).classList.toggle("hidden");
document.getElementById(modalID + "-backdrop").classList.toggle("hidden");
document.getElementById(modalID).classList.toggle("flex");
document.getElementById(modalID + "-backdrop").classList.toggle("flex");
}
</script>
Initial attempts to pass the user ID to the modal form using inline JavaScript have proved challenging. The ID does not update with each button click, and transitioning back to Laravel variables and logic for displaying query data is problematic.