huahuazenmehua/pages/category/category.js

44 lines
1.3 KiB
JavaScript

import { injectPage } from '@jdmini/api'
const { CATEGORIES, ALL_COURSES } = require('../../utils/data.js')
Page(injectPage()({
data: {
categories: CATEGORIES,
activeCategory: '',
courseList: [],
keyword: ''
},
onLoad(options) {
const category = options.category ? decodeURIComponent(options.category) : CATEGORIES[0]
const keyword = options.keyword ? decodeURIComponent(options.keyword) : ''
this.setData({ activeCategory: category, keyword })
this.filterCourses(category, keyword)
},
filterCourses(category, keyword) {
let list = ALL_COURSES
if (keyword) {
list = list.filter(c => c.title.includes(keyword) || c.desc.includes(keyword) || c.category.includes(keyword))
} else {
list = list.filter(c => c.category === category)
}
this.setData({ courseList: list })
},
onCategorySwitch(e) {
const cat = e.currentTarget.dataset.category
this.setData({ activeCategory: cat, keyword: '' })
this.filterCourses(cat, '')
},
onCourseTap(e) {
const { courseId } = e.currentTarget.dataset
wx.navigateTo({ url: `/pages/course-detail/course-detail?courseId=${courseId}` })
},
onShareAppMessage() {
return { title: '画画怎么画 — 课程分类', path: '/pages/home/home' }
}
}))