AMP
  • websites

SeatMap Multiple Selection

Introduction

This sample allows to multiple select seats in a seatmap with one click: this is useful when an user wants to book adjacent seats without clicking multiple times; go here for the basic seatmap sample.

Setup

Additionally used AMP components must be imported in the header: amp-pan-zoom for making the seatmap zoomable and scrollable

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

amp-bind for enabling seat selection

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

amp-list for showing a list of seats

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

amp-mustache for rendering the amp-list content

<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>

amp-selector for selecting seats on the seatmap

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

Seatmap state

Here we are using an amp-state called seatMultiSelection to keep all the useful information for the seat selection.

  • we load a list of neighbours seats from the server
  • selectedSeats contains a list of selected seat ids
  • numberOfSeats contains the number of seats we want automatically select
<amp-state id="seatMultiSelection"
  src="https://amp.dev/static/samples/json/seatmap-neighbours.json">
  <script type="application/json">
    {
      "selectedSeats": [],
      "numberOfSeats": 3
    }
  </script>
</amp-state>

Seatmap with multiple selection

Everytime a seat is selected, we automatically select the 2 seats on the right of it. In case the number of seats on the right of the selected one are less than 2, we only select the remaining on the line.

Seat map

<div class="seatmap-container">
  <h1>Seat map</h1>
  <amp-list layout="fill"
    src="https://amp.dev/static/samples/json/seats.json"
    binding="refresh"
    items="."
    single-item
    noloading>
    <template type="amp-mustache">
      <amp-pan-zoom layout="fill"
        class="seatmap">
        <amp-selector on="select:AMP.setState({
            seatMultiSelection: {
              selectedSeats: event.selectedOptions.concat(
                            seatMultiSelection.neighbours[event.selectedOptions[0]].slice(0, seatMultiSelection.numberOfSeats-1))
              }
            })"
          layout="fill">
          <div class="svg-container">
            <svg preserveAspectRatio="xMidYMin slice"
              viewBox="0 0 {{width}} {{height}}">
              {{#seats}}
                <rect option="{{id}}"
                  role="button"
                  tabindex="0"
                  class="seat {{unavailable}}"
                  [class]="seatMultiSelection.selectedSeats.indexOf('{{id}}') != -1 ? 'selected seat {{unavailable}}' : 'seat {{unavailable}}'"
                  x="{{x}}"
                  y="{{y}}"
                  width="{{width}}"
                  height="{{height}}"
                  rx="{{rx}}"
                  ry="{{ry}}">
                </rect>
              {{/seats}}
            </svg>
          </div>
        </amp-selector>
      </amp-pan-zoom>
    </template>
    <div placeholder>
      <svg class="disabled"
        viewBox="0 0  "
        preserveAspectRatio="xMidYMin slice">
        
      </svg>
    </div>
  </amp-list>
</div>
需要进一步说明?

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

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

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

在 GitHub 上编辑实例