vue-品牌管理案例

品牌管理

分析

  1. 獲取到 id 和 name ,直接從 data 上面獲取
  2. 組織出一個對象
  3. 把這個對象,調用 數組的 相關方法,添加到 當前 data 上的 list 中
  4. 注意:在Vue中,已經實現了數據的雙向綁定,每當我們修改了 data 中的數據,Vue會默認監聽到數據的改動,自動把最新的數據,應用到頁面上;
div id="app">



    <div class="panel panel-primary">
      <div class="panel-heading">
        <h3 class="panel-title">添加品牌</h3>
      </div>
      <div class="panel-body form-inline">
        <label>
          Id:
          <input type="text" class="form-control" v-model="id">
        </label>

        <label>
          Name:
          <input type="text" class="form-control" v-model="name">
        </label>

        <!-- 在Vue中,使用事件綁定機制,爲元素指定處理函數的時候,如果加了小括號,就可以給函數傳參了 -->
        <input type="button" value="添加" class="btn btn-primary" @click="add()">

        <label>
          搜索名稱關鍵字:
          <input type="text" class="form-control" v-model="keywords">
        </label>
      </div>
    </div>



    <table class="table table-bordered table-hover table-striped">
      <thead>
        <tr>
          <th>Id</th>
          <th>Name</th>
          <th>Ctime</th>
          <th>Operation</th>
        </tr>
      </thead>
      <tbody>
        <!-- 之前, v-for 中的數據,都是直接從 data 上的list中直接渲染過來的 -->
        <!-- 現在, 我們自定義了一個 search 方法,同時,把 所有的關鍵字,通過傳參的形式,傳遞給了 search 方法 -->
        <!-- 在 search 方法內部,通過 執行 for 循環, 把所有符合 搜索關鍵字的數據,保存到 一個新數組中,返回 -->
        <tr v-for="item in search(keywords)" :key="item.id">
          <td>{{ item.id }}</td>
          <td v-text="item.name"></td>
          <td>{{ item.ctime }}</td>
          <td>
            <a href="" @click.prevent="del(item.id)">刪除</a>
          </td>
        </tr>
      </tbody>
    </table>



  </div>

注:用了bootstrap

var vm = new Vue({
      el: '#app',
      data: {
        id: '',
        name: '',
        keywords: '', // 搜索的關鍵字
        list: [
          { id: 1, name: '奔馳', ctime: new Date() },
          { id: 2, name: '寶馬', ctime: new Date() }
        ]
      },
    methods: {
        add() { 
          var car = { id: this.id, name: this.name, ctime: new Date() }
          this.list.push(car)
          this.id = this.name = ''
        },
        del(id) { // 根據Id刪除數據
          // 分析:
          // 1. 如何根據Id,找到要刪除這一項的索引
          // 2. 如果找到索引了,直接調用 數組的 splice 方法

          /* this.list.some((item, i) => {
            if (item.id == id) {
              this.list.splice(i, 1)
              // 在 數組的 some 方法中,如果 return true,就會立即終止這個數組的後續循環
              return true;
            }
          }) */
          var index = this.list.findIndex(item => {
            if (item.id == id) {
              return true;
            }
          })

          // console.log(index)
          this.list.splice(index, 1)
        },
        search(keywords) { // 根據關鍵字,進行數據的搜索
          /* var newList = []
          this.list.forEach(item => {
            if (item.name.indexOf(keywords) != -1) {
              newList.push(item)
            }
          })
          return newList */

          // 注意:  forEach   some   filter   findIndex   這些都屬於數組的新方法,
          //  都會對數組中的每一項,進行遍歷,執行相關的操作;
          return this.list.filter(item => {
            // if(item.name.indexOf(keywords) != -1)

            // 注意 : ES6中,爲字符串提供了一個新方法,叫做  String.prototype.includes('要包含的字符串')
            //  如果包含,則返回 true ,否則返回 false
            //  contain
            //console.log(keywords);
            if (item.name.includes(keywords)) {
              return item
            }
          })
          }
    });
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章