About the implementation of WeChat mini program collection function

不言
Release: 2018-06-26 15:41:31
Original
7030 people have browsed it

This article mainly introduces the implementation code of the WeChat applet collection function. The basic function is to display the collected items after clicking on the collection, and the currently clicked collection item will appear on another page. Friends who need it can refer to

Requirements

After clicking the collection, it will show that it has been collected, and the currently clicked collection will appear on another page. Project

Problems that need to be solved

  1. After clicking to favorite It needs to show that it has been collected, and the text status changes

  2. How does another page know that you clicked to collect, and get the data you clicked to collect

How to solve?

  1. Data state binding, and cached by state control style (ternary operator)

  2. (setStorageSync, getStorageSync), click on the page to set the cache (data id), display the page to obtain the cache, and by obtaining the cache id, take out the obtained id item in the entire data and put it into a new array

Specific implementation

wxml

<image class="save " src="{{isClick?&#39;../../youzan-image/save-s.png&#39;:&#39;../../youzan-image/save.png&#39;}}" bindtap="haveSave"></image>
   <text class="saveText">{{isClick?&#39;已收藏&#39;:&#39;收藏&#39;}}</text>
Copy after login

Click the page js

 Page({
  data: {
  job: [],
  jobList: [],
  id: &#39;&#39;,
  isClick: false,
  jobStorage: [],
  jobId: &#39;&#39;
  },
  haveSave(e) {
  if (!this.data.isClick == true) {
   let jobData = this.data.jobStorage;
   jobData.push({
   jobid: jobData.length,
   id: this.data.job.id
   })
   wx.setStorageSync(&#39;jobData&#39;, jobData);//设置缓存
   wx.showToast({
   title: &#39;已收藏&#39;,
   });
  } else {
   wx.showToast({
   title: &#39;已取消收藏&#39;,
   });
  }
  this.setData({
   isClick: !this.data.isClick
  })
  }
 })
Copy after login

to display the page js

import jobList from &#39;../../api/detail&#39;
Page({
 data: {
 id:&#39;&#39;,
 job:[],
 savejob:[],
 },
 onLoad: function (options) {
 console.log(wx.getStorageSync(&#39;jobData&#39;));
 let savejob = wx.getStorageSync(&#39;jobData&#39;)//获得缓存
 let index = savejob.length-1;
 console.log(savejob[index].id);
 let jobid = savejob[index].id
 let temp= jobList[jobid] //将获得缓存后匹配的数据放入新的数组
 let job= [];
 job.push(temp);
 this.setData({
  id:index,
  job: job,
 })
 },
})
Copy after login

The above is the entire content of this article , I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

WeChat Mini Program implements the functions of liking, deleting lists and sharing

WeChat Mini Program Implementation of scrolling message notifications

WeChat applet dynamically displays the effect of project countdown

The above is the detailed content of About the implementation of WeChat mini program collection function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!