Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add render_selection option #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function getConfigTreeBuilder()
->scalarNode('width')->defaultNull()->end()
->scalarNode('object_manager')->defaultNull()->end()
->booleanNode('render_html')->defaultFalse()->end()
->booleanNode('render_selection')->defaultFalse()->end()
->end();

return $treeBuilder;
Expand Down
3 changes: 2 additions & 1 deletion Form/Type/Select2EntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public function configureOptions(OptionsResolver $resolver)
'callback' => null,
'class_type' => null,
'query_parameters' => [],
'render_html' => $this->config['render_html'] ?? false
'render_html' => $this->config['render_html'] ?? false,
'render_selection' => $this->config['render_selection'] ?? false
]
);
}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ If text_property is omitted then the entity is cast to a string. This requires i
* `autostart` Determines whether or not the select2 jQuery code is called automatically on document ready. Defaults to true which provides normal operation.
* `width` Sets a data-width attribute if not null. Defaults to null.
* `class_type` Optional value that will be added to the ajax request as a query string parameter.
* `render_html` This will render your results returned under ['html'].
* `render_html` This will render your results returned under ['html'].
* `render_selection` This will render your selection returned under ['html'].

The url of the remote query can be given by either of two ways: `remote_route` is the Symfony route.
`remote_params` can be optionally specified to provide parameters. Alternatively, `remote_path` can be used to specify
Expand Down
7 changes: 6 additions & 1 deletion Resources/public/js/select2entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
prefix = Date.now(),
query_parameters = $s2.data('query-parameters'),
render_html = $s2.data('render-html'),
render_selection = $s2.data('render-selection'),
cache = [];

let reqParams = $s2.data('req_params');
Expand Down Expand Up @@ -130,7 +131,11 @@
return option.html ? option.html : option.text;
},
templateSelection: function (option) {
return option.text;
if (render_selection) {
return option.html ? option.html : option.text;
} else {
return option.text;
}
}
}, mergedOptions);
}
Expand Down
4 changes: 4 additions & 0 deletions Resources/views/Form/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
{% set attr = attr|merge({'data-render-html': 'true'}) %}
{% endif %}

{% if render_selection %}
{% set attr = attr|merge({'data-render-selection': 'true'}) %}
{% endif %}

{% if class_type %}
{% set attr = attr|merge({'data-classtype': class_type}) %}
{% endif %}
Expand Down