一、下拉框
1.将下拉框需要显示的值写入实体类
package com.lj.entity;
public class Category {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
public Category(long id, String name) {
super();
this.id = id;
this.name = name;
}
public Category() {
super();
// TODO Auto-generated constructor stub
}
}
2、查询所有类型的方法(CategoryDao)
package com.lj.dao;
import java.util.List;
import com.zking.util.BaseDao;
import com.zking.util.PageBean;
import com.lj.entity.Category;
public class CategoryDao extends BaseDao<Category> {
public List<Category> list(Category category, PageBean pageBean) throws Exception {
String sql="select * from t_easyui_category where 1=1 ";
long id = category.getId();
if(id !=0) {
sql += " and id ="+id;
}
return super.executeQuery(sql, Category.class, pageBean);
}
}
3、子控制器(CategoryAction)
package com.lj.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.ResponseUtil;
import com.lj.dao.CategoryDao;
import com.lj.entity.Category;
public class CategoryAction extends ActionSupport implements ModelDriver<Category>{
private Category category=new Category();
private CategoryDao categoryDao =new CategoryDao();
@Override
public Category getModel() {
// TODO Auto-generated method stub
return category;
}
/**
* 加载书籍类别下拉框
* @param req
* @param resp
* @return
*/
public String combobox(HttpServletRequest req, HttpServletResponse resp) {
try {
List<Category> list = categoryDao.list(category,null);
ResponseUtil.writeJson(resp, list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String load (HttpServletRequest req, HttpServletResponse resp) {
try {
//传递id到后台 只会查出一个类别
Category c = categoryDao.list(category,null).get(0);
ResponseUtil.writeJson(resp, c);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
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:'<iframe width="100%" height="100%" src="'+$("#ctx").val()+node.attributes.self.url+'" />',
closable:true
});
}
}
});
})
5、通过数据库内的类型传到增加窗口的下拉框,使其灵活性
借助API中的ComboBox(下拉列表框)
<input id="cid" name="cid" value="" label="类别" >
js文件:
$(function () {
$('#cid').combobox({
url:'${pageContext.request.contextPath}/category.action?methodName=combobox', valueField:'id',
textField:'text'
});
});
二、书籍上架下架新增功能
1、完成新增功能
①、新增页面所需的属性实体类
private long id;
private String name;
private String pinyin;
private long cid;
private String author;
private float price;
private String image;
private String publishing;
private String description;
private int state;
// 查询时间的时候用这个格式
@JsonFormat(pattern="yyyy-mm-dd HH:mm:ss",timezone="GMT+8")
private Date deployTime;
private int sales;
②、写方法BookDao
public void add( Book t) throws Exception {
//转化拼音
t.setPinyin(PinYinUtil.getAllPingYin(t.getName()));
t.setDeployTime(new Date());
super.executeUpdate("insert into t_easyui_book(name,pinyin,cid,author,price,image,publishing,description,state,deployTime,sales) values(?,?,?,?,?,?,?,?,?,?,?)", t, new String[] {"name","pinyin","cid","author","price","image","publishing","description","state","deployTime","sales"});
}
③、写子控制器(BookAction)
public String add(HttpServletRequest req, HttpServletResponse resp) {
try {
bookDao.add(book);
ResponseUtil.writeJson(resp, 1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
ResponseUtil.writeJson(resp, 0);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return null;
}
④、获取数据,提交表单
/* 通过form控件提交 */
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');
}
到这里新增就完成了
2、上架与下架
①、上架方法,也就是修改
// 查询
public List<Book> list(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"});
}
②、子控制器
public void list(HttpServletRequest req, HttpServletResponse resp) {
PageBean pageBean=new PageBean();
pageBean.setRequest(req);
try {
List<Book> 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();
}
}
调用方法,写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'); // 重新载入当前页面数据
}
})
}
}
});
}
最后结果就是