123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- const getHeight = require("../../utils/GetRheRemainingHeight");
- Component({
- options: {
- addGlobalClass: true
- },
- properties: {
- styleType: {
- type: String,
- value: "default", //默认样式
- },
- sort: {
- type: Array
- }, //排序规则列表
- search: {
- type: Boolean
- }, //是否开启搜索
- list: { //功能列表
- type: Array,
- value: [{
- label: "全部",
- icon: "icon-webxialaxuanxiangjiantou",
- color: "",
- width: "",
- id: "1"
- }, {
- label: "排序",
- icon: "icon-shengxu",
- color: "",
- width: "",
- id: "sort"
- }, {
- label: "筛选",
- icon: "icon-shaixuan",
- color: "",
- width: "",
- id: "filtrate"
- }]
- },
- condition: {
- type: String
- }, //搜索内容
- onClick: {
- type: Function
- },
- startUsing: {
- type: Boolean
- }, //启用搜索
- onSearch: {
- type: Function
- }, //搜索回调
- record: { //记录历史
- type: Boolean,
- value: true
- },
- placeholder: {
- type: String,
- value: "搜索商品"
- },
- isFilter: {
- type: Boolean,
- value: false
- }
- },
- data: {
- sortShow: false,
- reversed: 0, //是否翻转
- condition: "",
- history: [], //搜索历史
- showHistory: false,
- listHeight: null,
- },
- lifetimes: {
- attached() {
- if (wx.getStorageSync('SearchHistory')) this.setData({
- history: wx.getStorageSync('SearchHistory')
- })
- },
- ready() {
- getHeight.getHeight('.Yl_head', this).then(res => this.setData({
- listHeight: res - 80
- }));
- }
- },
- methods: {
- /* 排序 */
- sortClick(e) {
- const {
- name
- } = e.target.dataset;
- if (name == 'confirm') {
- let sort = this.data.sort.find(v => v.sorted == 1),
- i = this.data.list.findIndex(v => v.id == 'sort');
- this.setData({
- [`list[${i}].label`]: sort.sortname,
- [`list[${i}].icon`]: sort.reversed == 1 ? 'icon-jiangxu1' : 'icon-shengxu',
- })
- let pages = getCurrentPages(),
- page = pages[pages.length - 1];
- page.setData({
- "content.sort": this.data.sort
- });
- page.getList(true);
- };
- this.setData({
- sortShow: false
- })
- },
- sortClose() {
- this.setData({
- sortShow: false
- })
- },
- /* 设置排序规则 */
- handleSort(e) {
- const {
- id
- } = e.currentTarget.dataset;
- this.setData({
- sort: this.data.sort.map(v => {
- v.sorted = v.sortid == id ? 1 : 0
- return v;
- })
- });
- },
- /* 设置升序和倒叙 */
- handleReversed(e) {
- let {
- id
- } = e.currentTarget.dataset;
- if (this.data.reversed == id) return;
- this.setData({
- reversed: id,
- sort: this.data.sort.map(v => {
- v.reversed = id;
- return v;
- })
- });
- },
- onClick(e) {
- const {
- item
- } = e.currentTarget.dataset;
- if (item.id == "sort") {
- //排序
- this.setData({
- sortShow: true
- })
- } else if (item.id == "filtrate") {
- let page = getCurrentPages()[getCurrentPages().length - 1].selectComponent("#Filtrate");
- if (page) {
- page.setData({
- show: true
- })
- } else {
- this.triggerEvent("onClick", item)
- }
- } else {
- this.triggerEvent("onClick", item)
- }
- },
- onChick1(e) {
- const {
- id
- } = e.currentTarget.dataset;
- if (id == "sort") {
- //排序
- this.setData({
- sortShow: true
- })
- } else if (id == "filtrate") {
- let page = getCurrentPages()[getCurrentPages().length - 1].selectComponent("#Filtrate");
- if (page) {
- page.setData({
- show: true,
- })
- } else {
- getCurrentPages()[getCurrentPages().length - 1].setData({
- modalName: 'viewModal'
- })
- }
- }
- },
- /* 开启关闭搜索 */
- clickSearch() {
- this.setData({
- startUsing: !this.data.startUsing
- });
- setTimeout(this.setListHeight, 400)
- },
- /* 搜索 */
- confirmSearch(e) {
- if (this.data.condition == e.detail.value) return;
- this.setData({
- condition: e.detail.value
- })
- this.triggerEvent("onSearch", e.detail.value)
- },
- /* 开始搜索 */
- startSearch({
- detail
- }) {
- if (this.data.condition == detail) return;
- this.setData({
- condition: detail
- })
- this.triggerEvent("onSearch", detail)
- if (this.data.record || detail == '') {
- let list = this.data.history;
- if (list.findIndex(v => v == detail) == -1) {
- list.push(detail)
- this.setData({
- history: list
- });
- wx.setStorageSync("SearchHistory", list)
- }
- }
- },
- /* 取消搜索 */
- endSearch() {
- this.setData({
- condition: ""
- })
- this.triggerEvent("onSearch", "")
- },
- /* 删除搜索历史 */
- deleteHistory(e) {
- let that = this;
- wx.showModal({
- title: '提示',
- content: '是否删除所有搜索历史',
- complete: ({
- confirm
- }) => {
- if (confirm) {
- wx.setStorageSync("SearchHistory", [])
- that.setData({
- history: []
- });
- this.setListHeight();
- }
- }
- })
- },
- /* 快速搜索 */
- clickTag(e) {
- const {
- name
- } = e.currentTarget.dataset;
- this.setData({
- condition: name
- });
- this.triggerEvent("onSearch", name)
- },
- /* 单独删除 */
- delteTag(e) {
- const {
- name
- } = e.currentTarget.dataset;
- this.setData({
- history: this.data.history.filter(v => v != name)
- });
- wx.setStorageSync('SearchHistory', this.data.history);
- this.setListHeight();
- },
- /* 设置列表高度 */
- setListHeight() {
- let pages = getCurrentPages();
- if (pages[pages.length - 1].setListHeight) pages[pages.length - 1].setListHeight();
- },
- /* 搜索框焦点 */
- onFocus() {
- this.setData({
- showHistory: true
- });
- setTimeout(this.setListHeight, 50);
- },
- /* 搜索框失焦 */
- onBlur() {
- this.setData({
- showHistory: false
- })
- setTimeout(this.setListHeight, 50);
- }
- }
- })
|