struts2+spring3+ibatis下类似淘宝搜索的的三级互联效果

项目框架 struts2+spring3+ibatis

用了js+json+jq去实现这个效果

 

需要jquery-min.js
<script>
function selectOnchangeb(){
var obj=document.getElementById('myselecta');  
 
var index=obj.selectedIndex; //序号,取当前选中选项的序号  
 
var val = obj.options[index].value; 
  var sel = $("#optionb"); //二级下拉框Id

var parentID=val;
    //alert(parentID);
    $.getJSON("${context}/courseTtype/index/getCourseTypeSonTypes.action?parentID="+parentID,function call(data){  
             writeHtml(data); 
        });
}  
function writeHtml(data){
    var sel = $("#optionb"); //二级下拉框Id
    sel.empty();
      var objb=document.getElementById('optionb'); 
     objb.options.length=0;
      objb.add(new    Option("","")); 
   // alert(data);
$.each(data,function(i, item){//填充到二级下拉框
//alert(item.name);
                 // $("<option value='"+item.mainid+"'>"+item.name+"</option>").appendTo(sel);    //这个jq实现不了那个填充效果不知道为什么  无奈之下只能下面的js填充法去实现
                  var obj=document.getElementById('optionb');  
       //  添加一个选项  
         obj.add(new    Option(item.name,item.mainid));  
                }); 
                selectOnchangec();
}


function selectOnchangec(){
//alert('b');
//var obj=document.getElementById('optionb');  
 
//var index=obj.selectedIndex; //序号,取当前选中选项的序号  
      var selects=document.getElementsByName('optionb');
      var val;
    if(selects&&selects[0])
    {
       val=selects[0].value;
     }
 //val = obj.options[index].value; 
  var sel = $("#optionc"); //三 级下拉框Id

var parentID=val;
   // alert(parentID);
    $.getJSON("${context}/courseTtype/index/getCourseTypeSonTypes.action?parentID="+parentID,function call(data){  
             writeHtmlb(data); 
        });
}  
function writeHtmlb(data){
    var sel = $("#optionc"); //三级下拉框Id
    sel.empty();
      var objb=document.getElementById('optionc'); 
     objb.options.length=0;
      objb.add(new    Option("","")); 
   // alert(data);
$.each(data,function(i, item){//填充到二级下拉框
//alert(item.name);
                 // $("<option value='"+item.mainid+"'>"+item.name+"</option>").appendTo(sel);
                  var obj=document.getElementById('optionc');  
 
       //  添加一个选项  
         obj.add(new    Option(item.name,item.mainid));  
                }); 
}
</script>

 


             <select οnchange="selectOnchangeb()" id="myselecta">
             <option value=""></option>
             <s:iterator id="useriterator" value="#request['CourseTypeBigTypes']" >
    <option value="<s:property value="mainid" />"><s:property value="name" /></option>        
    </s:iterator>
             </select>
             
             <select id="optionb" οnchange="selectOnchangec()"><option value=""></option></select>
             <select id="optionc" ><option value=""></option></select>

 

 

后台:

ibatis

<select id="getCourseTypeBigTypes" resultMap="courseTypeResultMap"
  parameterClass="java.lang.String">
  select * from Base_CourseType where Type_ParentID=0
 </select>
 <select id="getCourseTypeSonTypes" resultMap="courseTypeResultMap"
  parameterClass="java.lang.String">
  select * from Base_CourseType where Type_ParentID=#parentID#
 </select>

 

action:

// 高级搜索 联动下拉框 第一个下拉框数据读取
 public String AdvancedSearch() {
  List CourseTypeBigTypes = courseTypeService.getCourseTypeBigTypes();
  HttpServletRequest request = ServletActionContext.getRequest();
  request.setAttribute("CourseTypeBigTypes", CourseTypeBigTypes);
  return SUCCESS;
 }

// 二级联动下拉框数据
 public void getCourseTypeSonTypes() throws IOException {
  List CourseTypeSonTypes = courseTypeService.getCourseTypeSonTypes(this
    .getParameter("parentID"));
  List list = new ArrayList();
  list = CourseTypeSonTypes;
  List listb = new ArrayList();
  StringBuffer selectoptions = new StringBuffer();
  for (Iterator i = list.iterator(); i.hasNext();) {
   CourseType map = (CourseType) i.next();
   String mid = map.getMainid();
   String name =  map.getName();
   selectoptions.append("<option value='" + mid + "'>" + name + "</option>") ;
   listb.add(map);
  }
  
  
  JSONArray jsonArray = JSONArray.fromObject(listb);


  HttpServletResponse hsr = ServletActionContext.getResponse();
  //hsr.getWriter().write(selectoptions.toString());
  PrintWriter writer;
  try {
  writer = hsr.getWriter();
  writer.print(jsonArray);
  writer.flush();
  writer.close();

  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }


  // return SUCCESS;
 }  

 

页面要调用 onload

 

<body οnlοad="selectOnchangeb();">

 

daoimpl

:
 @Override
 public List getCourseTypeBigTypes() {
  List list = new ArrayList();
  list = this.getSqlMapClientTemplate().queryForList(
    "getCourseTypeBigTypes");
  return list;
 }

 @Override
 public List getCourseTypeSonTypes(String parentID) {
  // TODO Auto-generated method stub
  List list = new ArrayList();
  list = this.getSqlMapClientTemplate().queryForList(
    "getCourseTypeSonTypes",parentID);
  return list;
 }

 

 

这个功能花了我一天多时间

 

 

 

sql:

create table hulianType (
   MainID               varchar(36)          not null,
   Type_Name            varchar(50)          not null,
   Type_ParentID        varchar(36)          not null,
   Type_CreateDate      datetime             not null,
   Type_IsSystemDefine  int                  null,
   Type_Describe        text                 null,
   constraint PK_hulianType  primary key (MainID)
)

 

 

转载请注明作者 谢谢 如有不清楚的同学可以 QQ交流 6637152

  • 大小: 26.8 KB
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值