微信小程序 侧栏分类三:数据从后台查询

一、关于后台

    1、总体结构分析

           1.entity:实体类

                1.商品类(cateItems)     2.小商品类(children)  

                    3.具体某一个商品类(水果:Fruits,干果:DryFruits,蔬菜:Vegetables,海鲜:seafood)

                    [注意:具体某一个商品类需要继承小商品类]

           2.servlet类

                1.cateItemsServlet

    2、具体代码

package eneity;

// 1.商品类(cateItems)
public class cateItems {

private int cate_id;                   // 品种id
private String cate_name;       // 品种名称 (水果,干果,蔬菜...)
private children children[];       // 此品种下的小商品

        

       // 自行添加 get/set    方法

       // 自行添加 有参/无参 构造方法

       .....

}

package eneity;

// 2.小商品类(children)
public class children {
private int child_id;      // 小商品的id
private String name;   // 小商品的名字,如(樱桃,芒果,葡萄)
private String image;  // 小商品对应的图片

        

       // 自行添加 get/set 方法

       // 自行添加 有参/无参 构造方法

}
package eneity;

// 3.1. 具体某个商品:水果类
public class Fruits  extends children{
public Fruits(int child_id, String name, String image) {
super(child_id, name, image);
}
}
package eneity;

// 3.2.具体某个商品:干果类
public class DryFruits extends children {
public DryFruits(int child_id, String name, String image) {
super(child_id, name, image);
}
}
package eneity;

// 3.3 具体某个商品:蔬菜类

public class Vegetables extends children {
public Vegetables(int child_id, String name, String image) {
super(child_id, name, image);
}
}
package eneity;

// 3.4 具体某个商品:海鲜类
public class seafood  extends children{
public seafood(int child_id, String name, String image) {
super(child_id, name, image);
// TODO Auto-generated constructor stub
}

}
package servlet;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import eneity.DryFruits;
import eneity.Fruits;
import eneity.Vegetables;
import eneity.cateItems;
import eneity.children;
import eneity.seafood;

public class cateItemsServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}


@SuppressWarnings("null")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

resp.setContentType("text/html;charset=utf-8");
/* 设置响应头允许ajax跨域访问 */
resp.setHeader("Access-Control-Allow-Origin", "*");
/* 星号表示所有的异域请求都可以接受, */
resp.setHeader("Access-Control-Allow-Methods", "GET,POST");

Writer out = resp.getWriter();

List<cateItems> list = new ArrayList<cateItems>();

children[] Fruits = new children[5];
Fruits[0] = new Fruits(1, "猕猴桃", "../images/mihoutao.png");
Fruits[1] = new Fruits(2, "龙眼", "../images/longyan.png");
Fruits[2] = new Fruits(3, "橘子", "../images/juzi.png");
Fruits[3] = new Fruits(4, "火龙果", "../images/huolongguo.png");
Fruits[4] = new Fruits(5, "草莓", "../images/caomei.png");

children[] DryFruits = new DryFruits[3];
DryFruits[0] = new DryFruits(1, "夏威夷果", "../images/xiaweiyi.png");
DryFruits[1] = new DryFruits(2, "开心果", "../images/kaixin.png");
DryFruits[2] = new DryFruits(3, "碧根果", "../images/bigen.png");

children[] Vegetables = new Vegetables[3];
Vegetables[0] = new Vegetables(1, "花椰菜", "../images/huaye.png");
Vegetables[1] = new Vegetables(2, "生菜", "../images/shengcai.png");
Vegetables[2] = new Vegetables(3, "番茄", "../images/fanqie.png");

seafood[] seafoods = null;


cateItems c1 = new cateItems(1, "水果", Fruits);
cateItems c2 = new cateItems(2, "干果", DryFruits);
cateItems c3 = new cateItems(3, "蔬菜", Vegetables);
cateItems c4 = new cateItems(4, "海鲜", seafoods);

list.add(c1);
list.add(c2);
list.add(c3);
list.add(c4);

JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);

out.write(JSONArray.fromObject(list).toString());
out.flush();
}
}

二、关于后

   1、wxml代码

<!--主盒子-->
< view class= "container">

