|
7 | 7 | var columns = @json($columns); |
8 | 8 | var forms = @json($forms); |
9 | 9 | jQuery(document).ready(function($) { |
| 10 | + /* Initialisation */ |
10 | 11 | $('.select2').select2(); |
| 12 | +
|
11 | 13 | /* Form Submission */ |
12 | 14 | $(document).on('click', '.form-btn', function(event) { |
13 | 15 | event.preventDefault(); |
14 | | - axios.post(route(routes.store), $('#' + forms.create).serialize()) |
15 | | - .then(response => { |
16 | | - $(table_id).DataTable().ajax.reload(); |
17 | | - swal('{!! __('User') !!}', response.data.message, 'success'); |
18 | | - $('#' + forms.create) |
19 | | - .find('input, textarea, select') |
20 | | - .val(''); |
21 | | - $('#user-modal').modal('hide'); |
22 | | - }).catch(error => console.error(error)); |
| 16 | + /* Can be refactor to determine to use post or put */ |
| 17 | + if($("[name='_method']").val() == 'PUT') { |
| 18 | + axios.put(route(routes.store), $('#' + forms.create).serialize()) |
| 19 | + .then(response => { |
| 20 | + $(table_id).DataTable().ajax.reload(); |
| 21 | + swal('{!! __('User') !!}', response.data.message, 'success'); |
| 22 | + $('#' + forms.create) |
| 23 | + .find('input, textarea, select') |
| 24 | + .val(''); |
| 25 | + $('#user-modal').modal('hide'); |
| 26 | + }).catch(error => console.error(error)); |
| 27 | + } else { |
| 28 | + axios.post(route(routes.store), $('#' + forms.create).serialize()) |
| 29 | + .then(response => { |
| 30 | + $(table_id).DataTable().ajax.reload(); |
| 31 | + swal('{!! __('User') !!}', response.data.message, 'success'); |
| 32 | + $('#' + forms.create) |
| 33 | + .find('input, textarea, select') |
| 34 | + .val(''); |
| 35 | + $('#user-modal').modal('hide'); |
| 36 | + }).catch(error => console.error(error)); |
| 37 | + } |
23 | 38 | }); |
24 | 39 |
|
25 | 40 | /* Actions */ |
| 41 | + $(document).on('click', '.create-action-btn', function(event) { |
| 42 | + /* Handle Method Spoofing */ |
| 43 | + $("[name='_method']").val('POST'); |
| 44 | + /* Handle primary key */ |
| 45 | + $("[name='id']").val(null); |
| 46 | + $('#user-modal').modal('show'); |
| 47 | + }); |
| 48 | +
|
26 | 49 | $(document).on('click', '.show-action-btn', function(event) { |
27 | 50 | event.preventDefault(); |
28 | 51 | var id = $(this).data(primary_key); |
|
41 | 64 | $('#view-user-modal').modal('show'); |
42 | 65 | }) |
43 | 66 | .catch(error => console.error(error)); |
44 | | - |
45 | 67 | }); |
46 | 68 |
|
47 | 69 | $(document).on('click', '.edit-action-btn', function(event) { |
48 | 70 | var id = $(this).data(primary_key); |
49 | 71 | axios.get(route(routes.show, id)) |
50 | 72 | .then(response => { |
| 73 | + /* Handle Method Spoofing */ |
| 74 | + $("[name='_method']").val('PUT'); |
| 75 | + /* Handle primary key */ |
| 76 | + $("[name='id']").val(id); |
51 | 77 | var data = response.data.data; |
52 | | - |
53 | 78 | $.each(columns, function(index, val) { |
54 | 79 | if(data[index] != null) { |
55 | | - $("[name='" + index + "']").val(data[index]); |
| 80 | + if(typeof data[index] == 'object') { |
| 81 | + var values = $.map(data[index], function(elem, idx) { |
| 82 | + return Number(idx); |
| 83 | + }); |
| 84 | + $("[name='" + index + "[]']").val(values); |
| 85 | + if($("[name='" + index + "[]']").hasClass('select2')) { |
| 86 | + $("[name='" + index + "[]']").trigger('change'); |
| 87 | + } |
| 88 | + } else { |
| 89 | + $("[name='" + index + "']").val(data[index]); |
| 90 | + } |
56 | 91 | } |
57 | 92 | }); |
58 | 93 | $('#user-modal').modal('show'); |
|
63 | 98 | $(document).on('click', '.destroy-action-btn', function(event) { |
64 | 99 | event.preventDefault(); |
65 | 100 | var id = $(this).data(primary_key); |
66 | | -
|
67 | 101 | swal({ |
68 | 102 | title: '{!! __('Warning') !!}', |
69 | 103 | text: '{!! __('Are you sure want to delete this record?') !!}', |
|
0 commit comments