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

news/2024/7/20 2:46:19 标签: 小程序, apache

基本功能:

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

没完成的待办

已完成的待办

新建待办test

清除待办foo

代码js文件:

//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:

<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:

/**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;
}


http://www.niftyadmin.cn/n/5331067.html

相关文章

大数据开发之Hadoop(入门)

第 1 章&#xff1a;Hadoop概述 1.1 Hadoop是什么 1、Hadoop是一个由Apache基金会所开发的分布式系统基础架构。 2、主要解决&#xff0c;海量数据的存储和海量数据的分析计算问题。 3、Hadoop通常是指一个更广泛的概念-Hadoop生态圈 1.2 Hadoop优势&#xff08;4高&#xf…

【LeetCode】206. 反转链表(简单)——代码随想录算法训练营Day03

题目链接&#xff1a;206. 反转链表 题目描述 给你单链表的头节点 head &#xff0c;请你反转链表&#xff0c;并返回反转后的链表。 示例 1&#xff1a; 输入&#xff1a;head [1,2,3,4,5] 输出&#xff1a;[5,4,3,2,1] 示例 2&#xff1a; 输入&#xff1a;head [1,2] 输…

Redis主从架构、哨兵集群原理实战

1.主从架构简介 背景 单机部署简单&#xff0c;但是可靠性低&#xff0c;且不能很好利用CPU多核处理能力生产环境必须要保证高可用&#xff0c;一般不可能单机部署读写分离是可用性要求不高、性能要求较高、数据规模小的情况 目标 读写分离&#xff0c;扩展主节点的读能力&…

Docker 项目如何使用 Dockerfile 构建镜像

1、Docker 和 Dockerfile 的重要性 1.1、Docker 简介&#xff1a;讲述 Docker 的起源、它是如何革新现代软件开发的&#xff0c;以及它为开发者和运维团队带来的好处。重点强调 Docker 的轻量级特性和它在提高应用部署、扩展和隔离方面的优势。 1.2、Dockerfile 的作用&#…

天猫企业店铺商家电话采集软件使用教程

使用天猫企业店铺商家电话采集软件可以帮助我们快速获取天猫店铺的商家电话&#xff0c;提供了便利的信息获取方式。下面是一个简单的使用教程&#xff0c;并附带相应的代码。 首先&#xff0c;我们需要准备环境和工具。 Python环境&#xff1a;请确保您的电脑上已经安装了Pyt…

java将word转换成pdf,并去除水印

注意我这里只是将word字节替换成pdf字节&#xff0c;如果是文件根据自己实际情况来做 1、所需jar包 <dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>15.8.0</version></dependency&g…

微信小程序---封装uni.$showMsg()方法

当数请求失败之后&#xff0c;经常需要调用 uni.showToast(( /* 配置对象/ 方法来提示用户时&#xff0c;可以在全局封装一个 uni.showMsg()方法&#xff0c;来简化 uni.showToast() 方法的调用。具体的改造步骤如下: 1.在main.js中&#xff0c;为uni对象挂载自定义的$showMsg…

计算机毕业设计 基于SSM的历史/博物馆藏系统的设计与实现 Java实战项目 附源码+文档+视频讲解

博主介绍&#xff1a;✌从事软件开发10年之余&#xff0c;专注于Java技术领域、Python人工智能及数据挖掘、小程序项目开发和Android项目开发等。CSDN、掘金、华为云、InfoQ、阿里云等平台优质作者✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精…