Ant Design Vue 表格自定义单元格

环境:

<template>
    <page-view>
        <a-card>
            <div class="search-box">
                <a-input v-model="table.query.ruleName" placeholder="输入关键词" />
                <a-button type="primary" @click="onSearch"><a-icon type="search" />查询</a-button>
                <a-button @click="reset"><a-icon type="undo" />重置</a-button>
            </div>
            <div class="table-wrapper">
                <div>
                    <a-table
                        ref="table"
                        :columns="table.columns"
                        :dataSource="table.dataList"
                        :pagination="table.pagination"
                        @change="handleChangePage"
                    >
                        <span slot="ruleName" slot-scope="text, record">
                          <template>
                              <span>{{ record.ruleName }}</span>
                          </template>
                        </span>
                        <span slot="ruleDescription" slot-scope="text, record">
                          <template>
                                <span>{{ record.ruleDescription }}</span>
                          </template>
                        </span>
                        <span slot="score" slot-scope="text, record">
                          <template>
                              <span v-if="!record.isUse">{{ record.score }}</span>
                              <a-input-number v-if="record.isUse" v-model="record.score" :min="1"/>
                          </template>
                        </span>
                        <span slot="dayMaxScore" slot-scope="text, record">
                          <template>
                              <span v-if="!record.isUse">{{ record.dayMaxScore }}</span>
                              <a-input-number v-if="record.isUse" v-model="record.dayMaxScore" :min="record.score"/>
                          </template>
                        </span>
                        <span slot="state" slot-scope="text, record, index">
                          <template>
                            <div v-if="!record.isUse">
                               <span v-if="record.state == 0">禁用</span>
                               <span v-if="record.state == 1">启用</span>
                            </div>
                            <a-dropdown v-if="record.isUse">
                              <a class="ant-dropdown-link">
                                  <span v-if="record.state == 0">禁用</span>
                                  <span v-if="record.state == 1">启用</span>
                                <a-icon type="down" />
                              </a>
                              <a-menu slot="overlay">
                                <a-menu-item v-if="record.state == 1">
                                  <a href="javascript:;" @click="handleChangeStatus(record, index, 0)">禁用</a>
                                </a-menu-item>
                                <a-menu-item v-if="record.state == 0">
                                  <a href="javascript:;" @click="handleChangeStatus(record, index, 1)">启用</a>
                                </a-menu-item>
                              </a-menu>
                            </a-dropdown>
                          </template>
                        </span>
                        <span slot="action" slot-scope="text, record, index">
                          <template>
                            <a-button v-if="record.isUse" style="border-color: #1890ff;color: #1890ff;" @click="handleSaveCol(record, index)">保存</a-button>
                            <a-button v-if="!record.isUse" style="border-color: forestgreen;color: forestgreen;margin: 0 4px" @click="handleChangeCol(record, index)">修改</a-button>
                          </template>
                        </span>
                    </a-table>
                </div>
            </div>
        </a-card>
    </page-view>
</template>

<script>
    import {listPage, saveOrUpdate} from '@/api/point/point'
    import {columns} from './constant'
    export default {
        name: 'PointSetting',
        data() {
            return {
                table: {//表格数据
                    columns: columns,
                    query: {},
                    dataList: [
                        // { ruleName: 2,ruleDescription:333,score:444, state: 1,dayMaxScore:222 }
                    ],
                    pagination: {
                        current: 1,
                        pageSize: 10,
                        total: 0
                    },
                },
            }
          },
        created() {
            this.listPage();//加载列表
        },
        methods: {
            listPage(pagination, filters, sorters) {
                const params = Object.assign({
                    ruleName: this.table.query.ruleName
                }, pagination, filters, sorters);
                return listPage(params)
                    .then(response => {
                        this.table.dataList = response.data;
                    })
            },
            onSearch() {
                this.listPage();//刷新列表
            },
            reset() {
                this.table.query = {};//清空查询条件
                this.listPage();//重置列表
            },
            handleChangePage(page) {
                this.table.pagination.current = page.current
            },
            handleChangeStatus(data, index, type) {
                this.table.dataList.splice(index, 1, {
                    ...data,
                    state: type
                })
            },
            // 保存按钮
            handleSaveCol(data, index) {
                this.table.dataList.splice(index, 1, {
                    ...data,
                    isUse: false
                })
                console.log(data)
                this.saveOrUpdate(data);
            },
            // 修改按钮
            handleChangeCol(data, index) {
                this.table.dataList.splice(index, 1, {
                    ...data,
                    isUse: true
                })
            },
            //保存方法
            saveOrUpdate(data) {
                return saveOrUpdate(data.id, data).then(
                    response => {
                        return response;
                    }
                );
            },
          }
    };
</script>

<style scoped>
    .search-box {
        display: flex;
        align-items: center;
        margin-bottom: 15px;
    }
    .search-box>button {
        margin-left: 15px;
    }
    .search-box>input {
        width: 200px;
    }
    .ant-dropdown-link>span {
        margin-right: 5px;
    }
</style>

问题:

如何在表格中自定义输入框、下拉框?

解决:

constant.js中代码

import { initTimeDate } from '@/utils/utils'
export const columns = [
  {
    title: '序号',
    width: '80px',
    align: 'center',
    customRender: (text, record, index) => {
      return index + 1;
    },
  },
  {
    title: '积分规则',
    width: '200px',
    align: 'center',
    dataIndex: 'ruleName',
    scopedSlots: { customRender: 'ruleName' },
  },
  {
    title: '规则说明',
    align: 'center',
    dataIndex: 'ruleDescription',
    scopedSlots: { customRender: 'ruleDescription' },

  },
  {
    title: '分值',
    align: 'center',
    width: '200px',
    dataIndex: 'score',
    scopedSlots: { customRender: 'score' },
  },
  {
    title: '每日分值上限',
    dataIndex: 'dayMaxScore',
    align: 'center',
    width: '200px',
    scopedSlots: { customRender: 'dayMaxScore' },
  },
  {
    title: '状态',
    dataIndex: 'state',
    align: 'center',
    width: '150px',
    scopedSlots: { customRender: 'state' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '200px',
    align: 'center',
    scopedSlots: { customRender: 'action' },
  },
];

export const columnsTwo = [
  {
    title: '序号',
    width: '80px',
    align: 'center',
    customRender: (text, record, index) => {
      return index + 1;
    },
  },
  {
    title: '部门机构',
    align: 'center',
    dataIndex: 'diskName',
  },
  {
    title: '用户姓名',
    width: '150px',
    align: 'center',
    dataIndex: 'diskSize',
    scopedSlots: { customRender: 'diskSize' },

  },
  {
    title: '积分',
    align: 'center',
    width: '200px',
    dataIndex: 'createTime',
  },
];

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值