微信小程序 自定义组件---范围段的填写及选中

效果图:

调用处:(例如在index页面调用)

index.json(input_select为组件的名称)

{

"usingComponents": {

"input_select": "/component/selector/input_select/input_select",

}

}

index.js

Page({

/*

* 页面的初始数据

*/

data: {

content: "自定义组件",

number_list: [{

key: "100",

name: "100以下"

}, {

key: "200",

name: "200以下"

}, {

key: "300",

name: "300以下"

}]

},

/**

* 生命周期函数--监听页面初次渲染完成

*/

onReady: function() {

//获得input_select组件

this.input_select = this.selectComponent("#input_select");

},

此处省略微信小程序的部分方法

//*************************单选带输入框按钮*******************************

showDialog: function() {

this.input_select.showDialog();

},

//取消事件

_inputSelectCancel() {

console.log('你点击了取消');

this.input_select.hideDialog();

},

//确认事件

_inputSelectConfirm(e) {

console.log('你点击了确定==', e.detail);

this.input_select.hideDialog();

},

})

index.wxml:

<view catchtap="showDialog">
  单选带输入框按钮
  <input_select id="input_select" bind:inputSelectCancel="_inputSelectCancel" bind:inputSelectConfirm="_inputSelectConfirm" content='{{content}}' number_list="{{number_list}}">
  </input_select>
</view>

自定义组件部分(****)

     命名为:input_select

input_select.wxml

<!--component/selector/singer.wxml-->
<view class='singer-bg' wx:if="{{isShow}}">
  <view class='singer-body'>
    <view class='singer-body-name'>
      <view class='singer-body-name-line'></view>
      <view class='singer-body-name-txt'>{{content}}</view>
      <view class='singer-body-name-line'></view>
    </view>
    <view class='singer-body-double'>
      <view class='double-input'>
        <input class='double-input-txt' value='{{json.start}}' placeholder='开始值' bindblur='changestart' bindconfirm='changestart' bindinput='changestart'></input>
      </view>
      <view class='double-input'>
        <input class='double-input-txt' value='{{json.end}}' placeholder='结束值' bindblur='changeend' bindconfirm='changeend' bindinput='changeend'></input>
      </view>
    </view>

    <view class='singer-body-list'>
      <block wx:for="{{number_list}}" wx:for-item="item" wx:key="unique">
        <view class='body-list-item-choosed' wx:if="{{json.start==item.key}}" data-key='{{item.key}}' bindtap='chooseNumber'>{{item.name}}</view>
        <view class='body-list-item'data-key='{{item.key}}' bindtap='chooseNumber' wx:else>{{item.name}}</view>
      </block>
    </view>
    <view class='singer-body-kongbai'></view>
    <view class='singer-body-icon'>
      <view class='icon-left' catchtap='_inputSelectCancel'>重置</view>
      <view class='icon-right' catchtap='_inputSelectConfirm'>确定</view>
    </view>
  </view>
</view>

input_select.js

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //标题文字
    content: {
      type: String,
      value: ''
    },
    number_list: {
      type: Array,
      value: [{
        key: '',
        name: ''
      }, ]
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isShow: false,
    json: {
      start: "",
      end: "",
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    chooseNumber: function(e) {
      this.setData({
        json: {
          start: e.currentTarget.dataset.key,
          end: this.data.json.end,
        }
      })
    },
    changestart: function(event) {
      var start = event.detail.value;
      this.setData({
        json: {
          start: start,
          end: this.data.json.end,
        }
      })
    },
    changeend: function(event) {
      var end = event.detail.value;
      this.setData({
        json: {
          start: this.data.json.start,
          end: end,
        }
      })
    },
    //隐藏弹框
    hideDialog: function() {
      this.setData({
        isShow: false,
        json: {
          start: "",
          end: "",
        }
      })
    },
    //展示弹框
    showDialog: function() {
      this.setData({
        isShow: true,
      })
    },
    /*
     * 内部私有方法建议以下划线开头
     * triggerEvent 用于触发事件
     */
    _inputSelectCancel() {
      //触发取消回调
      this.triggerEvent("inputSelectCancel")
    },
    _inputSelectConfirm() {
      //触发成功回调
      this.triggerEvent("inputSelectConfirm", this.data.json);
    }
  }
})

input_select.json

{
  "component": true
}

input_select.wxss

/* component/selector/singer.wxss */

.singer-bg {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.5);
}

.singer-bg .singer-body {
  min-width: 100%;
  width: 100%;
  height: 968rpx;
  max-height: 968rpx;
  background: rgba(255, 255, 255, 1);
  border-radius: 20px 20px 0px 0px;
  position: absolute;
  left: 0;
  bottom: 0;
}

.singer-bg .singer-body .singer-body-name {
  width: 100%;
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  margin-top: 60rpx;
  margin-bottom: 50rpx;
}

