Эх сурвалжийг харах

stag-suggest - auto-populate [data-name="x"] on selection

Vijayakrishnan 3 жил өмнө
parent
commit
21df9d913f

+ 11 - 0
public/js/stag-suggest.js

@@ -162,9 +162,20 @@
             let input = $(this).closest('.position-relative').prev('[stag-suggest]');
             input.val(label);
             input.data('suggest-data', data);
+
+            let scope = input.attr('stag-suggest-scope');
+            if(!scope) scope = 'form';
+            scope = $(scope);
+
             for(let x in data) {
                 if(data.hasOwnProperty(x)) {
                     input.attr('data-' + x, data[x]);
+
+                    // auto-populate if there's a field matching data-name="x" in the scope
+                    if(scope.find('[data-name="' + x + '"]').length) {
+                        scope.find('[data-name="' + x + '"]').val(data[x]).trigger('change');
+                    }
+
                 }
             }
             input.trigger('input');