el-table表格里面显示数组编号,修改为显示对应的数组名字

为了将表格中的某个列显示为数组中的名称而不是编号,你需要定义一个映射关系,即将编号映射到相应的名称。假设你有一个数组 namesArray,其中包含了编号和名称的对应关系,你可以使用这个数组来替换表格中的编号。

示例代码

假设你有一个 namesArray,它是一个对象数组,每个对象都有一个 idname 属性。你可以在表格渲染时查找对应的名称,并显示它。

1. 定义映射数组
const namesArray = [
    { id: 1, name: '名称1' },
    { id: 2, name: '名称2' },
    { id: 3, name: '名称3' },
    // ... 其他项
];

// 示例数据
const tableData = [
    { ORGON_CODE: '999-N0000008', ID: 1, ...otherProperties },
    { ORGON_CODE: '999-N0000009', ID: 2, ...otherProperties },
    // ... 其他项
];
2. 在模板中显示名称

如果你使用的是 Vue.js,可以在模板中使用计算属性或方法来查找对应的名称。

Vue 2 示例
<template>
  <el-table :data="tableData">
    <el-table-column prop="ORGON_CODE" label="机构代码"></el-table-column>
    <el-table-column label="名称">
      <template slot-scope="scope">
        {{ getNameById(scope.row.ID) }}
      </template>
    </el-table-column>
    <!-- 其他列 -->
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { ORGON_CODE: '999-N0000008', ID: 1, ...otherProperties },
        { ORGON_CODE: '999-N0000009', ID: 2, ...otherProperties },
        // ... 其他项
      ],
      namesArray: [
        { id: 1, name: '名称1' },
        { id: 2, name: '名称2' },
        { id: 3, name: '名称3' },
        // ... 其他项
      ]
    };
  },
  methods: {
    getNameById(id) {
      return this.namesArray.find(item => item.id === id)?.name || '未知';
    }
  }
};
</script>
Vue 3 示例
<template>
  <el-table :data="tableData">
    <el-table-column prop="ORGON_CODE" label="机构代码"></el-table-column>
    <el-table-column label="名称">
      <template v-slot="{ row }">
        {{ getNameById(row.ID) }}
      </template>
    </el-table-column>
    <!-- 其他列 -->
  </el-table>
</template>

<script setup>
import { ref } from 'vue';

const tableData = ref([
  { ORGON_CODE: '999-N0000008', ID: 1, ...otherProperties },
  { ORGON_CODE: '999-N0000009', ID: 2, ...otherProperties },
  // ... 其他项
]);

const namesArray = ref([
  { id: 1, name: '名称1' },
  { id: 2, name: '名称2' },
  { id: 3, name: '名称3' },
  // ... 其他项
]);

function getNameById(id) {
  return namesArray.value.find(item => item.id === id)?.name || '未知';
}
</script>

说明

  1. 映射数组 namesArray

    • 包含了编号和名称的对应关系。
  2. 获取名称的方法 getNameById

    • 使用 Array.prototype.find 方法来查找匹配的名称。
    • 如果没有找到匹配项,则返回 '未知'
  3. 模板中的使用

    • 在模板中使用 slot-scope(Vue 2)或 v-slot(Vue 3)来访问当前行的数据,并调用 getNameById 方法获取名称。

通过这种方式,你可以将表格中的编号替换为对应的名称,从而提高表格的可读性和用户友好性。如果 namesArray 动态变化,建议将 getNameById 方法改为计算属性或响应式函数,以确保实时更新。

如果你想要在 `el-table` 的单元格中以多行展示某个属性,而其他属性只需要展示一行,可以使用 `scoped slot` 的方式来自定义单元格的展示方式。 具体做法是,在表格中指定 `prop` 为对象数组的列,同时在该列上使用 `scoped slot`,并在 `scoped slot` 中自定义单元格的展示方式。在 `scoped slot` 中,你可以根据当前行的数据来判断该行的某个属性是否需要进行多行展示,然后分别处理该属性的多行展示和其他属性的一行展示。 以下是一个示例代码,其中假设表格数据中有一个 `content` 属性需要进行多行展示,而其他属性只需展示一行。示例中使用了 `v-if` 来判断当前行数据中 `content` 属性的行数,并添加了对应的 `rowspan` 属性来控制该属性的多行展示。 ```html <el-table :data="tableData"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="content" label="内容"> <template slot-scope="scope"> <td v-if="scope.row.content.length > 1" :rowspan="scope.row.content.length">{{ scope.row.content[0] }}</td> <td v-else>{{ scope.row.content[0] }}</td> </template> </el-table-column> </el-table> ``` 在上述示例中,假设 `tableData` 数组中的每个对象都有一个 `content` 属性,该属性是一个数组,其中的每个元素需要在单元格中进行多行展示。在 `scoped slot` 中,我们首先使用 `v-if` 来判断当前行数据中 `content` 属性的长度是否大于 1,如果是,则说明该属性需要进行多行展示,我们就在第一行单元格中添加了对应的 `rowspan` 属性。在第二行及之后的单元格中,我们只需要展示该属性的对应值即可。如果 `content` 属性的长度为 1,则说明该属性只需要展示一行,我们直接将该属性的值渲染到单元格中即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值