Advertisement
LTroya

Dropdown dependientes

Jun 15th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.12 KB | None | 0 0
  1. // Index.html
  2. <body>
  3.  
  4. <select id="tasks">
  5.     @foreach($tasks as $task)
  6.         <option value="{{ $task->id }}">{{ $task->title }}</option>
  7.     @endforeach
  8. </select>
  9.  
  10. <select id="subtasks"></select>
  11.  
  12. <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  13. <script>
  14.     $(document).ready(function () {
  15.         $('#tasks').change(function (ev) {
  16.             var id = ev.target.value;
  17.  
  18.             $.ajax({
  19.                 method: 'GET',
  20.                 url: `/tasks/${id}/subtasks`
  21.             }).done(function (response) {
  22.                 var subTasks = $('#subtasks');
  23.  
  24.                 $.each(response, function(index, task) {
  25.                     subTasks.append($("<option />").val(task.id).text(task.title));
  26.                 });
  27.             })
  28.         });
  29.     });
  30. </script>
  31.  
  32. // Route.php
  33. Route::get('random', function () {
  34.     return view('random', [
  35.         'tasks' => \App\Task::all()
  36.     ]);
  37. });
  38.  
  39. Route::get('tasks/{task}/subtasks', 'TaskController@subTasks');
  40.  
  41. // TaskController
  42. public function subTasks(Task $task)
  43.     {
  44.         return Task::where('id', '>', $task->id)->get();
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement