微信小程序多选标签的实现(单选或者多选)

请添加图片描述
暑假留在社团跟别人一起开发一个校园小程序,如今也基本快开发完成了,整理一下日后可能用到的小组件。

类似于上图,下方的待选项为一个组件,根据父组件传入传入的参数决定是否为多选。
父组件的HTML代码如下

<view class="my-tag">
  <view class="tag-des" >
    <text>个人标签</text>
    <text 
      class="tips-text" 
      wx:if="{{ !alreadyselect }}">可多选</text>
    <view 
      wx:else="{{ alreadyselect }}"
      class="selected-my-tag">
      <block
        wx:for="{{ SelectMyTag }}"
        wx:key="index"
        >
        <view>{{ item.type }}</view>
      </block>
    </view>
  </view>
  
  <Tag bindonClickSelectTypeMulit='SelectMyTags' tags="{{ MyTagsList }}"/>
  <view style='height:30rpx;'></view>
</view>

接下来是CSS样式。

.my-tag{
  width: 682rpx;
  
  border-radius: 38rpx;
  background: #fff;
  box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.16);
  margin-top: 68rpx;
  margin-left: 34rpx;
}
.tag-des{
  padding-top: 34rpx ;
  margin-left: 34rpx;
  margin-right: 41rpx;
  font-weight: bold;
  font-size: 28rpx;
  border-bottom: 3rpx solid rgb(230, 230, 230);
  display: flex;
  align-items: center;
}
.tips-text{
  margin-left: 50rpx;
  color: #ADADAD;
  font-weight: normal;
  font-size: 28rpx;
}
.selected-my-tag{
  display: flex;
  flex-wrap:wrap ;
  margin-left: 50rpx;
}
.tag-des text{
  min-width: 112rpx;
}
.selected-my-tag view{
  margin-left: 30rpx;
  color: #ADADAD;
}

父组件的JS代码

 data: {
    MyTagsList: { 
      type: [ '运动达人',"新手求带" ,"王者大佬","剧本杀爱好者","快跑偷人啦","饮茶先啦落班先"],
      multichoice: true //多选,若单选不传参或为False
     },
    alreadyselect: false,
    SelectMyTag:[],}
   //选择个人标签
   SelectMyTags(e){
    this.setData({
      SelectMyTag : e.detail.type,
      alreadyselect:true
    })
    console.log(e.detail.type);
  },

子组件的代码

<view class="select-tag">
    <block>
      <view 
        wx:if="{{ !tags.multichoice }}"
        wx:for="{{ tags.type }}" 
        wx:key='index'
        data-index='{{ index }}'
        class="my-tag  {{ selected == index ? 'selected':'' }}"
        bindtap="onClickSelectType"
      >{{ item }}</view>
      <view 
        wx:if="{{ tags.multichoice }}"
        wx:for="{{ tags.type }}" 
        wx:key='index'
        data-index='{{ index }}'
        class="my-tag  {{ TagList[index].checked ? 'selected':'' }}"
        bindtap="onClickSelectType"
      >{{ item }}</view>
    </block>
    
  </view>
.select-tag{
  margin-left: 42rpx;
  display: flex;
  font-size: 22rpx;
  line-height: 40rpx;
  display: flex;
  flex-wrap: wrap;
  margin-top: 20rpx;
}
.my-tag{
  color: #787CF9;
  min-width: 120rpx;
  padding: 0 20rpx;
  height: 40rpx;
  text-align: center;
  border-radius: 20rpx;
  background-color: #F7F3FD;
  margin-right: 30rpx;
  margin-top: 20rpx;
}
.selected{
  background-color:#9573E7 !important;
  color:white
}
// components/TeamTag/index.js
Component({
  /**
   * tags:{}
   */
  properties: {
    CurrentSelected: {
      type:Number,
      value:0,
      observer:function(newvalue){
        this.setData({
          selected:newvalue
        })
      }
    },
    tags: {
      type: Object,
      value:{
       type: ['学习局','运动局','游戏局','其他']
      },
      multichoice: false //不能多选
    }
  },

  /**
   * 组件的初始数据
   */
  data: { 
    selected:0,
    AlreadySelected:[ ],
    TagList:[]
  },
  lifetimes:{
    attached(){
      const NewTypeArr =[]
      if(this.properties.tags.multichoice){
        this.properties.tags.type.forEach(item => {
          NewTypeArr.push({type:item,checked:false})
        })
        this.setData({
          TagList:NewTypeArr
        })
        
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onClickSelectType(e){
      const {index} = e.currentTarget.dataset
      //console.log(index);
      //判断是否为多选
      if(this.properties.tags.multichoice==true){
        let SelectArr = this.data.AlreadySelected
        let taglist = this.data.TagList
        //判断是否选中
        let isselected = SelectArr.findIndex(item => this.properties.tags.type[index]==item.type)
      //  console.log(isselected);
        //已经选中,重复点击更改为未选中的状态
        if(isselected!=-1) {         
          SelectArr.splice(isselected,1);
          taglist[index].checked=false
          this.setData({
            selected:index,
            AlreadySelected:[].concat(SelectArr),
            TagList: [].concat(taglist)
          })
        }
        else{
          SelectArr.push({ type:this.properties.tags.type[index],id:index })
          taglist[index].checked=true
          this.setData({
            selected:index,
            AlreadySelected:[].concat(SelectArr),
            TagList: [].concat(taglist)
          })
        }
        //console.log(this.data.AlreadySelected);
        //选中的标签
        this.triggerEvent("onClickSelectTypeMulit",{type:this.data.AlreadySelected})
      }
      else{
        this.setData({
          selected:index
        })
        this.triggerEvent("onClickSelectType",{index,type:this.properties.tags.type[index]})
      }
     
     
    },
  }
})

  • 1
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
微信小程序中可以通过使用组件来实现单选多选和全选功能。 单选: 可以使用radio组件来实现单选功能。radio组件需要与radio-group组件配合使用。当用户点击其中一个radio后,radio-group会自动将其它的radio选项取消选中。 多选: 可以使用checkbox组件来实现多选功能。checkbox组件需要与checkbox-group组件配合使用。当用户点击某个checkbox时,checkbox-group会自动将其它的checkbox选项保持原有状态。 全选: 可以使用checkbox组件来实现全选功能。需要在checkbox组件中加入一个value属性,并将其绑定到全选按钮的checked属性上。当用户点击全选按钮时,将全选按钮的状态传递给所有的checkbox选项即可。 示例代码: 单选: ``` <radio-group> <label wx:for="{{list}}" wx:key="{{index}}"> <radio value="{{item.value}}">{{item.name}}</radio> </label> </radio-group> ``` 多选: ``` <checkbox-group> <label wx:for="{{list}}" wx:key="{{index}}"> <checkbox value="{{item.value}}">{{item.name}}</checkbox> </label> </checkbox-group> ``` 全选: ``` <checkbox value="all" checked="{{allChecked}}" bindchange="onAllChange">全选</checkbox> <checkbox-group> <label wx:for="{{list}}" wx:key="{{index}}"> <checkbox value="{{item.value}}" checked="{{item.checked}}">{{item.name}}</checkbox> </label> </checkbox-group> ``` 其中,allChecked和item.checked为数据中的属性,需要在js文件中定义和更新。onAllChange为全选按钮的change事件处理函数,代码如下: ``` onAllChange: function (e) { const isChecked = e.detail.value.length > 0; const list = this.data.list.map(item => { item.checked = isChecked; return item; }); this.setData({ allChecked: isChecked, list }); } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值