uniapp实现列表动态添加

1.效果图:

2.代码实现:

这里没有用uniapp提供的uni-list控件

<template>
    <view id="app">
    <!--     这里为了让标题(h)居中展示,给h标签设置了父标签,并设置父标签text-align: center;来实现
        标题内容居中展示 -->
        <view class="biaoti">
            <h3>动态添加</h3>
        </view> 
        姓名:<input class="shurukuang" type="text"  maxlength="8"  confirm-type="next"  placeholder="请输入姓名" v-model="name"/>
        <br>
        年龄:<input class="shurukuang" type="number" maxlength="3" confirm-type="next" placeholder="请输入年龄" v-model="age"/>
        <br>
        班级:<input class="shurukuang" placeholder="请输入班级" maxlength="2" v-model="banji"/>
        <br>
        <button id="add" type="primary" hover-start-time="20" @click="addMember()">添加</button>
    <list-view class="main" id="list-view">
        <list-item class="list_item" :data-bean="item" v-for="(item, index) in dataList" :key="index">
            <view style="height: 1upx; background-color:aliceblue; margin-top: 5upx;"></view>
            <view class="horizontal">
                    <text class="item_value">{{item.name}}</text>
                    <text class="item_value">{{item.age}}</text>
                    <text class="item_value">{{item.banji}}</text>
            </view>
            <view style="height: 5upx; background-color: gainsboro;"></view>
        </list-item>
    </list-view>

    </view>
    
</template>
<script setup>
    // const plugin = uni.requireNativePlugin('test')
    export default {
        data() {
            return {
                reply: "",
                msg: "",
                name:"",
                age:"",
                banji:"",
                dataList:[
                    {name:"张三",age:"18",banji:"一班"}
                ]
            }
        },
        onShow: function() {
            //设置状态栏标题
            uni.setNavigationBarTitle({
                title:"人员信息"
            })
        },
        onLoad() {
        },
        methods: {
            addMember(){
                //校验信息是否填写完整
                if(this.name==""){
                    alert("请先输入姓名")
                }else if(this.age==""){
                    alert("请先输入年龄")
                }else if(this.banji==""){
                    alert("请先输入班级")
                }else{
                    //将输入框的数据组装成对象
                    var bean={name:this.name,age:this.age,banji:this.banji}
                    //往list列表添加数据
                    this.dataList.push(bean)
                    //数据添加和刷新成功,清空上一次输入记录
                    this.name=""
                    this.age=""
                    this.banji=""
                }
                
            }
            
 
        }
    }
</script>
<style>
    
    #app{
        text-align: left;
        padding: 16rpx;
    }
    
    
    
    .biaoti{
        text-align: center;
    }
    
    .horizontal{
        text-align: center;
    }
    
    .item_value{
        width: 33%;
        line-height: 60rpx;
        color: black;
        display: inline-block;
    }
    

    
    .shurukuang {
        font-weight: bold;
        padding-left: 1em;
        padding-top: 10rpx;
        padding-bottom: 10rpx;
        border: solid #cecece;
        border-radius: 5rpx;
    }
    
    .h{
        margin-top: 100rpx;
        background: deeppink;
    }
    
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }
 
    .logo {
        height: 200rpx;
        width: 200rpx;
        margin-top: 200rpx;
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 50rpx;
    }
 
    .text-area {
        display: flex;
        justify-content: center;
    }
 
    .title {
        color: #5555ff;
        width: 100vw;
        display: flex;
        justify-content: center;
    }
</style>

3.列表添加和删除:

1.列表添加对象数据:list.push(item)

2.列表删除对象数据:list.splice(i(下标),1)
 

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现列表置顶,你可以使用uni-app中提供的scroll-view组件。 首先,需要在scroll-view组件中添加一个置顶按钮,按钮的点击事件需要使用scroll-view组件中的scroll-top属性来实现。 接着,需要监听scroll-view组件的滚动事件,判断当前滚动的距离是否大于列表的高度,如果大于则显示置顶按钮,否则隐藏。 最后,在置顶按钮的点击事件中,将scroll-view组件的scroll-top属性设置为0,即可实现列表置顶的功能。 以下是一个简单的示例代码: ``` <template> <view> <scroll-view class="list" scroll-y="true" scroll-top="{{scrollTop}}" bindscroll="onScroll"> <!-- 列表内容 --> </scroll-view> <view class="to-top" style="display:{{showTopBtn ? 'block' : 'none'}}" bindtap="toTop">返回顶部</view> </view> </template> <script> export default { data() { return { scrollTop: 0, // scroll-view 组件的滚动位置 showTopBtn: false, // 是否显示置顶按钮 } }, methods: { // 监听 scroll-view 组件的滚动事件 onScroll(event) { const scrollTop = event.detail.scrollTop const listHeight = 1000 // 列表的高度 if (scrollTop > listHeight) { this.showTopBtn = true } else { this.showTopBtn = false } }, // 点击置顶按钮,将 scroll-view 组件的滚动位置设置为0 toTop() { this.scrollTop = 0 } } } </script> <style> .list { height: 500px; overflow: auto; } .to-top { position: fixed; bottom: 20px; right: 20px; width: 100px; height: 40px; line-height: 40px; text-align: center; background-color: #333; color: #fff; border-radius: 20px; cursor: pointer; } </style> ``` 你可以根据自己的实际需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值