小程序样例1:简单待办列表

基本功能:

显示所有待办列表(点击不同的文本进行显示)

没完成的待办

已完成的待办

新建待办test

清除待办foo

代码js文件:

javascript 复制代码
//index.js
//获取应用实例
const app = getApp();
Page({
  data: {
    todo: '',
    todos: [
      {
        "id": 1474894720002,
        "todo": "foo",
        "completed": false
      },
      {
        "id": 1474894720922,
        "todo": "bar",
        "completed": true
      },
      {
        "id": 1474894723594,
        "todo": "baz",
        "completed": false
      }
    ],
    filterTodos: [],
    filter: 'all',
    activeCount: 0,
  },
  bindTodoInput(e) {
    this.setData({
      todo: e.detail.value
    });
  },
  saveTodo(e) {
    if (this.data.todo.trim().length === 0) {
      return;
    }

    const newTodo = {
      id: new Date().getTime(),
      todo: this.data.todo,
      completed: false,
    };
    
    this.setData({
      todo: '',
      todos: this.data.todos.concat(newTodo),
      filterTodos: this.data.filterTodos.concat(newTodo), 
      activeCount: this.data.activeCount + 1,
    });
  },

  todoFilter(filter, todos) {
    return filter === 'all' ? todos
      : todos.filter(x => x.completed === (filter !== 'active'));
  },
  toggleTodo(e) {
    const { todoId } = e.currentTarget.dataset;
    const { filter, activeCount } = this.data;
    let { todos } = this.data;
    let completed = false;
    todos = todos.map(todo => {
      if (Number(todoId) === todo.id) {
        todo.completed = !todo.completed;
        completed = todo.completed;
      }
      return todo;
    });
    const filterTodos = this.todoFilter(filter, todos);
    this.setData({
      todos,
      filterTodos,
      activeCount: completed ? activeCount - 1 : activeCount + 1,
    });
  },
  useFilter(e) {
    const { filter } = e.currentTarget.dataset;
    const { todos } = this.data;
    const filterTodos = this.todoFilter(filter, todos);
    this.setData({
      filter,
      filterTodos,
    });
  },
  clearCompleted() {
    const { filter } = this.data;
    let { todos } = this.data;
    todos = todos.filter(x => !x.completed);
    this.setData({
      todos,
      filterTodos: this.todoFilter(filter, todos),
    });
  },
  todoDel(e) {
    const { todoId } = e.currentTarget.dataset;
    const { filter, activeCount } = this.data;
    let { todos } = this.data;
    const todo = todos.find(x => Number(todoId) === x.id);
    todos = todos.filter(x => Number(todoId) !== x.id);
    this.setData({
      todos,
      filterTodos: this.todoFilter(filter, todos),
      activeCount: todo.completed ? activeCount : activeCount - 1,
    });
  },
  onLoad() {
    console.log('onLoad');
    const that = this;
    const activeCount = this.data.todos
          .map(x => x.completed ? 0 : 1)
          .reduce((a, b) => a + b, 0);
    that.setData({
      activeCount,
      filterTodos: this.data.todos
    });
  }
});

wxml:

html 复制代码
<scroll-view class="container" scroll-y="true">
  <view class="todo">
    <input class="new-todo"
      placeholder="添加待办列表"
      value="{{todo}}"
      bindinput="bindTodoInput"
    />
    <button type="primary" class="new-todo-save" bindtap="saveTodo">→</button>
  </view>
  
  <view class="todo-footer">
    <text class="total">{{activeCount}} 个待办</text>
    <view class="filter">
      <text bindtap="useFilter"
        data-filter="all"
        class="{{ filter === 'all' ? 'filter-item filter-active' : 'filter-item'}}"
       >所有</text>
      <text bindtap="useFilter"
        data-filter="active"
        class="{{ filter === 'active' ? 'filter-item filter-active' : 'filter-item'}}"       >待办</text>
      <text bindtap="useFilter"
        data-filter="completed"
        class="{{ filter === 'completed' ? 'filter-item filter-active' : 'filter-item'}}"
      >已完成</text>
    </view>
    <text wx:if="{{ todos.length - activeCount != 0 }}" class="clear" bindtap="clearCompleted">清除完成项</text>
    <text wx:else class="clear-empty"></text>
  </view>
  <view class="todo-list">
    <view class="todo-item" wx:for="{{filterTodos}}" wx:key="id">
      <icon bindtap="toggleTodo" class="todo-check"
        data-todo-id="{{item.id}}"
        type="{{ item.completed ? 'success_circle' : 'circle'}}" />
      <text class="{{ item.completed ? 'todo-content todo-completed' : 'todo-content'}}">{{item.todo}}</text>
      <icon bindtap="todoDel" class="todo-del" data-todo-id="{{item.id}}" type="cancel" />
    </view>
  </view>
</scroll-view>

wxss:

html 复制代码
/**index.wxss**/
.todo {
  margin: 20rpx;
  display: flex;
  align-items: center;
  background: #F5F5F5;
  height: 70rpx;
}

.new-todo {
  border: none;
  font-style: italic;
  width: 100%;
}

.new-todo-save {
  font-size: 28rpx
}

.todo-list {
  margin: 20rpx;
  display: flex;
  flex-direction: column;
  flex-grow: 2;
}

.todo-item {
  display: flex;
  height: 80rpx;
  position: relative;
}

.todo-check {
  margin-top: -6rpx;
}

.todo-del {
  margin-top: -6rpx;
  position: absolute;
  right: 20rpx;
}

.todo-content {
  margin-left: 20rpx;
}

.todo-completed {
  color: #d9d9d9;
  text-decoration: line-through;
}

.todo-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 80rpx;
  margin-left: 20rpx;
  margin-right: 20rpx;
  font-size: 24rpx;
}

.filter {
  display: flex;
  flex-direction: row;
}

.filter-item {
  margin-left: 10rpx;
  padding: 6rpx 14rpx;
}

.filter-active {
  border: 1px solid;
  border-color: rgba(175, 47, 47, 0.2);
}

.clear-empty {
  width: 120rpx;
  height: 24rpx;
}
相关推荐
码上成长12 分钟前
小程序请求层怎么封装?
前端·小程序
万岳科技系统开发14 分钟前
医疗诊所小程序如何实现患者管理和会员运营?
java·小程序·apache
java1234_小锋13 小时前
【免费】基于Python的微信小程序网约车(打车,在线叫车,移动出行)系统(FastAPI+Vue3) 锋哥原创出品,必属精品
微信小程序·小程序·fastapi·网约车系统·在线叫车系统
2501_9160074718 小时前
Bundle ID 注册与管理 App ID 创建指南 命名规则 权限开关与删除注意事项
android·ios·小程序·https·uni-app·iphone·webview
凡科网小帆1 天前
2026小程序商城做的比较好的品牌,怎么选小程序商城品牌
小程序·小程序商城
2501_915909061 天前
iOS 证书创建与管理 类型限制 p12 密码与云备份实操
android·ios·小程序·https·uni-app·iphone·webview
黄华SJ520it2 天前
顶俏洗衣液模式制度开发介绍:S2B2C社交分销+多门店核销系统全解析
前端·数据库·小程序·零售·系统开发
北极糊的狐2 天前
钉钉小程序报错data.formatTime is not a function是因为 axml 模板中不能直接调用 Page 内自定义方法!
java·小程序·钉钉
show4333 天前
多格式导出怎么做?2026免费小程序TXT/SRT实践
开发语言·小程序·c#
AI砖家3 天前
AI编程—微信小程序开发规范
微信小程序·小程序·notepad++·ai编程