• vue案例--实现搜索功能


    1、原型:

    2、实现搜索功能代码

    <template>
      <div class="safetyInfo">
        <strong>用户名:</strong><input type="text" name="" id="username" placeholder="用户名" v-model="username"/>
        <strong>性别:</strong>
        <select v-model="sex">
          <option>1</option>
          <option>0</option>
        </select>
    <!--    <input type="text" name="" id="sex" placeholder="性别" v-model="sex"/>-->
        <button @click="btn" class="btn btn-default">搜索</button> <!--@click="btn"这部分为vue语法-->
        <button @click="btn_cl" class="btn btn-default">清空</button> <!--@click="btn"这部分为vue语法-->
        <hr/>
        <table class="table table-bordered">
          <thead>
          <tr>
            <th>编号</th>
            <th>用户名</th>
            <th>电话</th>
            <th>年龄</th>
            <th>性别</th>
          </tr>
          </thead>
          <tbody>
          <tr v-for="item in searchData" :key="item.id">
            <td>{{item.id}}</td>
            <td>{{item.userName}}</td>
            <td>{{item.phoneNumber}}</td>
            <td>{{item.age}}</td>
            <td>{{item.sex}}</td>
          </tr>
          </tbody>
        </table>
      </div>
    </template>
    
    <script>
    export default {
      name: 'link',
      data () {
        return {
          username:'',
          sex:'',
          searchData: [],
          products:[
          ],
        }
      },
      //勾子
      methods:{
        btn_cl:function(){
          this.username = '';//用户名
          this.sex = '';//性别
        },
        btn:function(){
          var username = this.username;//用户名
          var sex = this.sex;//性别
          console.log(username)
          if( username != '' || sex != '' ) {
            console.log("带参搜索")
            this.$http.post('http://127.0.0.1:8081/getUserByParam',
              {
                userName: this.username,
                sex:this.sex
              }).then((response) => {
              this.products = [response.body.user]
              console.log(this.products)
              if(this.products[0]==null ) {alert("没有对应的数据")}
              console.log(this.products)
              if (true) {
                this.searchData = this.products.filter(function (product) {
                  console.log(product)
                  return Object.keys(product).some(function (key) {
                    console.log(key)
                    return String(product[key]).toLowerCase().indexOf(username) > -1
                  })
                })
              }
            }).catch(function (err) {
              console.log(err);
            })
          }else {
            this.$http.get('http://127.0.0.1:8081/getUser').then((response) => {
              console.log("无参搜索")
              this.products = response.body.user
              if (true) {
                console.log(this.products)
                this.searchData = this.products.filter(function (product) {
                  console.log(product)
                  return Object.keys(product).some(function (key) {
                    console.log(key)
                    return String(product[key]).toLowerCase().indexOf(username) > -1
                  })
                })
              }
            }).catch(function (err) {
              console.log(err);
            })
          }
        }
      }
    }
    </script>
  • 相关阅读:
    android WebView总结
    Java抓取网页数据(原网页+Javascript返回数据)
    Linux之旅(1): diff, patch和quilt (下)
    浅谈UML的概念和模型之UML九种图
    基于注解的Spring MVC
    Hibernate自增列保存失败的问题
    京东,你玩我?
    MySQL 通配符学习小结
    Java中怎样由枚举常量的ordinal值获得枚举常量对象
    HDU 4588 Count The Carries 计算二进制进位总数
  • 原文地址:https://www.cnblogs.com/ywjfx/p/12527187.html
Copyright © 2020-2023  润新知