1.1:根据下拉框类型写实体类:
1.2 查询所有类型的方法
public ListlistType(Category category,PageBean pageBean) throws Exception{ String sql="select * from t_easyui_category where 1=1"; return executeQuery(sql, Category.class, pageBean); }
1.3 子控制器(CategoryAction)
package com.xyy.web; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.xyy.dao.CategoryDao; import com.xyy.entity.Category; import com.zking.framework.ActionSupport; import com.zking.framework.ModelDriver; import com.zking.util.ResponseUtil; public class CategoryAction extends ActionSupport implements ModelDriver{ public Category category=new Category(); public CategoryDao categoryDao=new CategoryDao(); @Override public Category getModel() { return category; } public String listType(HttpServletRequest req, HttpServletResponse resp) throws Exception { List listType = categoryDao.listType(category, null); ResponseUtil.writeJson(resp, listType); return null; } }
1.4在点击菜单栏需弹出一个增加的窗口
$(function(){
$("#bookMenus").tree({
url:$("#ctx").val()+"/permission.action?methodName=tree",
// 给菜单栏一个点击
onClick: function(node){
// 判断面板是否存在
var exists=$("#bookTabs").tabs('exists',node.text);
if(exists){
$("#bookTabs").tabs('select',node.text);
}else{
$('#bookTabs').tabs('add',{
title:node.text,
content:'',
closable:true
});
}
}
});
})
1.5 通过数据库内的类型传到增加窗口的下拉框,使其灵活性
借助API中的ComboBox(下拉列表框)
js文件:
$(function () {
$('#cid').combobox({
url:'${pageContext.request.contextPath}/category.action?methodName=combobox', valueField:'id',
textField:'text'
});
});
效果展示:
二,书籍新增
2.1书籍实体类:
// 查询时间的时候用这个格式
@JsonFormat(pattern="yyyy-mm-dd HH:mm:ss",timezone="GMT+8")
private Date deployTime;
2.2bookDao
public void add( Book t) throws Exception {
// 转化拼音
t.setPinyin(PinYinUtil.getAllPingYin(t.getName()));
t.setDeployTime(new Date());
String sql="insert into t_easyui_book(name,pinyin,cid,author,price,image,publishing,description,state,deployTime,sales) values(?,?,?,?,?,?,?,?,?,?,?)";
super.executeUpdate(sql,
t, new String[] {"name","pinyin","cid","author","price","image","publishing","description","state","deployTime","sales"});
}
2.3 bookAction
public void add(HttpServletRequest req, HttpServletResponse resp) {
try {
bd.add(book);
ResponseUtil.writeJson(resp, 1);
} catch (Exception e) {
e.printStackTrace();
try {
ResponseUtil.writeJson(resp, 0);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
2.4 获取数据,提交表单
function submitForm() {
$('#ff').form('submit', {
url:'${pageContext.request.contextPath}/book.action?methodName=add',
success:function(data){
if(data==1){
$('#ff').form('clear');
}
}
});
}
function clearForm() {
$('#ff').form('clear');
}
效果展示:
新增的书籍在未上架中。
三,上架&下架3.1 上架其实就是修改书籍属性
public Listlist(Book book, PageBean pageBean) throws Exception { String sql="select * from t_easyui_book where 1=1"; String name=book.getName(); int state = book.getState(); if(StringUtils.isNotBlank(name)) { sql+=" and name like '%"+name+"%'"; } if(state!=0) { sql+=" and state ="+state; } return super.executeQuery(sql, Book.class, pageBean); } // 上下架 public void editStatus(Book book) throws Exception { super.executeUpdate("update t_easyui_book set state=? where id=?", book,new String[] {"state","id"}); }
3.2 子控制器
public void list(HttpServletRequest req, HttpServletResponse resp) {
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
try {
List list = bd.list(book, pageBean);
ResponseUtil.writeJson(resp, new R().data("total", pageBean.getTotal())
.data("rows", list));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 如果上架,书籍的状态改为2
// 如果下架,书籍的状态改为3
public void editStatus(HttpServletRequest req, HttpServletResponse resp) {
try {
bd.editStatus(book);
ResponseUtil.writeJson(resp, 1);
} catch (Exception e) {
e.printStackTrace();
try {
ResponseUtil.writeJson(resp, 0);
} catch (Exception e1) {
e1.printStackTrace();
}
}
3.3 JS文件
function shangjia() {
$.messager.confirm('确认','您确认想要上架此书籍吗?',function(r){
if (r){
var row = $('#dg').datagrid('getSelected');
if (row){
$.ajax({
url:'${pageContext.request.contextPath}/book.action?methodName=editStatus&state=2&id=' + row.id,
success:function (data) {
}
})
}
}
});
}
根据状态的不同,改变上下架
function xiajia() {
$.messager.confirm('确认','您确认想要下架此书籍吗?',function(r){
if (r){
var row = $('#dg').datagrid('getSelected');
if (row){
$.ajax({
url:'${pageContext.request.contextPath}/book.action?methodName=editStatus&state=3&id=' + row.id,
success:function (data) {
$('#dg').datagrid('reload'); // 重新载入当前页面数据
}
})
}
}
});
}
效果展示:已上架&已下架



