vue.js寫個選擇多條數據的和查詢功能

效果圖如下:

需求是(其中我用了mui的css樣式):

1、點擊input框後選擇審覈人列表菜單過渡從下往上出現;
2、選擇人後點擊確定按鈕,選擇的人出現在input框中;
3、點擊空白的地方則關閉選擇審覈人窗口
4、選擇審覈人列表菜單中有查詢功能
實例一 實例一
Computer $1600

代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <link rel="stylesheet" href="css/mui.min.css">
    <style>
        /*,蒙版*/
        .overlay{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            z-index: 1000;
            background: rgba(0, 0, 0, .5);
        }
     .checklist{
         -webkit-transition: .3s;
         position: fixed;
         bottom: 0;
         left: 0;
         z-index: 2000;
         width: 100%;
         background-color: #fff;
     }
        .wrap {
            display: -webkit-flex;
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 45px;
            font-size: 16px;
            padding: 0 13px;
            border-bottom: 1px solid rgb(217, 217, 217);
        }
        .cancel{
            padding: 5px 10px;
            font-size: 12px;
            font-weight: 400;
            display: inline-block;
            cursor: pointer;
            text-align: center;
            color: #333;
            border: 1px solid #ccc;
            border-radius: 3px;
            background-color: #fff;
        }
        .confirm{
            padding: 5px 10px;
            font-size: 12px;
            font-weight: 400;
            display: inline-block;
            cursor: pointer;
            text-align: center;
            border-radius: 3px;
            color: #fff;
            border: 1px solid #007aff;
            background-color: #007aff;
        }
       .list{
           width: 100%;
           height: 230px;
           overflow-x: hidden;
           overflow-y: scroll;
       }
        .list .line {
            display: -webkit-flex;
            display: flex;
            justify-content: center;  /*周圍留有空白*/
            align-items: center;
            height: 30px;
        }
        .list .line .lines{
            display: -webkit-flex;
            display: flex;
            flex-direction: column;
            justify-content: center; /*周圍留有空白*/
            align-items: flex-start;
            width: 90%;
        }
        /*過渡樣式*/
        .fade-enter-active, .fade-leave-active {
            -webkit-transform: translateY(0px);
        }
        .fade-enter, .fade-leave-active {
            -webkit-transform: translateY(300px);
        }
            /*搜索框樣式*/
        .mui-search .mui-placeholder{
            line-height: 30px;
        }
        .row{
            overflow: visible !important;
            position: relative;
            left: 30%;
            width: 60%;
            transform: translateX(-50%);
            height: 30px;
            display: inline-block;
        }
        input[type=search]{
            height: 30px;
            padding-left: 30px;
        }
        .mui-search input[type=search]{
            padding-right: 0px;
        }
         .title{
            width: 110%;
        }
    </style>
</head>
<body>
<div id="app">
    <p>{{ message }}</p>
    <input type="text" @click="btn($event)" v-model="peoPle">
     <!--過渡的動畫-->
     <transition name="fade">
         <div class="checklist" v-if="visible" ref="main">
             <div class="wrap">
                 <button class="cancel" @click="onhide">取消</button>
                 <div class="mui-input-row row">
                     <div>
                         <span class="mui-icon mui-icon-search" style="position: absolute; top: 9%; left: 2%;"></span>
                     </div>
                     <input v-model="search" type="search" placeholder="" class="int mui-input-clear">
                 </div>
               <!--  <span class="title">選擇審覈人</span>-->
                 <button class="confirm" @click="onConfirm">完成</button>
             </div>
             <div class="list">
                 <div class="line" v-for="item in list">
                     <div class="lines">
                         <label :for="item.name" class="title">{{item.name}}</label>
                     </div>
                     <div>
                         <input :id="item.name" :value="item.name" type="checkbox" v-model="checkedNames">
                     </div>
                 </div>
             </div>
         </div>
     </transition>
     <!--蒙層-->
     <div class="overlay" v-if="visible"></div>
</div>
<script>
  var vm = new Vue({
        el: '#app',
      //屏幕以外的點擊
        data: {
            message: 'Hello Vue.js!',
            visible:false,
            info:[
                {
                    'name':'智子'
                },
                {
                    'name':'王靜雲'
                },
                {
                    'name':'王雲'
                },
                {
                    'name':'張三'
                },
                {
                    'name':'李四'
                },
                {
                    'name':'王五'
                },
                {
                    'name':'趙璐'
                },
                {
                    'name':'李文'
                },
                {
                    'name':'海翔'
                }
            ], //數據
            peoPle:'',
            search:'', //查詢
            checkedNames:[]
        },
      //查詢功能
      //  computed比methods效率高,不需要重新渲染頁面
      computed:{
          list:function(){
              const arr =[];
              for(let i=0;i<this.info.length;i++){
                  //   console.log(that.info.length)
                  if(this.info[i].name.indexOf(this.search)!=-1){
                      arr.push(this.info[i])
                  }
              }
              return arr;
          }
      },
        methods:{
            /*點擊input*/
            btn (event) {
                //阻止冒泡
                event || (event = window.event);

                event.stopPropagation ? event.stopPropagation() : (event.cancelBubble = true);
                this.visible ? this.hide() : this.show()
            },
            show () {
                this.visible = true;
                document.addEventListener('click', this.hidePanel, false)
            },
            hide () {
                this.visible = false;
                document.removeEventListener('click', this.hidePanel, false)
            },

            hidePanel (e) {
                if (this.$refs.main && !this.$refs.main.contains(e.target)) {//點擊除彈出層外的空白區域
                    this.hide()
                }
            },
            /*取消*/
            onhide(){
                this.visible = false;
            },
            /*完成*/
            onConfirm(){
                this.visible = false;
                this.peoPle= this.checkedNames
            }
        }
    })
</script>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章