• 小程序选择组件


    <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- 1px;
        border-style: solid;
        border-radius: 7upx;
        padding: 7upx 24upx;
        margin-right: 24upx;
        display: inline-flex;
        font-size: 14px;
      }
    </style>
    
  • 相关阅读:
    ubuntu实时显示网速cpu占用和内存占用率
    删除以....开头的所有文件
    0.0.....1 至 0.99.......9 之间正则
    引入腾讯视频播放,可控制是否暂停播放
    解决微信小程序textarea层级太高遮挡其他组件的问题
    查看某分支推送记录
    小程序下载canvas生成图片
    微信小程序企业付款到个人
    秒 转化为 时:分:秒 ------- 类似倒计时
    iOS--崩溃日志的格式化分析---格式化crash日志
  • 原文地址:https://www.cnblogs.com/li1234yun/p/11104649.html
Copyright © 2020-2023  润新知