kktable

<template>
  <div>
    <a-table
      :columns="columns"
      :data-source="dataSource"
      :scroll="{ x: 1500, y: 300 }"
    >
      <template slot="centerSource" slot-scope="text, record">
        <editable-cell
          :text="text"
          @change="onCellChange(record.key, 'centerSource', $event,text)"
        />
      </template>
      <!-- 下拉框的修改 -->
       <template slot="centerSource4" slot-scope="text, record">
        <editable-cell
          :text="text"
          @change="onCellChange(record.key, 'centerSource4', $event,text)"
        />
      </template>
      <template slot="uploadImg" slot-scope="text, record">
        <a
          href="javascript:;"
          @click="uploadImg(record.key, 'uploadImg', $event)"
          >操作按钮</a
        >
      </template>
    </a-table>
    <a-button class="editable-add-btn" @click="handleAdd"> 提交 </a-button>
  </div>
</template>

<script>
import EditableCell from "./edit.vue";
export default {
  components: {
    EditableCell,
  },

  data() {
    return {
      list:[],
      columns: [],
      dataSource: [],
      selectedRowKeys: [],
      //  左侧固定列
      leftArr: [
        {
          title: "苹果",
          width: 100,
          dataIndex: "apple",
          key: "apple",
          fixed: "left",
        },
        {
          title: "橘子",
          width: 100,
          dataIndex: "orange",
          key: "orange",
          fixed: "left",
        },
      ],
      //   右侧固定列
      rightArr: [
        {
          title: "图片上传",
          key: "uploadImg",
          fixed: "right",
          width: 100,
          scopedSlots: { customRender: "uploadImg" },
        },
      ],
    };
  },
  computed: {},
  mounted() {
    this.init();
  },
  methods: {
    init() {
      // 一下所有循环的数量,可根据后台返回的数据长度做修改
      // 以下数据都可根据后台返回遍历填写变量即可
      // 数据源
      for (let i = 0; i < 5; i++) {
        let data = {
          key: i,
          apple: `不可编辑${i}`,
          orange: `不可编辑${i}`,
          banana: `不可编辑${i}`,
          centerSource:`假数据${i}`,
        };
        // 动态生成多列中间的数据,并且一一对应
        for (let j = 0; j < 5; j++) {
          data[`centerSource${j}`] = `筛选${i}`;
        }
        this.dataSource.push(data);
      }
      // 中间动态列,默认有一列表头是下拉框,其余的可在前面插入,也可在后面插入
      let centerArr = [
        {
          title: "下拉框",
          dataIndex: "centerSource4",
          key: "scenterSource",
          width: 150,
          scopedSlots: { customRender: "centerSource4" },
          // 筛选列表
          filters: [
            {
              text: "筛选3",
              value: "筛选3",
            },
            {
              text: "筛选4",
              value: "筛选4",
            },
          ],
          // 筛选
          onFilter: (value, record) => {
            let res = record.centerSource4.indexOf(value) === 0;
            return res;
          },
        },
      ];
      // 组合中间列数据
      for (let i = 0; i < 4; i++) {
        if (i == 1) {
          // 再下拉框列前面插入
          centerArr.unshift({
            title: "水稻" + i,
            dataIndex: "centerSource" ,
            key: i,
            width: 150,
            scopedSlots: { customRender: "centerSource" },
          });
        } else {
          // 再下拉框列后面插入
          centerArr.push({
            title: "水稻" + i,
            dataIndex: "centerSource" ,
            key: i,
            width: 150,
            scopedSlots: { customRender: "centerSource" },
          });
        }
      }
      // 右侧固定列
      for (let i = 0; i < 1; i++) {
        this.rightArr.unshift({
          title: "香蕉" + i,
          dataIndex: "banana",
          key: "banana" + i,
          fixed: "right",
          width: 100,
        });
      }
      //   最终所有数据
      this.columns = this.leftArr.concat(centerArr, this.rightArr);
    },
    // 操作按钮
    uploadImg() {
      console.log("操作按钮");
    },
    // 可编辑
    onCellChange(key, dataIndex, value,text) {
      const dataSource = [...this.dataSource];
      const target = dataSource.find((item) => item.key === key);
      if (target) {
        target[dataIndex] = value;
        this.dataSource = dataSource;
      }
    },
    handleAdd() {
      alert(JSON.stringify(this.dataSource))
      console.log(this.dataSource, "12321");
    },
  },
  filters: {},
};
</script>

<style scoped lang=''>
.editable-add-btn{
  float: right;
  margin-right: 1rem;
}
</style>
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值