vue在表格添加一行或者删除选择的行数

在这里插入图片描述

html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="bootstrap.min.css">
    <title>Document</title>
</head>

<body>
    <div id="model">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>序号</th>
                    <th>书名</th>
                    <th>作者</th>
                    <th>单价</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(v,i) of book">
                    <td>{{i+1}}</td>
                    <td>{{v.name}}</td>
                    <td>{{v.author}}</td>
                    <td>{{v.price}}</td>
                    <td><button type="button" name="" id="" v-on:click="del(i)" class="btn btn-primary">删除</button></td>
                </tr>
                <!-- <tr>
                    </td>
                    <td><button type="button" name="" id="" class="btn btn-primary" btn-lg btn-block">删除</button></td>
                </tr> -->
            </tbody>
        </table>
        <form action="">
            <label>添加书籍</label>
            <div class="form-group col-md-3">
                <label for="">书名:</label>
                <input type="text" v-model="newBook.name" class="form-control" name="" id="" 
                    placeholder="">
                <small id="helpId"  v-bind:style="mystyle" class="">{{error1}}</small>
            </div>
            <div class="form-group col-md-3">
                <label for="">作者:</label>
                <input type="text"  v-model="newBook.author" class="form-control" name="" id="" 
                    placeholder="">
                <small id="helpId2" v-bind:style="mystyle" class="">{{error2}}</small>
            </div>
            <div class="form-group col-md-3">
                <label for="">价格:</label>
                <input type="text" v-model="newBook.price" class="form-control" name="" id="" 
                    placeholder="">
                <small id="helpId3" v-bind:style="mystyle" class="">{{error3}}</small>
            </div>
            <button type="button" v-on:click="addBook" name="" id="" class="btn btn-primary ">添加</button>
    </div>
    </form>

</body>
<script src="vue.min.js"></script>
</html>

js

 let vm = new Vue({
        el: '#model',
        data: {
            error1:'',
            error2: '',
            error3: '',
            mystyle:{
                color: 'red',
            },
            id: 3,
            newBook: {
                id:'',
                name: '',
                author: '',
                price: '',
            },
            book: [
                {
                    id: "1",
                    name: "红楼梦",
                    author: "曹雪芹",
                    price: "32"
                },
                {
                    id: "2",
                    name: "水浒传",
                    author: "施耐庵",
                    price: "30"
                },
                {
                    id: "3",
                    name: "三国演义",
                    author: "罗贯中",
                    price: "24"
                },
            ]


        },

        methods: {
        //添加一行
            addBook: function () {
                if(this.newBook.name==""){
                    this.error1="书名不能为空!"
                    return;
                }else{
                     this.error1 = ""
                }
                if(this.newBook.author == ""){
                    this.error2 = "作者不能为空!"
                      return;
                }else{
                   this.error2 = ""
                }
                if (this.newBook.price == "") {
                    this.error3 = "价格不能为空!"
                     return;
                }else{
                    this.error3 = ""
                    
                }
                // this.id += 1;
                // this.newBook.id= this.id;
                this.book.push(this.newBook);
                this.newBook = {
                    id: '',
                    name: '',
                    author: '',
                    price: '',
                };
                this.error1='';
                this.error2 = '';
                this.error3 = '';
            },
            //删除
            del:function(i){
                this.book.splice(i,1);
            }
        },
    })

注意删除一行后序号也会发生改变

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Ant Design Vue表格的最后一列添加选择框,可以使用`customRender`属性和`selection`组件结合。首先,在表格的`columns`中添加一个自定义渲染函数,如下所示: ```javascript columns: [ // 其他列配置... { title: '选择', customRender: ({ record }) => { return <a-checkbox checked={this.selectedRows.includes(record)} onChange={(e) => this.handleSelect(record, e)} /> } } ] ``` 在自定义渲染函数中,我们使用`<a-checkbox>`组件来渲染选择框,并将`checked`属性设置为当前行是否被选中,`onChange`事件处理函数则调用`handleSelect`方法来更新选中行数组`selectedRows`。 接着,在表格的`template`中,添加`selection`组件,如下所示: ```html <a-table :columns="columns" :dataSource="dataSource" :rowKey="record => record.id" :pagination="false" :rowSelection="{selectedRowKeys: selectedRows, onChange: handleSelectAll}"> <template #selection> <a-checkbox v-model:checked="isAllSelected" @change="handleSelectAll">{{isAllSelected ? '取消全选' : '全选'}}</a-checkbox> </template> </a-table> ``` 在`template`中,我们使用`<a-table>`组件的`rowSelection`属性来开启行选择功能,并将选中行的`key`保存在`selectedRows`数组中,并通过`onChange`事件处理函数`handleSelectAll`来实现全选/取消全选功能。在`template`的`selection`插槽中,我们使用`<a-checkbox>`组件来实现全选/取消全选的交互操作。 最后,需要在组件的`data`中定义一些变量和方法,如下所示: ```javascript data() { return { selectedRows: [], // 选中行的key数组 isAllSelected: false // 是否全选 } }, methods: { // 处理行选择 handleSelect(record, e) { const index = this.selectedRows.indexOf(record.id) if (e.target.checked && index === -1) { this.selectedRows.push(record.id) } else if (!e.target.checked && index !== -1) { this.selectedRows.splice(index, 1) } this.isAllSelected = this.selectedRows.length === this.dataSource.length }, // 处理全选/取消全选 handleSelectAll(e) { if (e.target.checked) { this.selectedRows = this.dataSource.map(record => record.id) } else { this.selectedRows = [] } this.isAllSelected = e.target.checked } } ``` 在`handleSelect`方法中,我们根据`<a-checkbox>`的选中状态来更新选中行数组`selectedRows`,并根据选中行数与总行数的比较来更新全选/取消全选的状态。在`handleSelectAll`方法中,我们根据`<a-checkbox>`的选中状态来更新选中行数组`selectedRows`和全选/取消全选的状态。 这样,就可以在Ant Design Vue表格的最后一列添加选择框了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值