Vue学习——使用v-for渲染后,改变列表里的某个数据并可以二次渲染

前言

vue的导入

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

or:

<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>

问题

问题描述:通过v-for渲染表格,在第一次渲染成功后,修改了某个单元格内容,第二次渲染时该单元格内容未改变(同行其他单元格内容成功渲染)

问题分析:渲染成功后直接修改表格的内容,会直接替换掉修改位置的表达式,使得不能二次渲染

代码示例:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js"
            integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
            crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
<div>
    <div id="app" style="text-align: center;align-items: center">
        <table style="width: 50%;margin: 0 auto"
               class="table table-condensed table-bordered table-hover">
            <thead>
            <tr>
                <th scope="col">ID</th>
                <th scope="col">用户名</th>
                <th scope="col"></th>

            </tr>
            </thead>
            <tbody id="pwd_list">
            <tr v-for="item in items">
                <th scope="row">{{ item.ID }}</th>
                <td><span>{{ item.name }}</span></td>
                <td>
                    <button class="btn btn-default" v-on:click="Onclick($event)">修改</button>
                </td>
            </tr>
            </tbody>
        </table>
        <button class="btn btn-default" v-on:click="review()" style="margin: 2px auto">重新渲染表格</button>
    </div>
</div>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            items: [
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"}
            ],
        },
        methods: {
            Onclick: function (event) {
                var button_ob = event.currentTarget;
                var name_obj = $(button_ob).parent().prev();
                name_obj.text('测试')
            },
            review: function () {
                app.items = [
                    {"ID": 2, "name": "qwe1"},
                    {"ID": 2, "name": "qwe1"}
                ]
            }
        }
    });

</script>
</body>

</html>

如上述代码所示:点击修改按钮将当前行的用户名修改为测试,然后再点击重新渲染表格按钮,但是已修改位置数据没有改变仍为测试

解决方法

本人是vue的初学者所以接下来会将我遇到这个问题时尝试的几种方法展示出来:

第一种“蠢”办法

基于我是要对用户名进行修改的这个需求,想到可以在用户名这个单元格内增加一个隐藏<span id="change" style="display:none">hide</span>标签来展示修改文本

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js"
            integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
            crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
<div>
    <div id="app" style="text-align: center;align-items: center">
        <table style="width: 50%;margin: 0 auto"
               class="table table-condensed table-bordered table-hover">
            <thead>
            <tr>
                <th scope="col">ID</th>
                <th scope="col">用户名</th>
                <th scope="col"></th>

            </tr>
            </thead>
            <tbody id="pwd_list">
            <tr v-for="item in items">
                <th scope="row">{{ item.ID }}</th>
                <td>
                    <span id="default">{{ item.name }}</span>
                    <span id="change" style="display:none">hide</span>
                </td>
                <td>
                    <button class="btn btn-default" v-on:click="Onclick($event)">修改</button>
                </td>
            </tr>
            </tbody>
        </table>
        <button class="btn btn-default" v-on:click="review()" style="margin: 2px auto">重新渲染表格</button>
    </div>
</div>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            items: [
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"}
            ],
        },
        methods: {
            Onclick: function (event) {
                var button_ob = event.currentTarget;
                var name_obj = $(button_ob).parent().prev();
                name_obj.find('span[id^="default"]').hide();
                name_obj.find('span[id^="change"]').show();
                name_obj.find('span[id^="change"]').text('测试')
            },
            review: function () {
                app.items = [
                    {"ID": 2, "name": "qwe1"},
                    {"ID": 2, "name": "qwe1"}
                ]
                $("[id=default]").each(function () {
                    $(this).show()
                });
                $("[id=change]").each(function () {
                    $(this).hide()
                });
            }
        }
    });

</script>
</body>

</html>

第二种方法

每次点击修改时,不直接修改单元格内容,而是对数据集对应内容进行修改

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js"
            integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
            crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
<div>
    <div id="app" style="text-align: center;align-items: center">
        <table style="width: 50%;margin: 0 auto"
               class="table table-condensed table-bordered table-hover">
            <thead>
            <tr>
                <th scope="col">ID</th>
                <th scope="col">用户名</th>
                <th scope="col"></th>

            </tr>
            </thead>
            <tbody id="pwd_list">
            <tr v-for="(item,index) in items">
                <th scope="row">{{ item.ID }}</th>
                <td>
                    <span id="default">{{ item.name }}</span>
                    <span id="change" style="display:none">hide</span>
                </td>
                <td>
                    <!-- <button class="btn btn-default" v-on:click="Onclick($event)">修改</button>-->
                    <button class="btn btn-default" v-on:click="Onclick(index)">修改</button>

                </td>
            </tr>
            </tbody>
        </table>
        <button class="btn btn-default" v-on:click="review()" style="margin: 2px auto">重新渲染表格</button>
    </div>
</div>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            items: [
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"},
                {"ID": 1, "name": "qwe"}
            ],
        },
        methods: {
            Onclick: function (index) {
                 /*app.items[index].name = '测试';*/
                this.$set(this.items[index], 'name', 测试)
            },
            review: function () {
                app.items = [
                    {"ID": 2, "name": "qwe1"},
                    {"ID": 2, "name": "qwe1"}
                ]
                $("[id=default]").each(function () {
                    $(this).show()
                });
                $("[id=change]").each(function () {
                    $(this).hide()
                });
            }
        }
    });

</script>
</body>

</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js是一种流行的JavaScript框架,可以帮助我们构建动态的Web应用程序。使用Vue.js的v-for指令,我们可以轻松地在HTML模板中渲染JSON数组对象列表数据。 假设我们有一个JSON数组对象,包含多个学生的信息,每个学生有姓名和年龄两个属性。我们想通过Vue.js的v-for指令,将这些学生的信息批量渲染到页面上。 首先,在Vue.js中,需要定义一个data对象来存储我们的数据。我们可以在data对象中定义一个students属性,其值为我们的JSON数组对象。 接下来,在HTML模板中,我们可以使用v-for指令来遍历这个JSON数组对象。具体来说,我们可以在一个div容器上使用v-for指令,并使用"student in students"语法来进行遍历。这样,vue.js会自动将每个学生的信息渲染到页面上。 最后,我们可以在v-for指令的内部使用{{ }}语法来显示学生的具体信息。其中,使用{{ student.name }}和{{ student.age }}来分别显示学生的姓名和年龄。 整个过程可以通过下面的代码示例来实现: ```html <div id="app"> <div v-for="student in students"> <p>姓名:{{ student.name }}</p> <p>年龄:{{ student.age }}</p> </div> </div> <script> new Vue({ el: '#app', data: { students: [ { name: '小明', age: 18 }, { name: '小红', age: 19 }, { name: '小亮', age: 20 } ] } }) </script> ``` 以上就是一个基于v-for指令实现批量渲染JSON数组对象列表数据的示例。通过这个示例,我们可以看到Vue.js提供了非常方便的数据驱动视图的方式,使得我们可以轻松地渲染和更新大量的数据

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值