微信小程序 —— 组件 { }

该示例展示了如何在微信小程序中通过父组件将数据传递给子组件,并在子组件中实现搜索过滤功能。父组件`search`传递了一个包含多个项目(如JavaScript、Vue等)的列表,子组件`todolist`接收这个列表,并在用户输入搜索关键字时实时更新显示的项目。当输入匹配项时,相应的项目变得活跃,否则隐藏。这个例子涉及到了数据绑定、事件触发和条件渲染等关键概念。
摘要由CSDN通过智能技术生成
父组件
  • search.wxml
<!-- 将数据传递给子组件 -->
<Tabs list="{{list}}" binditemChange="handleItemChange"></Tabs>
  • search.js
Page({
  data: {
    list: [
      {
        id: 0,
        name: 'JavaScript',
        isDone: false,
        isActive: true
      },
      {
        id: 1,
        name: 'Vue',
        isDone: false,
        isActive: true
      },
      {
        id: 2,
        name: 'Nodejs',
        isDone: false,
        isActive: true
      },
      {
        id: 3,
        name: 'Git',
        isDone: false,
        isActive: true
      },
      {
        id: 4,
        name: 'jQuery',
        isDone: false,
        isActive: true
      },
      {
        id: 5,
        name: 'Java',
        isDone: false,
        isActive: true
      },
      {
        id: 6,
        name: 'C++',
        isDone: false,
        isActive: true
      },
      {
        id: 7,
        name: 'eCharts',
        isDone: false,
        isActive: true
      }
    ]
  },

  handleItemChange(e) {
    const { list } = this.data;

    let { value } = e.detail;

    list.forEach((item) => {
      const itemName = item.name.toLowerCase()
      if (itemName.includes(value)) {
        item.isActive = true
      } else {
        item.isActive = false
      }
    })
    this.setData({
      list
    })
  }
})
子组件
  • todolist.wxml
<view class="container">
  <view class="searchBox">
    <input type="text" class="searchInput" bindinput="handleInput" />
    <image src="../../icon/search.png" class="searchImg" mode="aspectFit" />
  </view>

  <view class="todoListBox">
    <view wx:for="{{list}}" wx:key="id" class="todolist" hidden="{{!item.isActive}}">
      <checkbox class="cb">{{item.name}}</checkbox>
    </view>
    <!-- <view wx:for="{{list}}" wx:key="id" class="todolist {{item.isActive ? '' : 'notactive'}}">
      <checkbox class="cb">{{item.name}}</checkbox>
    </view> -->
  </view>
</view>
  • todolist.wxss
.container {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100%;
}

.searchBox {
  display: flex;
}
.searchInput {
  padding: 5px;
  border: 1px solid black;
  border-radius: 10px;
  margin-right: 10px;
}
.searchImg {
  width: 30px;
  height: 30px;
}

.todoListBox {
  width: 400px;
  height: 300px;
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.todolist {
  width: 250px;
}
.cb {
  margin-bottom: 5px;
}

.notactive {
  display: none;
}
  • todolist.js
Component({
  properties: {
    list: {
      type: Array,
      value: []
    }
  },

  data: {

  },

  methods: {
    handleInput(e) {

      let { value } = e.detail
      value = value.toLowerCase()

      this.triggerEvent("itemChange",{value})

    }
  }
})
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

微风拂晚霞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值