小程序选择组件

Posted li1234yun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小程序选择组件相关的知识,希望对你有一定的参考价值。

<template>
  <view class="checkbox-container">
    <view v-for="(item, index) in checkboxListData" :key="index" class="checkbox-item" :style="'border-color': typeColor[type], 'background': item.isChecked? typeColor[type] : '#ffffff'"
      @click="checkboxItemClick(index, item)">
      <view :style="'color': item.isChecked? '#ffffff' : typeColor[type] ">item.name</view>
    </view>
  </view>
</template>

<script>
  export default 
    model: 
      prop: 'selectedList',
      event: 'selectedListChange'
    ,
    props: 
      checkboxList: 
        type: Array,
        default: () => [
            name: "t1",
            id: 1
          ,
          
            id: 2,
            name: "t2"
          
        ]
      ,
      selectedList: 
        type: Array,
        default: () => [
          name: 't1',
          id: 1
        ]
      ,
      type: 
        type: String,
        default: "success" // primary/success/danger
      ,
      isSingle: 
        type: Boolean,
        default: false
      
    ,
    data() 
      return 
        checkboxListData: [],
        typeColor: 
          primary: "#007bff",
          success: "#8BC34A",
          danger: "#dc3545"
        ,
      ;
    ,
    watch: 
      selectedList(newVal) 
        console.log('select list new value:', newVal)
        this.getCheckboxListData(newVal)
      
    ,
    
    mounted() 
      // 获取checkbox list data
      this.getCheckboxListData()
    ,
    
    methods: 
      getCheckboxListData(selectedList) 
        // 获取checkbox list data
        if (selectedList === undefined) 
          selectedList = this.selectedList
         
        const checkboxListData = []
        for (const i in this.checkboxList) 
          checkboxListData.push(Object.assign(
            isChecked: false
          , this.checkboxList[i]))

          for (const j in selectedList) 
            if (this.checkboxList[i].id === selectedList[j].id) 
              checkboxListData[i].isChecked = true
              break
            
          
        
        this.checkboxListData = checkboxListData
        console.log('mounted list data:', this.checkboxListData)
      ,

      updateCheckboxListData(listItem) 
        const selectedList = []
        for (const i in this.checkboxListData) 
          if (this.checkboxListData[i].id === listItem.id) 
            if (this.isSingle) 
              this.checkboxListData[i].isChecked = true
             else 
              this.checkboxListData[i].isChecked = !this.checkboxListData[i].isChecked
            
           else 
            if (this.isSingle) 
              this.checkboxListData[i].isChecked = false
            
          
          
          if (this.checkboxListData[i].isChecked) 
            selectedList.push(this.checkboxListData[i])
          
        
        return selectedList
      ,

      checkboxItemClick(index, item) 
        console.log('checkbox item click:', index, item)
        const result = this.updateCheckboxListData(item)
        
        console.log('selected list:', result)
        this.$emit('selectedListChange', result)
      
    
  ;
</script>

<style scoped>
  .checkbox-container 
    padding: 14upx 0;
  

  .checkbox-item 
    border-width: 1px;
    border-style: solid;
    border-radius: 7upx;
    padding: 7upx 24upx;
    margin-right: 24upx;
    display: inline-flex;
    font-size: 14px;
  
</style>

以上是关于小程序选择组件的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序 怎么 选择组件 做操作

支付宝小程序日期选择组件datePicker封装

微信小程序-自定义picker选择器

微信小程序自定义picker年月日时分秒选择器组件

微信小程序自定义picker组件

小程序日期(日历)时间 选择器组件