element ui的table表格如果想给一行中的某一列添加不同的操作可以这样做

方法1:

代码:

            <el-table-column
                    label="知识点"
                    prop="description"
                    show-overflow-tooltip>
                <template slot-scope="scope">
                    <router-link v-bind:to="'/index?id='+scope.row.id">                           
                        {{scope.row.description}}
                    </router-link>
                </template>
            </el-table-column>

如果想控制某一个元素。可以使用事件添加判断

感觉上面的方法不好用。我想了另一个办法:

方法2:
1,在click里面关联一个isClick默认是false,如果点击了。就变成true。
2,在点击每一行之后。要跳转之前。加一个判断。isClick为true就不跳转。

关键代码:

完整代码:

<template>
    <div>
        <h3>我创建的试卷列表:包含了所有做过和没有做过的试卷MyExam</h3>
        <el-table
                :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
                style="width: 100%" @cell-click="handle">
            <el-table-column
                    label="序号"
                    prop="id">
            </el-table-column>
            <el-table-column
                    label="试卷名称"
                    prop="name">
            </el-table-column>
            <el-table-column
                    label="试卷描述"
                    prop="description">
            </el-table-column>
            <el-table-column
                    label="试卷标签"
                    prop="categories">
            </el-table-column>
            <el-table-column
                    label="题目数量"
                    prop="topicCount">
            </el-table-column>
            <el-table-column
                    label="总分"
                    prop="score">
            </el-table-column>
            <el-table-column
                    label="考试时长"
                    prop="time">
            </el-table-column>
            <el-table-column
                    label="创建时间"
                    prop="createTime">
            </el-table-column>
            <el-table-column
                    align="right">
                <!--                    <template slot="header" slot-scope="scope">-->
                <template slot="header">
                    <el-input
                            v-model="search"
                            size="mini"
                            placeholder="输入关键字搜索"/>
                </template>
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="beginTest(scope.$index, scope.row)">开始考试
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除试卷
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    background
                    layout="prev, pager, next"
                    :total="total">
        </el-pagination>
    </div>
</template>

<script>
    import global from "../../Global";

    export default {
        name: "MyExam",
        data() {
            return {
                tableData: [
                    {
                        id: '',//试卷id序号
                        name: '',//试卷名称
                        creatorId: '',//创建者id
                        description: '',//试卷描述
                        categories: [],//标签
                        topicCount: 0,//题目数量
                        score: 0,//总分
                        time: 0,//考试时长
                        createTime: '',//创建时间
                    }
                ],
                isClick: false,  //判断按钮是否被点击
                search: '',
                pageNum: 1,
                pageSize: 10,
                total: 0,
            }
        },
        methods: {
            //点击每一行都可以跳转
            handle(row, column, event, cell) {
                console.log(row)
                console.log(column)
                console.log(event)
                console.log(cell)
                // 跳转到编辑题目页面需要的数据id
                console.log("试卷编号是:" + row.id);
                console.log("试卷创建者是:" + row.creatorId);
                localStorage.setItem("examId", row.id);
                localStorage.setItem("examCreatorId", row.creatorId);
                if (!this.isClick) {
                    this.$router.push({path: '/examdetails'});
                }
            },
            beginTest(index, row) {
                this.isClick = true;
                console.log(index, row);
                console.log(row.id);
                this.$router.push({
                    path: '/createtest',
                    query: {
                        id: row.id
                    }
                });
            },
            //删除试卷
            handleDelete(index, row) {
                console.log(index, row);
                console.log(row.id);
                console.log(typeof row.id);
                this.isClick = true;
                console.log("是否被点击了删除按钮试卷?==" + this.isClick);
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let formdata = new FormData();
                    formdata.append('id', row.id);
                    this.$axios.post("/exam/deleteExam", formdata).then(response => {
                        console.log("response.data" + response.data);
                        if (response.data.status === global.responseCode.OK) {
                            console.log("删除成功!");
                            //删除成功之后。页面重新获取数据
                            this.getData();
                            this.isClick = false;
                        } else {
                            console.log("删除失败: " + response.data.msg);
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.isClick = false;
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //分页
            // 每页显示的条数
            handleSizeChange: function (size) {
                // 改变每页显示的条数
                this.pagesize = size;
                console.log(this.pagesize);
            },
            // 显示第几页
            handleCurrentChange: function (pageNum) {
                // 改变默认的页数
                this.pageNum = pageNum;
                console.log(this.pageNum); //点击第几页
                this.getData();
            },
            getData() {
                this.$axios.get("/exam/listMyExam", {params: {pageNum: this.pageNum}}).then(response => {
                    // console.log(response.data.result);
                    if (response.data.result) {
                        // this.tableData = response.data.result;
                        var arr = response.data.result.list;
                        this.tableData = arr.map(item => {
                            return {
                                id: item.id,
                                name: item.name,
                                creatorId: item.creatorId,
                                description: item.description,
                                topicCount: item.topicCount,
                                score: item.score,
                                time: item.time,
                                categories: item.categories,
                                createTime: item.createTime,
                            };
                        });
                        this.total = response.data.result.total;
                        // console.log(this.total);
                    } else {
                        console.log("获取试卷信息失败: " + response.data.msg);
                    }
                }).catch(function (error) {
                    console.log(error);
                })
            },
        },
        created() {
            this.getData();
        }
    }
</script>

<style scoped>

</style>

https://segmentfault.com/q/1010000021853272

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
element ui表格的某一列循环是指在表格的某一列数据进行循环展示。在element ui表格,我们可以通过自定义的render函数来实现该功能。 首先,在el-table-column设置自定义列的render函数,该函数会接收到两个参数:row 和 column。其,row代表当前行的数据,column代表当前列的配置信息。我们可以根据具体的需求,对row的某一列进行循环渲染。 其次,我们可以在render函数使用v-for指令来循环渲染数据。通过v-for指令,我们可以遍历某一列数据,生成对应的HTML元素。 最后,根据具体的需求,我们可以利用循环生成的HTML元素进行一些操作,比如添加样式、绑定事件等。 下面是一个示例代码,通过v-for指令实现对表格一列数据的循环展示: ```html <template> <el-table :data="tableData"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column label="爱好"> <template slot-scope="scope"> <div v-for="item in scope.row.hobbies" :key="item"> {{ item }} </div> </template> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', hobbies: ['篮球', '足球', '游泳'] }, { name: '李四', hobbies: ['跑步', '骑车'] }, { name: '王五', hobbies: ['唱歌', '跳舞', '书法'] } ] } } } </script> ``` 以上示例代码实现了一个element ui表格,其"爱好"列的数据被循环展示出来。每个爱好被渲染为一个div元素。最终的效果是每一行的"爱好"列都会展示出相应的爱好数据。 通过以上方法,我们可以实现element ui表格一列数据的循环展示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南北极之间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值