简体   繁体   English

如何将 Mapbox 商店定位器列表更改为下拉列表并触发 href 链接

[英]How to change Mapbox store locator list to the dropdown and trigger href link

need some advice or help with Mapbox original sample JS code, how to make from this peace of code dropdown instead of listings?需要一些关于 Mapbox 原始示例 JS 代码的建议或帮助,如何从这个和平的代码下拉列表而不是列表中制作?

The idea is: change Mapbox store locator listings to the dropdown.这个想法是:将 Mapbox 商店定位器列表更改为下拉列表。 Any help would be nice, thanks!任何帮助都会很好,谢谢!

I change some parts of code, but inside the dropdown is link I need to trigger this href link for map marker action, and here I need some help..我更改了代码的某些部分,但在下拉列表中是链接我需要为 map 标记操作触发此 href 链接,在这里我需要一些帮助..

I have this: (JS)我有这个:(JS)

/**
   * Add a listing for each store to the sidebar.
  **/
  function buildLocationList(data) {
    data.features.forEach(function(store, i){
      /**
       * Create a shortcut for `store.properties`,
       * which will be used several times below.
      **/
      var prop = store.properties;

      /* Add a new listing section to the sidebar. */
      var listings = document.getElementById('listings');
      var listing = listings.appendChild(document.createElement('option'));
      /* Assign a unique `id` to the listing. */
      listing.id = "listing-" + prop.id;
      /* Assign the `item` class to each listing for styling. */
      listing.className = 'item';

      /* Add the link to the individual listing created above. */
      var link = listing.appendChild(document.createElement('a'));
      link.href = '#';
      link.className = 'title';
      link.id = "link-" + prop.id;
      link.innerHTML = prop.text;

      /* Add details to the individual listing. */
      var details = listing.appendChild(document.createElement('span'));
      details.innerHTML = prop.text;
      details.innerHTML = prop.address;

      /**
       * Listen to the element and when it is clicked, do four things:
       * 1. Update the `currentFeature` to the store associated with the clicked link
       * 2. Fly to the point
       * 3. Close all other popups and display popup for clicked store
       * 4. Highlight listing in sidebar (and remove highlight for all other listings)
      **/
      link.addEventListener('click', function(e){
        for (var i=0; i < data.features.length; i++) {
          if (this.id === "link-" + data.features[i].properties.id) {
            var clickedListing = data.features[i];
            flyToStore(clickedListing);
            createPopUp(clickedListing);
          }
        }
        var activeItem = document.getElementsByClassName('active');
        if (activeItem[0]) {
          activeItem[0].classList.remove('active');
        }
        this.parentNode.classList.add('active');
      });
    });
  } 

here is: (html)这里是:(html)

<div class='sidebar'>
  <select id='listings' class='listings'></select>
</div>

this is html after js do the magic:这是 js 施展魔法后的 html:

<div class="sidebar">
  <select id="listings" class="listings">
<option id="listing-0" class="item"><a href="#" class="title" id="link-0">store1</a><span>Address here</span></option>

........ here is more options .......
</div>

need trigger <a href="#" class="title" id="link-0">store1</a> when dropdown selected?!选择下拉菜单时需要触发<a href="#" class="title" id="link-0">store1</a>吗?!

You can't have a link inside a select option.您不能在 select 选项中包含链接。 What you can do is write the link as data attribute to the options.您可以做的是将链接作为数据属性写入选项。 As the example doesn't work as Stack Snippet, here's a Fiddle .由于该示例不能用作 Stack Snippet,因此这里有一个Fiddle

$("select").on("change", function() {
  window.open($(this).find("option:selected").attr("data-link"), '_blank');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="sidebar">
  <select id="listings" class="listings">
    <option value="0">Select store</option>
    <option id="listing-0" class="item" data-link="https://www.google.com">store1 Address here</option>
    <option id="listing-1" class="item" data-link="https://www.stackoverflow.com">store2 Address here</option>
  </select>
</div>

here is the final code that works for me as I want it.这是我想要的最终代码。 hope its help someone to solve a similar problem;;))希望它可以帮助某人解决类似的问题;;))

a part of code from Mapbox JS I customize:我自定义的 Mapbox JS 的一部分代码:

/**
   * Add a listing for each store to the sidebar.
  **/
 function buildLocationList(data) {
    data.features.forEach(function(store, i){
      /**
       * Create a shortcut for `store.properties`,
       * which will be used several times below.
      **/
      var prop = store.properties;

      /* Add a new listing section to the sidebar. */
      var listings = document.getElementById('listings');
      var listing = listings.appendChild(document.createElement('span'));
      /* Assign a unique `id` to the listing. */
      listing.id = "listing-" + prop.id;
      /* Assign the `item` class to each listing for styling. */
      listing.className = 'item';

      /* Add the link to the individual listing created above. */
      var link = listings.appendChild(document.createElement('option'));
      link.href = '#';
      link.className = 'title';
      link.id = "link-" + prop.id;
      link.innerHTML = prop.address;

      document.getElementById('listings').addEventListener('change',function(){
            $(this).find('span').attr("data-link");
        });

      /**
       * Listen to the element and when it is clicked, do four things:
       * 1. Update the `currentFeature` to the store associated with the clicked link
       * 2. Fly to the point
       * 3. Close all other popups and display popup for clicked store
       * 4. Highlight listing in sidebar (and remove highlight for all other listings)
      **/
      link.addEventListener('click', function(e){
        for (var i=0; i < data.features.length; i++) {
          if (this.id === "link-" + data.features[i].properties.id) {
            var clickedListing = data.features[i];
            flyToStore(clickedListing);
            createPopUp(clickedListing);
          }
        }
        var activeItem = document.getElementsByClassName('active');
        if (activeItem[0]) {
          activeItem[0].classList.remove('active');
        }
        this.parentNode.classList.add('active');
      });
    });
  }

And here is the part of HTML for dropdown:这是 HTML 的下拉部分:

 <fieldset>
  <select id='listings' class='listings' name="some_txt_here" >
  <option selected>---select your place--</option>
</select>
  <label class="bars-txt" for="city">
    <span data-text="some_txt_here">some_txt_here</span>
  </label>
</fieldset>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM