uni-app 自定义下拉选择列表

效果图:

1.自定义组件ChoiceSelected.vue

2.组件代码:

<template name="ChoiceSelected">
    <!-- 自定义下拉选择框 start-->
    <view class="selected-all">
        <view :class="isShowChoice ? 'drop-down-box-selected' : 'drop-down-box'" @click="btnShowHideClick">
            <text class="dropdown-content">{{choiceContent}}</text>
            <view>
                <image class="dropdown-icon" src="../static/bottom_jiantou.png" mode="widthFix"></image>
            </view>
        </view>
        <!-- 弹框内容 -->
        <view class="dialog-view" v-if="isShowChoice">
            <text :class="choiceIndex ==index ?'dialog-title-selected':'dialog-title'"
                v-for="(item ,index) in choiceList" @click="btnChoiceClick(index)">{{item.choiceItemContent}}</text>
        </view>
    </view>
    <!-- 自定义下拉选择框 end -->
</template>

<script>
    export default {
        name: "ChoiceSelected",
        data() {
            return {
                isShowChoice: false
            };
        },
        props: {
            choiceIndex: {},
            choiceList: {},
            choiceContent: {}
        },
        methods: {
            // 选择
            btnChoiceClick: function(position) {
                var _this = this
                // _this.choiceIndex = position
                _this.isShowChoice = false
                // _this.choiceContent = _this.choiceList[position].choiceItemContent
                _this.$emit("onChoiceClick",position)
            },
            // 显示与隐藏选择内容
            btnShowHideClick: function() {
                var _this = this
                if (_this.isShowChoice) {
                    _this.isShowChoice = false
                } else {
                    _this.isShowChoice = true
                }
            },
        }
    }
</script>

<style>
    /* end */
    .dialog-title-selected {
        color: white;
        font-size: 30rpx;
        padding-left: 20rpx;
        padding-top: 6rpx;
        padding-bottom: 6rpx;
        padding-right: 15rpx;
        background-color: #55ffff;
    }

    .dialog-title {
        color: black;
        font-size: 30rpx;
        padding-left: 20rpx;
        padding-top: 6rpx;
        padding-bottom: 6rpx;
        padding-right: 15rpx;
        background-color: white;
    }

    .dialog-view {
        display: flex;
        flex-direction: column;
        justify-content: center;
        width: 100%;
        border: 2rpx solid #F0AD4E;
        box-sizing: border-box;
    }

    .dropdown-icon {
        width: 30rpx;
        height: 30rpx;
        margin-left: 15rpx;
        margin-right: 20rpx;
    }

    .dropdown-content {
        color: black;
        font-size: 30rpx;
        padding-left: 20rpx;
        padding-top: 5rpx;
        padding-bottom: 5rpx;
        flex-grow: 1;
    }

    .drop-down-box-selected {
        display: flex;
        flex-direction: row;
        align-items: center;
        min-height: 60rpx;
        width: 100%;
        border: 2rpx solid #F0AD4E;
        box-sizing: border-box;
    }

    .drop-down-box {
        display: flex;
        flex-direction: row;
        align-items: center;
        min-height: 60rpx;
        width: 100%;
        border: 2rpx solid gray;
        border-radius: 5rpx;
        box-sizing: border-box;
    }

    .selected-all {
        display: flex;
        flex-direction: column;
        justify-content: center;
        width: 100%;
    }

    /* start */
</style>
 

3.调用与注册组件

子组件中参数传递的方法

 在子组件中的_this.$emit("onChoiceClick",position)必须与父vue中@调用名称一致。

 参数说明:

属性默认值/类型说明
isShowChoicefalse/boolean是否显示下拉类表数据
choiceIndex0/int下拉类表数据,选择位置
choiceContent“请选择”/String选择的内容
choiceList自定义/数组下拉类表数据

4.父vue的代码:

<template>
    <view class="content">
        <view class="selected-view">
            <choice-selected :choiceContent="choiceContent" :choiceIndex="choiceIndex" :choiceList="choiceList"
                @onChoiceClick="onChoiceClick"></choice-selected>
        </view>
    </view>
</template>

