Skip to content

Commit

Permalink
Fix check indexItem for select2 directive
Browse files Browse the repository at this point in the history
  • Loading branch information
volterra79 committed Feb 26, 2024
1 parent 8a85e56 commit fbf67ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/components/WMSLayersPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</label>
<select
id = "g3w-wms-layers"
multiple = "multiple"
:multiple = "true"
clear = "true"
v-select2 = "'selectedlayers'"
>
Expand Down
14 changes: 9 additions & 5 deletions src/directives/v-select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export default {
select2_value,
indexItem, /** @since 3.9.1 */
} = vnode.data.attrs || {};
const isArray = binding.value && Array.isArray(vnode.context[binding.value]); //check if is an array
const isArrayWithIndex = binding.value
&& Array.isArray(vnode.context[binding.value]) //check if is an array
&& undefined !== indexItem //check if indexItem is defined /** @since 3.10.0 **/
$(el)
.select2({
width: '100%',
Expand All @@ -24,19 +26,21 @@ export default {
})
.on('select2:select', (e) => {
if (binding.value) {
//get value
const value = e.params.data.id;
//check is can have multiple value
if (multiple && (
(isArray
(isArrayWithIndex
? vnode.context[binding.value][indexItem].value
: vnode.context[binding.value]
).filter(d => value === d).length === 0)
) {
(isArray
(isArrayWithIndex
? vnode.context[binding.value][indexItem].value
: vnode.context[binding.value]
).push(value);
} else {
if (isArray) {
if (isArrayWithIndex) {
vnode.context[binding.value][indexItem].value = value;
} else {
vnode.context[binding.value] = value;
Expand All @@ -46,7 +50,7 @@ export default {
})
.on('select2:unselect', (e) => {
if (binding.value && multiple) {
if (isArray) {
if (isArrayWithIndex) {
vnode.context[binding.value][indexItem].value = vnode.context[binding.value][indexItem].value.filter(d => e.params.data.id !== d);
} else {
vnode.context[binding.value] = vnode.context[binding.value].filter(d => e.params.data.id !== d);
Expand Down

0 comments on commit fbf67ff

Please sign in to comment.