Skip to content

Commit 06426de

Browse files
committed
allow to add additional parameters from for data
1 parent b69a8d9 commit 06426de

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

Form/Type/Select2EntityType.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Symfony\Component\Routing\RouterInterface;
1414
use Tetranz\Select2EntityBundle\Form\DataTransformer\EntitiesToPropertyTransformer;
1515
use Tetranz\Select2EntityBundle\Form\DataTransformer\EntityToPropertyTransformer;
16+
use Symfony\Component\PropertyAccess\PropertyAccess;
1617

1718
/**
1819
*
@@ -94,6 +95,17 @@ public function finishView(FormView $view, FormInterface $form, array $options)
9495
$view->vars[$varName] = $options[$varName];
9596
}
9697

98+
if (isset($options['req_params']) && is_array($options['req_params']) && count($options['req_params']) > 0) {
99+
$accessor = PropertyAccess::createPropertyAccessor();
100+
101+
$reqParams = [];
102+
foreach ($options['req_params'] as $key => $reqParam) {
103+
$reqParams[$key] = $accessor->getValue($view, $reqParam . '.vars[full_name]');
104+
}
105+
106+
$view->vars['attr']['data-req_params'] = json_encode($reqParams);
107+
}
108+
97109
//tags options
98110
$varNames = array_keys($this->config['allow_add']);
99111
foreach ($varNames as $varName) {
@@ -154,6 +166,7 @@ public function configureOptions(OptionsResolver $resolver)
154166
'transformer' => null,
155167
'autostart' => true,
156168
'width' => isset($this->config['width']) ? $this->config['width'] : null,
169+
'req_params' => array(),
157170
)
158171
);
159172
}

Resources/public/js/select2entity.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
scroll = $s2.data('scroll'),
99
prefix = Date.now(),
1010
cache = [];
11+
12+
var reqParams = $s2.data('req_params');
13+
if (reqParams) {
14+
$.each(reqParams, function (key, value) {
15+
$('*[name="'+value+'"]').on('change', function () {
16+
$s2.val(null);
17+
$s2.trigger('change');
18+
});
19+
});
20+
}
21+
1122
// Deep-merge the options
1223
$s2.select2($.extend(true, {
1324
// Tags support
@@ -43,17 +54,23 @@
4354
}
4455
},
4556
data: function (params) {
57+
var ret = {
58+
'q': params.term
59+
};
60+
61+
var reqParams = $s2.data('req_params');
62+
if (reqParams) {
63+
$.each(reqParams, function (key, value) {
64+
ret[key] = $('*[name="'+value+'"]').val()
65+
});
66+
}
67+
4668
// only send the 'page' parameter if scrolling is enabled
4769
if (scroll) {
48-
return {
49-
q: params.term,
50-
page: params.page || 1
51-
};
70+
ret['page'] = params.page || 1;
5271
}
5372

54-
return {
55-
q: params.term
56-
};
73+
return ret;
5774
},
5875
processResults: function (data, params) {
5976
var results, more = false, response = {};

0 commit comments

Comments
 (0)