<script>
    import choiceSelected from '../../components/ChoiceSelected.vue'
    export default {
        components: {
            choiceSelected
        },
        data() {
            return {
                choiceList: [{
                        choiceItemId: "0",
                        choiceItemContent: "请选择"
                    },
                    {
                        choiceItemId: "P",
                        choiceItemContent: "苹果"
                    },
                    {
                        choiceItemId: "L",
                        choiceItemContent: "荔枝"
                    },
                    {
                        choiceItemId: "X",
                        choiceItemContent: "西瓜"
                    },
                    {
                        choiceItemId: "H",
                        choiceItemContent: "哈密瓜"
                    }
                ],
                choiceContent: "请选择", //选择的内容
                choiceIndex: 0 //选择位置
            }
        },
        methods: {
            // 修改选择的数据
            onChoiceClick: function(position) {
                console.log("+++++++" + position)
                var _this = this
                _this.choiceIndex = position
                _this.choiceContent = _this.choiceList[position].choiceItemContent
            },

        }
    }
</script>

<style>
    .selected-view {
        width: 80%;
        margin-top: 15rpx;
    }

    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
        height: 100%;
        background-color: white;
    }

    page {
        height: 100%;
        background-color: white;
    }
</style>
 

Demo

以上就是完成自定义下拉列表组件的所有代码,有什么问题或需要改进的可以留言共同探讨...

  • 7
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
uni-app中,你可以通过自定义组件来实现scroll-view的自定义下拉效果。下面是一个简单的示例: 1. 首先,创建一个自定义组件,比如命名为CustomScrollView。 在CustomScrollView.vue文件中,可以定义一个容器元素和一个下拉刷新的提示元素,如下所示: ```html <template> <view class="custom-scroll-view"> <view class="refresh-indicator" v-show="showRefreshIndicator"> <!-- 自定义下拉刷新的内容 --> <!-- ... --> </view> <scroll-view class="scroll-view-content"> <!-- scroll-view的内容 --> <!-- ... --> </scroll-view> </view> </template> <script> export default { data() { return { showRefreshIndicator: false, // 是否显示下拉刷新提示 startY: 0, // 记录开始滑动的位置 }; }, methods: { onTouchStart(e) { this.startY = e.touches[0].clientY; }, onTouchMove(e) { const currentY = e.touches[0].clientY; const distance = currentY - this.startY; if (distance > 0 && this.$refs.scrollView.scrollTop === 0) { // 下拉到顶部了,显示下拉刷新提示 this.showRefreshIndicator = true; } else { // 没有下拉到顶部,隐藏下拉刷新提示 this.showRefreshIndicator = false; } }, onTouchEnd() { if (this.showRefreshIndicator) { // 触发下拉刷新事件 this.$emit('refresh'); } this.showRefreshIndicator = false; }, }, }; </script> <style scoped> .custom-scroll-view { position: relative; height: 100%; } .refresh-indicator { position: absolute; top: -50px; /* 下拉刷新提示的高度 */ left: 0; right: 0; height: 50px; /* 下拉刷新提示的高度 */ } .scroll-view-content { height: 100%; } </style> ``` 2. 在使用CustomScrollView组件的页面中,可以引入该组件并监听其下拉刷新事件,如下所示: ```html <template> <view> <!-- ... --> <custom-scroll-view @refresh="onRefresh"> <!-- ... --> </custom-scroll-view> </view> </template> <script> import CustomScrollView from '@/components/CustomScrollView'; export default { components: { CustomScrollView, }, methods: { onRefresh() { // 处理下拉刷新逻辑 // ... }, }, }; </script> ``` 通过以上步骤,你就可以实现自定义下拉刷新效果了。当用户在CustomScrollView组件内部下拉到顶部时,会触发refresh事件,你可以在onRefresh方法中处理下拉刷新的逻辑,例如发送网络请求获取最新数据,然后更新页面内容。 需要注意的是,以上示例是基于uni-app框架的实现方式,如果你使用的是其他框架或原生开发,具体实现方式可能会有所不同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yyxhzdm

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

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

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

打赏作者

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

抵扣说明:

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

余额充值