微信小程序 TODO案例

源于 https://ask.csdn.net/questions/7759861 进行了简化。

效果

image

代码

index.js

Page({
  data: {
    // 待办列表
    todoList: [
      {
        thing: '洗衣服',
        completed: false
      },
      {
        thing: '刷鞋',
        completed: false
      },
      {
        thing: '写代码',
        completed: false
      }
    ],
    // 待办事务
    todoThing: ""
  },
  // 待办点击
  todoClick(e) {
    // 从wxml传入的点击的索引
    let index = e.currentTarget.dataset.index;
    this.setData({
      // 通过索引进行更改条目
      [`todoList[${index}].completed`]: !this.data.todoList[index].completed
    })
  },
  // 待办输入
  todoThingInput(e) {
    this.setData({ todoThing: e.detail.value })
  },
  // 添加按钮
  addClick() {
    this.setData({
      // 再数组最后追加一条
      [`todoList[${this.data.todoList.length}]`]: {
        thing: this.data.todoThing,
        completed: false
      },
      // 新增后清空输入框
      todoThing: ''
    })
  },
  // 删除按钮
  deleteClick(e) {
    // 从wxml传入的点击的索引
    let index = e.currentTarget.dataset.index;
    // 删除todoList索引为index的条目
    this.data.todoList.splice(index, 1)
    this.setData({
      // 对数组重新赋值
      todoList: this.data.todoList
    })
  }
})

index.wxml

<wxs module="tools">
  module.exports.getProgress = function (todo) {
    var length = todo.length;
    var checked = 0;
    for (var i = 0; i < todo.length; i++) {
      if (todo[i].completed) {
        checked++;
      }
    }
    return '完成度:' + (checked / length * 100).toFixed(2) + '%'
  }
</wxs>
<view class="todo-completion">{{tools.getProgress(todoList)}}</view>
<view class="todo-list">
  <view class="todo-item" wx:for="{{todoList}}" wx:key="unique" bindtap="todoClick" data-index="{{index}}">
    <!-- 点击条目增加删除线 -->
    <view class="todo-form-label {{item.completed?'delete-line':''}}">
      <radio checked="{{item.completed}}" /> {{item.thing}}
    </view>
    <!-- catchtap阻止冒泡并绑定事件 -->
    <view class="todo-delete" catchtap="deleteClick" data-index="{{index}}">删除</view>
  </view>
</view>
<view class="todo-form">
  <!-- bindconfirm  点击完成按钮时触发 -->
  <input class="form-item" type="text" placeholder="请输入想要做的事情" value="{{todoThing}}" bindinput="todoThingInput" bindconfirm="addClick" />
  <button type="primary" bindtap="addClick">添加</button>
</view>

index.wxss

.todo-form {
  padding: 20rpx 60rpx;
}

.todo-form .form-item {
  margin-bottom: 40rpx;
  font-size: 36rpx;
}

.todo-item .todo-form-label {
  /* 🚫阻止原有组件的事件 */
  pointer-events: none;
}

.delete-line {
  /* 给文字增加删除线 */
  text-decoration: line-through;
  color: rgba(0, 0, 0, .5);
}

.todo-list {
  padding: 30rpx 60rpx;
  display: flex;
  /* 通过样式来控制从头部追加 */
  flex-direction: column-reverse;
}

.todo-list .todo-item {
  margin-bottom: 40rpx;
  align-items: center;
  display: inline-flex;
  font-size: 28rpx;
  justify-content: space-between;
}

.todo-item .todo-delete {
  line-height: 48rpx;
  padding: 0 20rpx;
  font-size: 28rpx;
  color: #ff0000;
}

.todo-completion{
  text-align: center;
  padding: 20rpx 0;
}

完整代码 https://developers.weixin.qq.com/s/L1YEVWmD7LAo

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林一怂儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值