微信小程序评论区功能实现,有删除评论功能,本地数据例如数组,界面显示一共有多少条评论还有评论内容要有排序比如123之类,案例代码展示
时间: 2024-03-05 15:53:29 浏览: 25
好的,以下是一个简单的微信小程序评论区功能实现案例代码展示,使用的是本地数据数组:
```html
<!-- comments.wxml -->
<view class="container">
<!-- 评论列表 -->
<view class="comment-list">
<view class="comment-item" wx:for="{{comments}}" wx:key="index">
<view class="comment-info">
<text class="comment-name">{{item.name}}</text>
<text class="comment-time">{{item.time}}</text>
<button class="delete-btn" data-index="{{index}}" bindtap="onDeleteComment">删除</button>
</view>
<view class="comment-content">{{item.content}}</view>
</view>
</view>
<!-- 添加评论表单 -->
<form bindsubmit="onSubmitComment">
<input name="name" type="text" placeholder="请输入您的姓名" />
<textarea name="content" placeholder="请输入您的评论"></textarea>
<button type="submit">提交</button>
</form>
<!-- 评论总数 -->
<view class="comment-count">共 {{comments.length}} 条评论</view>
</view>
```
```javascript
// comments.js
Page({
data: {
comments: [],
},
onLoad: function () {
// 从本地存储中获取评论数据
const comments = wx.getStorageSync('comments') || [];
this.setData({ comments });
},
onSubmitComment: function (event) {
const { name, content } = event.detail.value;
const time = new Date().toLocaleString();
const comment = { name, content, time };
const comments = this.data.comments.concat(comment);
// 根据时间倒序排序
comments.sort((a, b) => new Date(b.time) - new Date(a.time));
this.setData({ comments });
wx.setStorageSync('comments', comments);
wx.showToast({ title: '评论成功' });
},
onDeleteComment: function (event) {
const { index } = event.currentTarget.dataset;
const comments = this.data.comments.filter((item, idx) => idx !== index);
this.setData({ comments });
wx.setStorageSync('comments', comments);
},
});
```
以上代码实现了评论区的基本功能,包括添加评论、删除评论、展示评论列表、展示评论总数以及根据时间排序展示评论列表等功能。当用户添加或删除评论时,数据会保存在本地存储中,以便下次进入小程序时可以加载之前的评论数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)