2022-03-25 【待办Todo】day04-渲染数据

一、渲染待办列表数据

1. 使用 v-for 循环渲染

<van-cell-group v-for="item in items" :key="item.id">
  ...
</van-cell-group>

v-for:循环渲染指令
items:data 中的数组
item:数组中的每一项
item.id:每一项的索引
:key:用于提升性能

2. 修改 Cell 单元格组件

<van-cell-group inset v-for="item in todoList" :key="item._id">
  <van-cell center :title="item.name" value="开始" :label="item.time + '分钟'" />
</van-cell-group>

3. 效果预览

在这里插入图片描述

二、实现滑动编辑和删除

1. 使用 SwipeCell 滑动单元格组件

在这里插入图片描述

2. html 部分代码

<van-cell-group inset v-for="item in todoList" :key="item._id">
  <van-swipe-cell>
	<template #left>
	  <van-button square type="primary" text="编辑" />
	</template>
	<van-cell center :title="item.name" value="开始" :label="item.time + '分钟'" />
	<template #right>
	  <van-button square type="danger" text="删除" />
	</template>
  </van-swipe-cell>
</van-cell-group>

3. css 部分代码

.van-cell-group {
  margin: 16px;
  .van-swipe-cell {
    .van-cell {
      background-color: #ffe;
    }
    .van-button {
      height: 100%;
    }
  }
}

三、点击编辑获取待办详情

1. 为点击事件绑定方法并传参

<van-button square type="primary" text="编辑" @click="editTodo(item._id)" />

2. 使用参数查询并显示弹窗

editTodo (id) {
  const db = this.$store.state.app.database()
  const todo = db.collection('todo')
  todo.doc(id).get().then(res => {
	this.todoForm = res.data[0]
	this.showDialog = true
  })
}

3. 效果预览

在这里插入图片描述

四、使用一个字段标记添加和编辑行为

1. 在 data 中添加一个 isUpdate 字段,值为 false

这里定义 isUpdate 的值是 false 时为添加待办,是 true 时为编辑待办

2. 修改 editTodo 方法

在开头设置 this.isUpdate = true

3. 修改弹窗部分代码

<van-dialog v-model="showDialog" :title="isUpdate ? '编辑待办' : '添加待办'" show-cancel-button :before-close="beforeClose" @closed="reset" @confirm="submit">
  ...
</van-dialog>

五、点击确认提交更新数据

1. 根据 isUpdate 的值判断行为

提交后先判断 isUpdate 的值,若为 true 则执行编辑操作,否则执行添加操作

2. 修改 setTodo 方法

代码如下

setTodo () {
  const db = this.$store.state.app.database()
  const todo = db.collection('todo')
  if (this.isUpdate) {
	todo.doc(this.todoForm._id).update({
	  name: this.todoForm.name,
	  time: this.todoForm.time
	}).then(() => {
	  this.$toast({
		message: '编辑成功',
		position: 'top'
	  })
	  this.showDialog = false
	  this.getTodo()
	})
  } else {
	todo.add(this.todoForm).then(() => {
	  this.$toast({
		message: '添加成功',
		position: 'top'
	  })
	  this.showDialog = false
	  this.getTodo()
	})
  }
}

3. 弹窗关闭后重置 isUpdate 为 false

修改 reset 方法

reset () {
  this.todoForm = this.$options.data().todoForm
  this.$refs.todoFormRef.resetValidation()
  this.isUpdate = false
}

六、实现删除待办功能

1. 为删除按钮添加点击事件

绑定删除待办的方法并传入参数

<van-button square type="danger" text="删除" @click="removeTodo(item._id)" />

2. 添加 removeTodo 方法

代码如下

removeTodo (id) {
  this.$dialog.confirm({
	message: '确定删除吗?'
  }).then(() => {
	const db = this.$store.state.app.database()
	const todo = db.collection('todo')
	todo.doc(id).remove().then(() => {
	  this.$toast({
		message: '删除成功',
		position: 'top'
	  })
	  this.getTodo()
	})
  }).catch(() => {})
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用C语言实现的单链表原地逆转的代码: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void reverseList(struct Node** head) { struct Node* prev = NULL; struct Node* current = *head; struct Node* next = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } *head = prev; } void printList(struct Node* head) { while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } void push(struct Node** headRef, int newData) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = newData; newNode->next = *headRef; *headRef = newNode; } int main() { struct Node* head = NULL; push(&head, 3); push(&head, 2); push(&head, 1); printf("Original list: "); printList(head); reverseList(&head); printf("Reversed list: "); printList(head); return 0; } ``` 在上述代码中,我们首先定义了一个 `Node` 结构体来表示单链表中的每个节点,包括节点的值和指向下一个节点的指针。然后我们定义了 `reverseList` 函数来实现原地逆转单链表的功能。该函数接受一个指向指针的指针 `head`,这是因为我们需要通过指针来修改单链表的头节点,所以我们传递指向指针的指针。在函数内部,我们使用三个指针 `prev`、`current` 和 `next` 来依次遍历单链表,并将每个节点的指针指向前一个节点,从而实现原地逆转单链表的目的。 最后,我们定义了一个 `push` 函数来添加新节点到单链表的头部,并定义了 `printList` 函数来打印单链表中所有节点的值。在 `main` 函数中,我们创建了一个包含三个节点的单链表,并调用 `reverseList` 函数来原地逆转该单链表,最后打印出原始和逆转后的单链表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值