【问题标题】:How to stop the hiding of dropdown content in specific situation in materialize?如何在具体情况下停止隐藏下拉内容?
【发布时间】:2021-08-03 10:41:39
【问题描述】:

我用物化库做了一个下拉菜单。它包含项目列表。现在我想在所有项目的底部查看更多链接。该链接已成功添加,但问题是当我单击该链接下拉/选择时会关闭。

const select = document.querySelector("select");

const fillSelect = (newOptions) => {
   const options = select.querySelectorAll("option");
   options.forEach((x) => x.remove());
   newOptions.forEach((opt) => {
      
      const optionElement = document.createElement("option");
      optionElement.innerHTML = opt;
      select.appendChild(optionElement);
   });
   updateSelect();
};

const addViewMoreLink = () => {
  const link = document.createElement('div');
  link.innerHTML = 'View more';
  link.addEventListener('click', e => {
    e.preventDefault();
    e.stopPropagation();
    return false;
  })
  const dropdownContent = select.parentNode.querySelector(".dropdown-content");
  dropdownContent.appendChild(link);
}


const updateSelect = () => {
  window.M.FormSelect.init(select, {});
  
  
}

window.onload = function(){
  fillSelect(["Option 1", "Option 2", "Option 3"]);
  addViewMoreLink()
}
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

    <!-- Compiled and minified JavaScript -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

<select id="organization-input">
</select>

我希望当我单击更多视图时,它应该关闭选择/下拉内容并显示新选项。添加新选项不是问题。目前的问题是,即使我使用了 stop e.stopPropagation();

,它也会关闭

【问题讨论】:

  • 我确信这可以在本地完成,还没有时间解决问题,但您可以尝试使用 onCloseEnd,我可能稍后会继续查看,因为我不是确定 css hack 是最好的方法jsfiddle.net/bxd18zse/1
  • “我希望当我点击更多视图时它应该关闭选择/下拉内容并显示新选项。” - 你可以使用 2 个选择吗?
  • @Maheer Ali 您能否更具体地说明您要实现的用户体验行为?如果用户单击“查看更多”,选择下拉菜单是否应该保持打开但选项会改变?还是应该是一个截断的列表,点击后会展开为完整列表?
  • @maqam7 当用户点击查看更多时,UI 选项应该会改变。应添加更多选项,下拉菜单应保持打开状态。

标签: javascript html css materialize


【解决方案1】:
<style type="text/css">
  .classic-dropdown .dropdown-content.select-dropdown {
    pointer-events: none;
  }
  .classic-dropdown .dropdown-content.select-dropdown li {
    pointer-events: all;
  }
</style>
<div class="classic-dropdown">
  <select id="organization-input">
  </select>
</div>

<script type="text/javascript">
const select = document.querySelector("select");

const fillSelect = (newOptions) => {
    const options = select.querySelectorAll("option");
    options.forEach((x) => x.remove());
    newOptions.forEach((opt) => {

        const optionElement = document.createElement("option");
        optionElement.innerHTML = opt;
        select.appendChild(optionElement);
    });
    updateSelect();
};

const addViewMoreLink = (e) => {
    const link = document.createElement('div');
    link.innerHTML = 'View more';
    link.addEventListener('click', e => {
        e.preventDefault();
        e.stopPropagation();
        return false;
    })
    const dropdownContent = select.parentNode.querySelector(".dropdown-content");
    dropdownContent.appendChild(link);
}


const updateSelect = () => {
    window.M.FormSelect.init(select, {});
}

window.onload = function() {
    fillSelect(["Option 1", "Option 2", "Option 3"]);
    addViewMoreLink()
}

document.getElementsByClassName('dropdown-content')[0]
</script>

【讨论】:

  • css 中仅包含 2 个属性 :)
  • 感谢您的回答。它正在部分工作。但这会导致另一个问题。当我单击下拉列表之外的某个位置时,它不会隐藏下拉列表。当我点击它外部时,我想隐藏它。
  • @MaheerAli 您的代码原样,在 chrome 上,当您在下拉列表之外单击时,它不会隐藏下拉列表:)
  • @MharisZafar 代码应该包含对您如何做事的解释,不鼓励只回答代码。请编辑您的答案并说明您采取了哪些措施来解决问题。
【解决方案2】:

正如您所提到的,您希望在单击查看更多并显示新选项时关闭下拉菜单。所以当你点击它时它会被关闭是很好的。现在您需要做的就是添加新选项,然后在dropdown-trigger 上触发一个点击事件,这样它就会再次打开并显示新选项。单击外部时下拉菜单未关闭的原因是正文高度等于其内容高度(下拉容器),因此您实际上并未单击正文。这就是为什么我在 css 中的 body 上设置了min-height

const select = document.querySelector("select");

const fillSelect = (newOptions) => {
   const options = select.querySelectorAll("option");
   // options.forEach((x) => x.remove());
   newOptions.forEach((opt) => {
      const optionElement = document.createElement("option");
      optionElement.innerHTML = opt;
      select.appendChild(optionElement);
   });
   updateSelect();
};

const addViewMoreLink = () => {
  const link = document.createElement('div');
  link.innerHTML = 'View more';
  link.addEventListener('click', e => {
    fillSelect(["Option 4", "Option 5", "Option 6"]);
    document.getElementsByClassName('dropdown-trigger')[0].click();
  });
  const dropdownContent = select.parentNode.querySelector(".dropdown-content");
  dropdownContent.appendChild(link);
}


const updateSelect = () => {
  const instances = window.M.FormSelect.init(select, {});
}

document.addEventListener('DOMContentLoaded', function() {
  fillSelect(["Option 1", "Option 2", "Option 3"]);
  addViewMoreLink();
});
body {
  min-height: 100vh;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

<select id="organization-input"></select>

【讨论】:

  • 新选项并不意味着之前的选项会消失。单击查看更多时,它将显示所有 6 个选项。
  • @MaheerAli 那么为什么要在 fillSelect 函数中删除它们?
猜你喜欢
  • 2015-10-24
  • 2018-04-18
  • 2021-09-12
  • 2018-11-19
  • 2021-06-10
  • 1970-01-01
  • 2020-11-02
  • 2016-11-11
  • 1970-01-01
相关资源
最近更新 更多