实现从数据库中动态获取对应的list集合,并在easyui的combobox中显示出来。
实现的效果如下:
1、数据库的表设计如图所示
2、数据库中填写相关的数据,如图所示。如图所示【法律法规】是所属栏目,因此他的字段parentid是0。【中国公民出国】、【内地居民往来港澳】是属于法律法规的类别。因此他们的字段parentid是对应1,【法律法规】的字段categoryid是1.
3、相关的配置:已经在前面的博客中写了
这里就不多写。只把关键代码贴出来。
4、对应的action代码
package crj.portal.web.management.action;
import java.io.ioexception;
import java.util.list;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import net.sf.json.jsonarray;
import net.sf.json.jsonobject;
import net.sf.json.jsonconfig;
import org.apache.log4j.logger;
import org.apache.struts2.servletactioncontext;
import org.hibernate.criteria;
import org.hibernate.criterion.order;
import com.sun.tools.javac.util.log;
import crj.portal.web.management.model.cpersontypetbl;
import crj.portal.web.management.service.categoryservice;
import crj.portal.web.management.service.itemservice;
import crj.portal.web.management.service.userservice;
public class itemmanageaction {
logger log=logger.getlogger(this.getclass());
private string page;
private string rows;
private string lanmuid;
private itemservice itemservice;// 依赖注入
//下拉框--查询栏目
public string categorytbl() throws exception{
list list=itemservice.querylanmu();
this.tojsonarray(list);
return null;
}
//根据栏目的id 查询拥有的类别
public string leibie() throws exception{
list list=itemservice.queryleibie(lanmuid);
this.tojsonarray(list);
return null;
}
public string tojsonarray(list list) throws ioexception{
httpservletresponse response = servletactioncontext.getresponse();
httpservletrequest request = servletactioncontext.getrequest();
jsonarray json = jsonarray.fromobject(list);
log.info("json格式:" +json.tostring());
response.setcharacterencoding("utf-8");// 指定为utf-8
response.getwriter().write(json.tostring());// 转化为json格式
return null;
}
public string save() throws exception {
return this.alllist();
}
public itemservice getitemservice() {
return itemservice;
}
public void setitemservice(itemservice itemservice) {
this.itemservice = itemservice;
}
public string getpage() {
return page;
}
public void setpage(string page) {
this.page = page;
}
public string getrows() {
return rows;
}
public void setrows(string rows) {
this.rows = rows;
}
public userservice getuserservice() {
return userservice;
}
public void setuserservice(userservice userservice) {
this.userservice = userservice;
}
public categoryservice getcategoryservice() {
return categoryservice;
}
public void setcategoryservice(categoryservice categoryservice) {
this.categoryservice = categoryservice;
}
public string getlanmuid() {
return lanmuid;
}
public void setlanmuid(string lanmuid) {
this.lanmuid = lanmuid;
}
}
5、对应的接口代码
public interface itemservice {
//下拉框--查询栏目
public list querylanmu() throws exception;
//下拉框--查询类别
public list queryleibie(string ids) throws exception;
}
6、对应的接口实现类代码
public class itemserviceimpl implements itemservice {
logger log = logger.getlogger(this.getclass());
private sessionfactory sessionfactory;
//下拉框--查询栏目
public list querylanmu() throws exception {
criteria criteria=this.sessionfactory.getcurrentsession().createcriteria(categorytbl.class);
criteria.add(restrictions.eq("parentid", 0));
criteria.addorder(order.asc("categoryid"));
return criteria.list();
}
//下拉框--查询类别
public list queryleibie(string ids) throws exception {
int i=integer.parseint(ids);
criteria criteria=this.sessionfactory.getcurrentsession().createcriteria(categorytbl.class);
criteria.add(restrictions.eq("parentid", i));
criteria.addorder(order.asc("categoryid"));
return criteria.list();
}
public sessionfactory getsessionfactory() {
return sessionfactory;
}
public void setsessionfactory(sessionfactory sessionfactory) {
this.sessionfactory = sessionfactory;
}
}
7、对应的jsp代码
string path = request.getcontextpath();
%>
信息管理/* 初始化下载表格信息 */
$(function() {
// 下拉框选择控件,下拉框的内容是动态查询数据库信息
$('#lanmu').combobox({
url:'itemmanage!categorytbl',
editable:false, //不可编辑状态
cache: false,
panelheight: 'auto',//自动高度适合
valuefield:'categoryid',
textfield:'categoryname',
onhidepanel: function(){
$("#leibie").combobox("setvalue",'');
var lanmuid = $('#lanmu').combobox('getvalue');
$.ajax({
type: "post",
url: "itemmanage!leibie?lanmuid="+lanmuid,
cache: false,
datatype : "json",
success: function(data){
$("#leibie").combobox("loaddata",data);
}
});
}
});
$('#leibie').combobox({
//url:'itemmanage!categorytbl',
editable:false, //不可编辑状态
cache: false,
panelheight: 'auto',//自动高度适合
valuefield:'categoryid',
textfield:'categoryname'
});
});
所属栏目:
类别:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
希望与广大网友互动??
点此进行留言吧!