如何在使用choices库时设置下拉框的默认选项?

huangapple go评论104阅读模式
英文:

How can I set the default option of dropdown while using choices library?

问题

我正在使用Choices库来创建下拉菜单。我需要做的是在页面重新加载之前保留下拉菜单的选定值并显示它,然后再提交。目前,我能够使用sessionStorage来存储该值。但是,在页面重新加载后,如何将其显示在choices下拉菜单中作为默认选项呢?我已阅读了文档,但无法弄清如何传递默认值。

document.querySelectorAll('.myDropdown').forEach(selectBox => {
    choicesElements = new Choices(selectBox, { addItemText: ['Yes'], sortFn: (a, b) => a < b });
    selectBox.addEventListener('change', () => {
       // 用于填充choices的代码
    });
});

let marks_dropdown = document.querySelector('.myDropdown');
marks_dropdown.addEventListener("change",function() {
    var choices_item_selectable = document.querySelector('.choices__item.choices__item--selectable');
    storeSelectedItem(choices_item_selectable.innerText);
});

function storeSelectedItem(innertext) {
    sessionStorage.setItem('innertext', innertext);
}

let innertext = sessionStorage.getItem('innertext');
if (innertext) {
    let new_choices_item_selectable = document.querySelector('.choices__item.choices__item--selectable');
    new_choices_item_selectable.innerText = innertext;
}

这是您提供的代码的翻译部分。

英文:

I am using Choices library for dropdown. What I need to do is to retain the selected value of dropdown and show it as soon as the page reloads before submitting. Right now I am able to store the value using sessionStorage. But how can I show it in the choices dropdown as the default option once the page reloads? I read the documentation but not able to figure out how to pass the default value.

document.querySelectorAll(&#39;.myDropdown&#39;).forEach(selectBox =&gt; {
    choicesElements = new Choices(selectBox, { addItemText: [&#39;Yes&#39;], sortFn: (a, b) =&gt; a &lt; b  } );
    selectBox.addEventListener(&#39;change&#39;, () =&gt; {
       // code to populate choices
    }
}
let marks_dropdown = document.querySelector(&#39;.myDropdown&#39;);
marks_dropdown_id.addEventListener(&quot;change&quot;,function() {
    var choices_item_selectable = document.querySelector(&#39;.choices__item.choices__item--selectable&#39;)
    storeSelectedItem(choices_item_selectable.innerText);
}

function storeSelectedItem(innertext) {
    sessionStorage.setItem(&#39;innertext&#39;, innertext);
}

let innertext = sessionStorage.getItem(&#39;innertext&#39;);
if (innertext) {
    let new_choices_item_selectable = document.querySelector(&#39;.choices__item.choices__item--selectable&#39;);
    new_choices_item_selectable.innerText = innertext;
}

答案1

得分: 0

我已经解决了以下问题。

assignChoicesElements = () => {
  document.querySelectorAll('.myDropdown').forEach(selectBox => {
    choicesElements['some-key'] = new Choices(selectBox, { sortFn: (a, b) => a < b });
    selectBox.addEventListener('change', () => {
        let reqdValue = selectBox.value;
        if(reqdValue != '') {
            storeSelectedItem(reqdValue);
        }
    });
    let elementsObject = {};
    document.querySelectorAll('unMarked').forEach(unmarkedElement => { 
       elementsObject['some_key'] = //whatever value to be stored;
    });
    choicesElements['some-key'].clearStore();
    choicesElements['some-key'].setChoices(getPossibleCombinations(elementsObject), 'value', 'label', false);
  });
};

getPossibleCombinations = (jsonObject) => {
  var possibleCombinations = {}
  Object.entries(jsonObject).forEach(([key, value]) => {
    var newPossibleCombinations = possibleCombinations
    Object.entries(possibleCombinations).forEach(([key, value]) => {
      let newValue = value
      newPossibleCombinations['some-value'] = newValue
    })
    newPossibleCombinations['key'] = ['some-value']
    possibleCombinations = newPossibleCombinations
  })
  var formatedPossibleCombinations = []
  Object.entries(possibleCombinations).forEach(([key, value]) => {
   // 这里有变化。获取存储的值,在创建值列表时,如果在sessionStorage中找到该值,则将selected: true添加到该值。
    let sessionStorageValue = sessionStorage.getItem('stored_item')
    if (sessionStorageValue) {
      formatedPossibleCombinations.push({ label: key, value: value, selected: true })
    }
  })
  return formatedPossibleCombinations
}

function storeSelectedItem(value) {
  sessionStorage.clear();
  sessionStorage.setItem('stored_item', value);
}

这段代码超出了问题的要求,但我添加了它以防有人找到它有用。

英文:

I solved the issue as below.

assignChoicesElements = () =&gt; {
document.querySelectorAll(&#39;.myDropdown&#39;).forEach(selectBox =&gt; {
choicesElements[&#39;some-key&#39;] = new Choices(selectBox, { sortFn: (a, b) =&gt; a &lt; b });
selectBox.addEventListener(&#39;change&#39;, () =&gt; {
let reqdValue = selectBox.value;
if(reqdValue != &#39;&#39;) {
storeSelectedItem(reqdValue);
}
});
let elementsObject = {};
document.querySelectorAll(&#39;unMarked&#39;).forEach(unmarkedElement =&gt; { 
elementsObject[&#39;some_key&#39;] = //whatever value to be stored;
});
choicesElements[&#39;some-key&#39;].clearStore();
choicesElements[&#39;some-key&#39;].setChoices(getPossibleCombinations(elementsObject), &#39;value&#39;, &#39;label&#39;, false);
};
getPossibleCombinations = (jsonObject) =&gt; {
var possibleCombinations = {}
Object.entries(jsonObject).forEach(([key, value]) =&gt; {
var newPossibleCombinations = possibleCombinations
Object.entries(possibleCombinations).forEach(([key, value]) =&gt; {
let newValue = value
newPossibleCombinations[&#39;some-value&#39;] = newValue
})
newPossibleCombinations[&#39;key&#39;] = [&#39;some-value&#39;]
possibleCombinations = newPossibleCombinations
})
var formatedPossibleCombinations = []
Object.entries(possibleCombinations).forEach(([key, value]) =&gt; {
// Here is the change. Get the stored value and while creating the list of values, add selected: true to the value if it is found in sessionStorage.
let sessionStorageValue = sessionStorage.getItem(&#39;stored_item&#39;)
if (sessionStorageValue) {
formatedPossibleCombinations.push({ label: key, value: value, selected: true })
}
})
return formatedPossibleCombinations
}
function storeSelectedItem(value) {
sessionStorage.clear();
sessionStorage.setItem(&#39;stored_item&#39;, value);
}

This code is more than required for the question. But I have added it just in case if anyone finds it useful.

huangapple
  • 本文由 发表于 2020年1月6日 18:36:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定