Do you build things with AMP? Fill out the AMP Developer Survey!
AMP
  • websites

ComboBox

Introduction

This sample demonstrastes a classic dropdown combo, with a text field which filters the dropdown elements according to the user's input. The options are rendered via amp-autocomplete.

Setup

The amp-autocomplete component is used for suggesting according to the user's input.

<script async custom-element="amp-autocomplete" src="https://cdn.ampproject.org/v0/amp-autocomplete-0.1.js"></script>

Autocomplete input

First we need to add an amp-autocomplete element by giving it an input field and a data source. Here we're using an inline script declaration defining a list of cities in the US as items. For long lists of suggestions, it's possible to fetch a remote JSON endpoint using the src attribute.

We will use filter="token-prefix" as our way to match on the user input and set min-characters="0" so that the combo box will display options even when the input is empty.

Finally, the combobox can be triggered via a button that focuses the input field: <button on="tap:combo-input.focus">.

<form method="post"
  action-xhr="/documentation/examples/api/echo">
  <amp-autocomplete class="combo-box"
    filter="token-prefix"
    min-characters="0">
    <input id="combo-input"
      type="text">
    <button hidden
      id="close"
      on="tap:open.show, close.hide">
      <i class="arrow"></i>
    </button>
    <button id="open"
      on="tap:combo-input.focus, open.hide, close.show">
      <i class="arrow"></i>
    </button>
    <script type="application/json">
      {
        "items": [
          "Albany, New York",
          "Annapolis, Maryland",
          "Atlanta, Georgia",
          "Augusta, Maine",
          "Austin, Texas",
          "Baton Rouge, Louisiana",
          "Bismarck, North Dakota",
          "Boise, Idaho",
          "Boston, Massachusetts",
          "Carson City, Nevada",
          "Charleston, West Virginia",
          "Cheyenne, Wyoming",
          "Columbia, South Carolina",
          "Columbus, Ohio",
          "Concord, New Hampshire",
          "Denver, Colorado",
          "Des Moines, Iowa",
          "Dover, Delaware",
          "Frankfort, Kentucky",
          "Harrisburg, Pennsylvania",
          "Hartford, Connecticut",
          "Helena, Montana",
          "Honolulu, Hawaii",
          "Indianapolis, Indiana",
          "Jackson, Mississippi",
          "Jefferson City, Missouri",
          "Juneau, Alaska",
          "Lansing, Michigan",
          "Lincoln, Nebraska",
          "Little Rock, Arkansas",
          "Madison, Wisconsin",
          "Montgomery, Alabama",
          "Montpelier, Vermont",
          "Nashville, Tennessee",
          "Oklahoma City, Oklahoma",
          "Olympia, Washington",
          "Phoenix, Arizona",
          "Pierre, South Dakota",
          "Providence, Rhode Island",
          "Raleigh, North Carolina",
          "Richmond, Virginia",
          "Sacramento, California",
          "Saint Paul, Minnesota",
          "Salem, Oregon",
          "Salt Lake City, Utah",
          "Santa Fe, New Mexico",
          "Springfield, Illinois",
          "Tallahassee, Florida",
          "Topeka, Kansas",
          "Trenton, New Jersey"
        ]
      }
    </script>
  </amp-autocomplete>
</form>
需要进一步说明?

如果此页面的说明未能涵盖您的所有问题,欢迎与其他 AMP 用户取得联系,讨论您的具体用例。

前往 Stack Overflow
一项无法解释的功能?

AMP 项目强烈鼓励您参与其中并作出贡献!我们希望您能成为我们开源社区的持续参与者,但我们也欢迎您对热衷问题做出单次贡献。

在 GitHub 上编辑实例