.singer-bg .singer-body .singer-body-name .singer-body-name-line {
  width: 260rpx;
  border-bottom: 2rpx solid rgba(240, 240, 240, 1);
}

.singer-bg .singer-body .singer-body-name .singer-body-name-txt {
  height: 48rpx;
  font-size: 34rpx;
  font-family: PingFangSC-Medium;
  font-weight: 500;
  color: rgba(0, 0, 0, 1);
  line-height: 48rpx;
  margin-left: 10rpx;
  margin-right: 10rpx;
}

.singer-bg .singer-body .singer-body-kongbai {
  width: 100%;
  height: 120rpx;
}

.singer-bg .singer-body .singer-body-icon {
  width: 100%;
  height: 110rpx;
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
  position: fixed;
  bottom: 0;
  z-index: 9999999999;
  background: rgba(255, 255, 255, 1);
}

.singer-bg .singer-body .singer-body-icon .icon-left {
  width: 374rpx;
  height: 110rpx;
  background: rgba(0, 89, 179, 0.1);
  font-size: 36rpx;
  font-family: PingFangSC-Regular;
  font-weight: 400;
  color: rgba(0, 89, 179, 1);
  line-height: 50rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.singer-bg .singer-body .singer-body-icon .icon-right {
  width: 374rpx;
  height: 110rpx;
  background: rgba(0, 89, 179, 1);
  font-size: 36rpx;
  font-family: PingFangSC-Regular;
  font-weight: 400;
  color: rgba(255, 255, 255, 1);
  line-height: 50rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.singer-bg .singer-body .singer-body-double {
  width: 100%;
  max-width: 100%;
  display: flex;
  flex-wrap: nowrap;
}

.singer-bg .singer-body .singer-body-double .double-input {
  width: 320rpx;
  height: 78rpx;
  background: rgba(255, 255, 255, 1);
  border-radius: 6rpx;
  border: 1px solid rgba(204, 204, 204, 1);
  margin-left: 40rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.singer-bg .singer-body .singer-body-double .double-input .double-input-txt {
  width: 280rpx;
  height: 78rpx;
  font-size: 30rpx;
  font-family: PingFangSC-Medium;
  font-weight: 500;
  color: rgba(26, 26, 26, 1);
  padding: 0rpx 20rpx 0rpx 20rpx;
}

.singer-bg .singer-body .singer-body-list {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  padding: 0rpx 0rpx 0rpx 30rpx;
  margin-top: 50rpx;
}

.singer-bg .singer-body .singer-body-list .body-list-item {
  width: 212rpx;
  height: 70rpx;
  background: rgba(240, 240, 240, 1);
  border-radius: 35rpx;
  margin-right: 30rpx;
  margin-bottom: 30rpx;
  font-size: 30rpx;
  font-family: PingFangSC-Regular;
  font-weight: 400;
  color: rgba(51, 51, 51, 1);
  display: flex;
  align-items: center;
  justify-content: center;
}

.singer-bg .singer-body .singer-body-list .body-list-item-choosed {
  width: 212rpx;
  height: 70rpx;
  background: rgba(0, 89, 179, 0.25);
  border-radius: 35rpx;
  margin-right: 30rpx;
  margin-bottom: 30rpx;
  font-size: 30rpx;
  font-family: PingFangSC-Regular;
  font-weight: 400;
  color: rgba(0, 89, 179, 1);
  display: flex;
  align-items: center;
  justify-content: center;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序的原生 picker-view 是一种可以进行滚动选择组件。它可以在小程序中展示一个滚动的选择器,用户可以通过上下滑动来选择需要的选项。 使用 picker-view 的步骤如下: 1. 在 wxml 文件中,通过<picker-view>标签来定义一个 picker-view 组件,可以通过style属性来设置它的样式。 2. 在 js 文件中,首先需要通过页面的setData()方法来初始化 picker-view 组件的数据源,使用数组的形式来表示选项。 3. 在 wxml 文件中,通过<picker-view-column>标签来定义 picker-view 中的每一列。可以使用wx:for属性来循环渲染每一列的选项。 4. 在 js 文件中,可以通过 onLoad()或者onShow()等生命周期方法来初始化 picker-view 组件选中项。可以通过setData()方法更新 selected 属性的值来改变选中项。 5. 在 wxml 文件中,通过<view>标签来定义一个按钮或者其他交互元素,通过bindtap属性来绑定事件处理函数。 6. 在 js 文件中,实现事件处理函数,并通过setData()方法更新 selected 值以改变选中项。 7. 可以通过onPickerChange()等事件来监听 picker-view 的选中项改变的事件,并实现相应的逻辑。 需要注意的是,picker-view 组件的数组类型数据源,每一项都需要有value和text两个属性,分别表示选项的值和显示文本。 通过以上步骤,就可以在微信小程序中使用原生 picker-view 组件实现选择器的功能。用户可以滚动选择选项,并通过自定义的事件处理函数来获取选中项的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值