<!-- 左侧栏开始 -->
< view class= 'nav_left'>
< block wx:for= "{{cateItems}}" wx:key= "id">
< view class= "nav_left_items {{curNav == item.cate_id ? 'active' : ''}}"
bindtap= "switchRightTab" data-id= "{{item.cate_id}}">
{{item.cate_name}}
</ view >
</ block >
</ view >
<!-- 左侧栏结束 -->


<!-- 右侧栏开始 -->
< view class= "nav_right">
< view wx:if= "{{curNav==curIndex}}">
< block wx:for= "{{cateItems[curIndex-1].children}}" wx:key= "id">
< view class= "nav_right_items">
< image src= "{{item.image}}"></ image >
< text >{{item.name}} </ text >
</ view >
</ block >
</ view >
</ view >
<!-- 右侧栏结束 -->


</ view >

2、js代

// pages/stock/stock_main.js
Page({

/* 页面的初始数据 */
data: {
curNav: 1,
curIndex: 1 /* 此变量用于判断该显示某个子item */
},

/* 把点击到的某一项 设为当前curNav */
switchRightTab: function (e) {
let id = e.target.dataset.id;
console.log(id);
this.setData({
curNav: id,
curIndex: id
})
},
onLoad: function (options) {
var that = this;
console.log( 'oload....');
wx.request({
url: 'http://localhost:8080/Min_Chengxu/Communications',
data: {
},
method: 'GET',
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
console.log(res.data);
that.setData({
cateItems: res.data,
})
},
fail: function (res) {
console.log( ".....fail.....");
}
})
}
})

3、wxss代

/* 1. 设置整个页面的背景颜色 */
page{
background: #f5f5f5;
/* 避免左侧Item不够时 被白色覆盖*/
}


/* 2.主盒子 */
.container {
width: 100%; /* 宽度占屏幕的100% */
height: 100%; /* 高度占屏幕的100% */
background-color: #fff; /* 背景颜色 */
}


/* 3.左盒子*/

/* 3.1. 左侧栏主盒子总体设置 */
.nav_left{
position: absolute; /* 使用绝对定位 */
top: 0px; /* 距离上边距:0px */
left: 0px; /* 距离左边距:0px */
width: 25%; /* 每个item所占的宽度 */
background: #f5f5f5; /* 主盒子设置背景色为灰色 */
text-align: center; /* 文字居中显示 */
}

/* 3.2. 左侧栏的每个item */
.nav_left .nav_left_items{
height: 40px; /* 每个item高40px*/
padding: 6px 0; /* 上内边距和下内边距是 6px[增加高度] 右内边距和左内边距是 0px*/
border-bottom: 1px solid #dedede; /* 设置下边线 */
font-size: 14px; /* 设置文字大小:14px */
}

/* 3.3. 左侧栏list的item被选中时*/
.nav_left .nav_left_items.active{
background: #fff; /* 背景色变成白色 */
color: #3385ff; /* 字体编程蓝色 */
border-left: 3px solid #3385ff; /* 设置边框的宽度以及颜色 */
}


/* 4.右盒子 */

/* 4.1. 右侧栏主盒子总体设置 */
.nav_right{
position: absolute; /* 使用绝对定位 */
top: 0; /* 距离上边距:0px */
left: 80px; /* 距离左边距:80px */
width: 75%; /* 右侧主盒子所占宽度 */
height: 600px; /* 右侧主盒子所占高度 */
padding: 10px; /* 所有 4 个内边距都是 10px*/
box-sizing: border-box; /* 为元素指定的任何内边距和边框都将在已设定的宽度和高度内进行绘制*/
background: #fff; /* 右侧主盒子背景颜色 */
}

/* 4.2. 右侧栏中的每个item */
.nav_right .nav_right_items{
float: left; /* 浮动向左 */
width: 33.33%; /* 每个item设置宽度是33.33% */
height: 120px; /* 每个item设置高度是120px */
text-align: center; /* 设置图片下方的提示文字居中显示 */
}

