鍍金池/ 問答/HTML/ elementUI中的Table組件數(shù)據(jù)變化后表格不會重新渲染問題怎么解決?

elementUI中的Table組件數(shù)據(jù)變化后表格不會重新渲染問題怎么解決?

當(dāng)排序時候 調(diào)用sortchange鉤子函數(shù),然后會觸發(fā)initsort方法,我本想讓第一行不出現(xiàn)上移的圖片(強制把排序后的圖片更新,沒成功),但是table的數(shù)據(jù)變化了,但是頁面沒有變化!請問為什么會這樣?

<template>
  <div>

    <!--音頻分集-->
    <div class="brand">
      <el-breadcrumb class="brand">
        <el-breadcrumb-item>系統(tǒng)首頁</el-breadcrumb-item>
        <el-breadcrumb-item>音頻分集</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="pad">

      <div>
        <el-container>
          <el-header>
            <i class="icon iconfont icon-icon--"></i>
            <span>分集列表(3集)</span>
          </el-header>
          <el-main>
            <el-table
              :data="tableData"
              @sort-change = 'sortchange'

              style="width: 100%">
              <el-table-column
                prop="id"
                label="編號"
                sortable
                width="220%"
              >
              </el-table-column>

              <el-table-column
                prop="audioName"
                label="音頻名稱"
                sortable
                width="220%">
              </el-table-column>

              <el-table-column
                prop="format"
                label="格式"
                width="220%">
              </el-table-column>

              <el-table-column
                prop="format"
                label="格式"
                width="220%">
              </el-table-column>

              <el-table-column
                prop="quantity"
                label="閱讀量"
                sortable
                width="220%">
              </el-table-column>

              <el-table-column prop="sort" label="排序" width="220%">
                <template slot-scope="scope">
                  <img v-show="scope.row.downimg" :src="scope.row.downimg" width="12%">
                  <img v-show="scope.row.upimg" :src="scope.row.upimg" width="12%">
                </template>
              </el-table-column>

            </el-table>
          </el-main>
        </el-container>

      </div>
    </div>
  </div>
</template>

<script>
  import Vue from 'vue'
  import downimg from '@/assets/xiayi.png'
  import upimg from '@/assets/shangyi.png'
 

  export default {
    name: "video-diversity",
    created(){
     this.initsort()
    },
    data() {
      return {
        audioname: '',
        tableData: [
          {
          id: '100001',
          audioName: '001-走向中考考場·七年級道德與法治',
          format: 'MP3',
          size: '10M',
          quantity: 23434,
          downimg: downimg,
          upimg: upimg,
        }, {
          id: '100002',
          audioName: '003-走向中考考場·七年級道德與法治',
          format: 'MP3',
          size: '10M',
          quantity: 4534,
          downimg: downimg,
          upimg: upimg,
        }, {
          id: '100003',
          audioName: '005-走向中考考場·七年級道德與法治',
          format: 'MP3',
          size: '10M',
          quantity: 45340,
          downimg: downimg,
          upimg: upimg,
        }
        ],
      }
    },
    methods: {
      search() {
        console.log(this.audioname);
      },
      sortchange(obj){
        this.initsort()
      },
      initsort(){
        Vue.set(this.tableData[0], 'upimg', '')
        Vue.set(this.tableData[this.tableData.length-1], 'downimg', '')

           this.tableData = this.tableData.filter(function (item) {
          console.log(item);
          return item
        })

      },
      downclick(a,b,c){
        console.log(a);

      },
      upclick(){

      }
    },
    watch:{
      'tableData':function (newval,oldval) {
        console.log(newval);
      },
    },
    beforeUpdate(){
      // debugger
      Vue.set(this.tableData[0], 'upimg', '')
      Vue.set(this.tableData[this.tableData.length-1], 'downimg', '')
    }

  }
</script>



  
  

clipboard.png

回答
編輯回答
貓小柒

vue 數(shù)組更新檢測
不好意思中午掃了一眼你的問題感覺可能是數(shù)組更新沒有檢測到的原因就強答了,剛才實際測試了一下,實際上你這樣寫是可以觸發(fā)更新的,具體你不行的原因因為代碼不全沒有辦法分析

附上jsfiddle測試地址

看過你的代碼了,你只是啟用sortable并沒有把sortable設(shè)置為custom啟用自定義排序,這個情況下其實el-table并沒有改變你tableData的排序順序,所以這個樣子當(dāng)然實現(xiàn)不了頭尾隱藏的效果

這樣給你提供一種比較簡單的實現(xiàn)思路,直接通過當(dāng)前列的index判斷是否是第一行或最后一行就行了

<el-table-column prop="sort" label="排序" width="220%">
  <template slot-scope="{row, $index}">
    <el-button v-show="!!$index" :icon="downimg"></el-button>
    <el-button v-show="$index < tableData.length - 1" :icon="upimg"></el-button>
    <!--
     <img v-show="!!$index" :src="downimg" width="12%">
     <img v-show="$index < tableData.length - 1" :src="upimg" width="12%">
     -->
  </template>
</el-table-column>

這個實現(xiàn)方式可以把downimgupimg放在data里面,不用每個數(shù)據(jù)都賦值,更科學(xué),節(jié)約內(nèi)存

data() {
  return {
    downimg: downimg,
    upimg: upimg,
    audioname: '',
    tableData: [
2017年7月22日 09:35