Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app">
  <div v-if="inputText">
    您選擇的是「{{inputText}}」
  </div>
  <div v-else>
    請選擇:
  </div>
  <selector
    v-model="inputText"
    :data="recoData">
  </selector>
  
</div>
              
            
!

CSS

              
                .autocomplete-input {
  position: relative;
  width: 300px;
  cursor: pointer;
  * {
    box-sizing: border-box;
  }
  input {
    width: 100%;
    height: 25px;
  }
  .list {
    position: absolute;
    top: 25px;
    width: 100%;
    border: 1px solid #cccccc;
    cursor: pointer;
    .item {
      height: 25px;
      width: 100%;
    }
  }
  .triangle {
    position: absolute;
    top: 8px;
    right: 8px;
    height:0;
    width:0;
    overflow: hidden;
    font-size: 0;
    line-height: 0;
    border-color:#888888 transparent transparent transparent;
    border-style:solid dashed dashed dashed;
    border-width:8px;
  }
}


              
            
!

JS

              
                Vue.component('selector',  {
  template: `
  <div class="autocomplete-input">
    <input
      type="text"
      placeholder="可輸入英文字母查詢"
      v-model="childValue"
      @click="active">
    <div
      class="list"
      v-show="isShowRecoList">
      <div
        class="item"
        v-for="item in recoList"
        @click="selectRecoText(item)">
        {{item}}
      </div>
    </div>
    <div
      class="triangle"
      @click="active">
    </div>
  </div>
  `,
  props: ['data', 'value'],
  data () {
    return {
      childValue: '', // 子組件中的輸入文字
      isActive: false // 是否啟用提示文字
    }
  },
  computed: {
    // 子組件中同步value的值
    syncValue: {
      get () {
        return this.value
      },
      set (v) {
        this.$emit('input', v)
      }
    },
    // 是否顯示提示選項
    isShowRecoList () {
      if (this.isActive && this.recoList.length > 0) {
        return true
      } else {
        return false
      }
    },
    // 提示選項
    recoList () {
      return this.data.filter(d => {
        return d.indexOf(this.childValue) >= 0
      })
    }
  },
  methods: {
    // 選擇選項
    selectRecoText (text) {
      this.childValue = text
      // 選定後才將值更新回父層
      this.syncValue = this.childValue
      // 關閉提示選項
      this.isActive = false
    },
    // 啟用提示選單
    active () {
      if (this.isActive === false) {
        this.isActive = true
        this.childValue = ''
      }
    }
  }
})

const vm = new Vue({
  el: '#app',
  component: 'selector',
  data: {
    recoData: ['candy','car','card','can','bakery','boring','band','country','drink','eye','fork','game','hospital','orange','restroom','sleep','talk','tree','yesterday','weather','zoo'],
    inputText: ''
  }
})

              
            
!
999px

Console