vue.js “element-ui”table如何将空单元格更改为“-”,

xjreopfe  于 2023-11-21  发布在  Vue.js
关注(0)|答案(3)|浏览(158)
var Main = {
      data() {
        return {
          tableData: [{
            date: '2016-05-03',
            name: 'Tom',
            address: 'No. 189, Grove St, Los Angeles'
          }, {
            date: '2016-05-02',
            name: '',
            address: 'No. 189, Grove St, Los Angeles'
          }, {
            date: '2016-05-04',
            name: '',
            address: 'No. 189, Grove St, Los Angeles'
          }, {
            date: '2016-05-01',
            name: 'Tom',
            address: 'No. 189, Grove St, Los Angeles'
          }]
        }
      }
    }
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
@import url("//unpkg.com/[email protected]/lib/theme-chalk/index.css");
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/[email protected]/lib/index.js"></script>
<div id="app">
<template>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="Date" width="180">
      </el-table-column>
      <el-table-column prop="name" label="Name" width="180">
        <template slot-scope="scope">
          {{scope.row.name || '-'}}
        </template>
      </el-table-column>
      <el-table-column prop="address" label="Address">
      </el-table-column>
    </el-table>
  </template>
</div>

我知道如果我使用'template'标签,我可以将null数据转换为'-'。
现在让我们假设我有100多个表,我不知道哪些单元格可以为空。
把'模板'到所有的el-table-columns将是非常艰苦的工作和效率低下的方式。
我想知道是否有任何方法可以同时将空单元格更改为'-'。帮助我,伙计们jsfiddle

yvgpqqbh

yvgpqqbh1#

只是:

<template>
<el-table :data="tableData" style="width: 100%">
  <el-table-column prop="date" label="Date" width="180">
  </el-table-column>
  <el-table-column prop="name" label="Name" width="180">
    <template slot-scope="scope">
      <!-- Replace empty cell by '-' --->
      <div class="cell" v-if="scope.row.name !== null">{{ scope.row.answer }}</div>
      <div v-else class="cell">-</div>
    </template>
  </el-table-column>
  <el-table-column prop="address" label="Address">
  </el-table-column>
</el-table>

字符串

o2rvlv0m

o2rvlv0m2#

最新代码:将slot-scope改为#default:

<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column empty="NA" prop="date" label="Date" width="180" />
    <el-table-column prop="name" label="Name" width="180" >
    <template #default="scope">
        <div style="display: flex; align-items: center">
            <div class="cell" v-if="scope.row.name !== ''">{{ scope.row.name }}</div>
            <div v-else class="cell">-</div>
        </div>
      </template>
    </el-table-column>
    <el-table-column prop="address" label="Address" />
  </el-table>
</template>

字符串

yqlxgs2m

yqlxgs2m3#

我相信有很多方法可以做到这一点。但是试试这个:

computed: {
  items() {
    let data = this.tableData;

    for(let item of data) {
      for(let key in item) {
        if (!item[key]) {
          item[key] = '-';
        }
      }
    }

    return data;
  }
}

个字符

相关问题