|
4 | 4 | var wrapManyFields = function() {
|
5 | 5 | $('.manyfield__holder').each(function(i, elem) {
|
6 | 6 | var canSort = $(elem).hasClass('manyfield__holder--cansort')
|
7 |
| - var canAdd = $(elem).find('.manyfield__add').length > 0 |
8 | 7 | var canRemove = $(elem).hasClass('manyfield__holder--canremove')
|
9 |
| - |
10 | 8 | var field = $(elem).find('.manyfield__row')
|
11 | 9 |
|
12 | 10 | $(elem).find('.manyfield__row').each(function (r, row) {
|
13 | 11 | if (canRemove) {
|
14 | 12 | if (!$(row).find('.manyfield__remove').length) {
|
15 |
| - var href = $(this).data('inline-save'); |
| 13 | + var href = $(this).data('inline-delete'); |
16 | 14 |
|
17 | 15 | if (href) {
|
18 | 16 | href.replace('saveRecord', 'deleteRecord');
|
19 |
| - href = href + '?ID='+ $(row).find('[name=ID]').val(); |
| 17 | + var id = $(row).find('input[type="hidden"][name*=ID]'); |
| 18 | + |
| 19 | + if (id.length < 1) { |
| 20 | + console.error('No ID hidden field in ManyField row'); |
| 21 | + } |
| 22 | + |
| 23 | + href = href + '?ID='+ id.val(); |
20 | 24 |
|
21 | 25 | $(row).prepend('<a class="btn btn-sm btn-danger manyfield__remove" href="' + href + '"><i class="fa fa-times"></i></a>');
|
22 | 26 | }
|
|
56 | 60 | value = $(field).val();
|
57 | 61 | }
|
58 | 62 |
|
| 63 | + var cleanName = name.substr(name.indexOf('[') + 1, name.indexOf(']') - (name.indexOf('[') + 1)); |
| 64 | + |
| 65 | + |
| 66 | + if (name.split('[').length > 3) { |
| 67 | + cleanName += name.substr(name.lastIndexOf('['), name.lastIndexOf(']') - (name.lastIndexOf('[') - 1)); |
| 68 | + } |
| 69 | + |
59 | 70 | data.push({
|
60 |
| - name: name.substr(name.indexOf('[') + 1, name.indexOf(']') - (name.indexOf('[') + 1)), |
| 71 | + name: cleanName, |
61 | 72 | value: value
|
62 | 73 | })
|
63 | 74 | })
|
64 | 75 |
|
| 76 | + |
65 | 77 | data.push({
|
66 | 78 | name: 'SecurityID',
|
67 | 79 | value: csrf
|
68 | 80 | })
|
69 | 81 |
|
70 | 82 | $.post(url, $.param(data), function() {
|
71 |
| - //.. |
| 83 | + $('body').trigger('manyFieldSaved'); |
72 | 84 | });
|
73 | 85 | })
|
74 | 86 |
|
75 | 87 | $('body').on('click', '.manyfield__add a', function(e) {
|
76 | 88 | e.preventDefault();
|
77 | 89 |
|
78 |
| - var |
79 |
| - form = $(this).parents('form'), |
80 |
| - parents = $(this).parents('.manyfield__holder'); |
| 90 | + var parents = $(this).parents('.manyfield__holder'); |
81 | 91 |
|
82 | 92 | $.get($(this).attr('href'), { index: parents.find('.manyfield__row').length }, function(data) {
|
83 | 93 | var rows = parents.find('.manyfield__row').last()
|
|
91 | 101 |
|
92 | 102 | // write the provided names as we don't need to namespace them in this
|
93 | 103 | // case - we can only edit one at a time
|
94 |
| - var content = $(data) |
| 104 | + var content = $(data); |
95 | 105 | content.find('input[name]').each(function(i, field) {
|
96 | 106 | if ($(field).attr('name').indexOf('[') !== false) {
|
97 | 107 | var name = $(field).attr('name').substring(
|
|
103 | 113 | }
|
104 | 114 | });
|
105 | 115 |
|
106 |
| - modal.find('.modal-body').html(content.html()); |
| 116 | + // move the modal form outside of the main form |
| 117 | + if (modal.parents('form').length > 0) { |
| 118 | + modal.detach().prependTo('body'); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + var saveURL = modal.attr('data-save-url'); |
| 123 | + content = $('<form action="'+ saveURL + '"></form>').html(data) |
| 124 | + modal.find('.modal-body').html(content); |
107 | 125 | modal.modal('show');
|
108 | 126 | } else {
|
109 | 127 | if (rows && rows.length) {
|
|
133 | 151 |
|
134 | 152 | parent.remove()
|
135 | 153 |
|
| 154 | + var data = { |
| 155 | + 'SecurityID': $('input[name=SecurityID]').val() |
| 156 | + }; |
| 157 | + |
136 | 158 | if ($(this).attr('href')) {
|
137 |
| - $.post($(this).attr('href')); |
| 159 | + $.post($(this).attr('href'), data); |
138 | 160 | }
|
139 | 161 |
|
140 | 162 | $('body').trigger('manyFieldRemoved', {
|
|
154 | 176 | form
|
155 | 177 | });
|
156 | 178 |
|
157 |
| - if (form.get(0).checkValidity()) { |
| 179 | + if (form.length > 0 && form.get(0).checkValidity()) { |
158 | 180 | var body = $(this).parents('.modal-content').find('.modal-body')
|
159 | 181 | .addClass('loading')
|
160 | 182 |
|
161 |
| - $.post(form.attr('action'), form.serialize(), function(reply) { |
| 183 | + var action = form.attr('action'); |
| 184 | + |
| 185 | + if (!action) { |
| 186 | + console.error('No action found on form'); |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + $.post(action, form.serialize(), function(reply) { |
162 | 191 | // reply should be the updated content for
|
163 |
| - form.parents('.manyfield__holder').html(reply); |
164 |
| - body.html(''); |
| 192 | + if (reply) { |
| 193 | + form.parents('.manyfield__holder').html(reply); |
| 194 | + body.html(''); |
| 195 | + } |
165 | 196 |
|
166 |
| - form.parents('.modal').modal('hide'); |
| 197 | + $('.modal').modal('hide'); |
167 | 198 | $('body').removeClass('modal-open');
|
168 | 199 | $('.modal-backdrop').remove();
|
169 | 200 |
|
|
175 | 206 | // highlight issues
|
176 | 207 | e.preventDefault();
|
177 | 208 |
|
| 209 | + alert('You are missing one or more fields'); |
| 210 | + |
178 | 211 | return false;
|
179 | 212 | }
|
180 | 213 | })
|
|
0 commit comments