/* 4.3. 右侧栏中的每个item的图样式设置 */
.nav_right .nav_right_items image{
width: 60px; /* 给图片设置宽度 */
height: 60px; /* 给图片设置高度 */
margin-top: 15px; /* 图片距离上边距15px */
border-radius: 40%; /* 给图片添加圆角边框 */
}


  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 微信小程序中, 侧边栏和商品栏联动的代码可能长这样: ``` // 在侧边栏组件中 // 1. 声明一个自定义事件, 名称为 selectCategory // 2. 在点击侧边栏每一项时, 触发自定义事件, 并将当前点击的分类的id作为参数传递 onCategoryClick: function (e) { let id = e.currentTarget.dataset.id this.setData({ activeCategoryId: id }) this.triggerEvent('selectCategory', { id: id }, {}) } // 在商品列表组件中 // 1. 监听侧边栏的 selectCategory 自定义事件 // 2. 在事件处理函数中, 获取到当前选中的分类id, 然后请求对应的商品数据并更新商品列表 pageLifetimes: { show: function () { this.getGoodsList() } }, methods: { getGoodsList: function () { let that = this WxRequest.get({ url: '/goods/list', data: { categoryId: that.data.categoryId } }).then(res => { if (res.code === 0) { that.setData({ goodsList: res.data.goodsList }) } }) }, onSelectCategory: function (e) { let id = e.detail.id this.setData({ categoryId: id }) this.getGoodsList() } } ``` 希望这能帮到你! ### 回答2: 微信小程序的侧边栏和商品栏联动的代码可以通过监听组件的点击事件来实现。以下是一个简单的示例: 1. 在json文件中设置面结构,包括侧边栏和商品栏的组件,例如: ```json { "usingComponents": { "sidebar": "/components/sidebar", "product-list": "/components/product-list" } } ``` 2. 在wxml文件中编写面布局,包括侧边栏和商品栏的位置及样式。 ```html <sidebar bind:selectItem="onSidebarItemClick"></sidebar> <product-list products="{{products}}"></product-list> ``` 3. 在js文件中编写逻辑代码,包括监听侧边栏点击事件和更新商品栏数据。 ```javascript Page({ data: { products: [] }, onLoad: function() { // 从服务器获取商品数据,并更新到this.data.products中 }, onSidebarItemClick: function(event) { const selectedCategory = event.detail.category; // 获取点击的侧边栏分类 // 根据分类筛选商品数据,并更新到this.data.products中 this.setData({ products: filteredProducts }); } }) ``` 4. 在侧边栏组件的js文件中,编写点击事件的触发和传递函数。 ```javascript Component({ methods: { onItemClick: function(event) { const category = event.currentTarget.dataset.category; // 获取点击的分类 this.triggerEvent('selectItem', { category: category }); } } }) ``` 这样,当侧边栏中的某个分类被点击时,就会触发selectItem事件,在面的onSidebarItemClick方法中处理该事件,根据选择的分类筛选商品数据并更新到商品栏中。 ### 回答3: 微信小程序侧边栏商品栏联动代码的实现主要是通过使用wxml、wxss和js文件,并结合微信小程序提供的API来实现的。 首先,需要在wxml文件中创建一个侧边栏和商品栏的布局。可以使用<view>标签来创建侧边栏,并使用<scroll-view>标签来创建商品栏。可以给<scroll-view>标签添加一个id属性,方便在js文件中获取该元素。然后,分别为侧边栏和商品栏中的每个商品项添加一个点击事件,用来触发联动效果。 接下来,在js文件中,可以通过选择器获取侧边栏和商品栏的元素。可以使用wx.createSelectorQuery()方法来获取指定元素的信息。获取到元素后,可以使用setData()方法将数据传递给wxml文件,从而更新显示的内容。 在点击侧边栏的商品项时,可以触发相应的点击事件。点击事件中,可以根据点击的商品项的索引,来滚动到对应商品栏中的商品项位置。可以使用wx.pageScrollTo()方法来实现滚动,传入滚动到的位置的偏移量即可。 此外,还可以在点击侧边栏的商品项时,将点击的商品项的索引传递给商品栏,用来控制商品栏中对应项的样式变化。可以使用wx:if或wx:show来控制相应商品项的显示与隐藏,并给对应的商品项添加不同的样式。 通过以上的代码实现,可以实现微信小程序侧边栏和商品栏的联动效果。用户在侧边栏选择不同的商品项时,商品栏会相应地滚动到对应的位置,并且高亮显示当前选中的商品项。这样可以提升用户体验,使得用户能够快速地浏览和选择商品。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值