Element-UI 之 el-table表格插槽、表头样式、内容居中

html 部分,使用 slot-scope插槽 

<el-table :data="tableData" border :header-cell-style="{ background: '#eef1f6', color: '#606266' }">
      <el-table-column prop="date" label="日期" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="address" label="地址" />
      <el-table-column prop="status" label="状态" width="120">
        <!-- 插槽 -->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1">标签一</el-tag>
          <el-tag v-if="scope.row.status == 2" type="success">标签二</el-tag>
          <el-tag v-if="scope.row.status == 3" type="info">标签三</el-tag>
          <el-tag v-if="scope.row.status == 4" type="warning">标签四</el-tag>
          <el-tag v-if="scope.row.status == 5" type="danger">标签五</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="240">
        <template slot-scope="scope">
          <el-button type="text" @click="handleClickGet(scope.row)">查看</el-button>
          <el-button type="text" @click="handleClickEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleClickDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

js部分 

<script>
export default {
  data() {
    return {
      tableData: [
        { date: '2016-05-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄', status: 1 },
        { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1517 弄', status: 2 },
        { date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1519 弄', status: 3 },
        { date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1516 弄', status: 4 },
        { date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1516 弄', status: 5 }
      ]
    }
  },
  methods: {
    // 查看
    handleClickGet(val) {
      console.log(val);
    },
    // 编辑
    handleClickEdit(val) {
      console.log(val);
    },
    // 删除
    handleClickDel(val) {
      console.log(val);
    },
  },
  mounted() {
  },
  created() { }
}
</script>

css部分,全局elementui的el-table内容居中样式 

<style lang="scss" >
// element-ui 表格内容居中
.el-table__cell {
  text-align: center !important;
}
</style>

:header-cell-style="{ background: '#eef1f6', color: '#606266' }" 

设置表头背景颜色、文字颜色

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
el-table是一个基于Element UI表格组件,可以用于展示数据并进行排序、筛选等操作。如果要进行列序的变换并绑定用户,可以通过以下几步来实现。 首先,使用el-table组件创建一个表格,并绑定需要展示的数据源,例如一个数组data。 ``` <el-table :data="data"> <!-- 列定义 --> </el-table> ``` 接着,定义需要展示的列,可以使用el-table-column组件,通过指定prop属性来绑定数据源中对应的字段。 ``` <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="gender" label="性别"></el-table-column> <!-- 其他列定义 --> ``` 以上例子中,prop属性分别对应data中的name、age、gender字段。根据实际情况定义需要展示的列。 然后,为了实现列序的变换功能,可以使用el-table的:order-prop属性,绑定一个数组orderProps,该数组指定了列的显示顺序。 ``` <el-table :data="data" :order-prop="orderProps"> <!-- 列定义 --> </el-table> ``` 在Vue的data中定义orderProps数组,并通过计算属性来根据用户的操作实时更新数组的值。 ``` data() { return { data: [], orderProps: ['name', 'age', 'gender'] } }, computed: { orderProps() { // 根据用户的操作来更新orderProps的值 return this.orderProps; } } ``` 通过计算属性来绑定orderProps数组,当用户进行列序的变换时,更新orderProps数组的值,el-table会根据该数组的顺序来展示列。 最后,为了绑定用户,可以在orderProps数组中添加一个特殊的字段,例如userID。在列定义中添加一个slot,通过插槽的方式自定义该列的内容。 ``` <el-table-column label="操作"> <template slot-scope="scope"> <el-button @click="handleClick(scope.row)">删除</el-button> </template> </el-table-column> ``` 以上例子中,通过slot-scope="scope"来获取行数据,可以通过scope.row获取到每一行的数据。在handleClick方法中可以获取到对应的userID,然后根据业务需求进行相应的操作。 通过以上的步骤,就可以实现el-table的列序变换并绑定用户的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值