JSP下拉選框,級聯選擇

前端:javascript

<%@ page contentType="text/html;charset=UTF-8" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ include file="/WEB-INF/views/include/head.jsp"%>
<title>Insert title here</title>
<script type="text/javascript">
//獲取查重內容列表
function getnext(tablename) {
    
    if(tablename!=""){
        $.ajax({
            type: "post",
            url: "${ctx}/home/findfields",
            data: {tablename:tablename,dbname:'jk'},
            success: function(data){
                //alert(data);
                debugger;
                $('#fieldSelect').empty();
                $('#fieldSelect').append('<option value="" >選擇查重字段</option>');
                $.each(data, function (i, item) {  
                    //alert(item.id + ","  + item.name);  
                    var v=item.fieldname;
                    var c=item.fieldcomment;
                    $('#fieldSelect').append('<option value="'+v+'" >'+c+'</option>');
                });
                
                
            }
         });
    }
    
}
$(function($) {
      // 你能夠在這裏繼續使用$做爲別名...
      $('#tableSelect').change(function(){ 
          var p1=$(this).children('option:selected').val();//這就是selected的值 
          getnext(p1);
         
          }) 
    });
</script>
</head>
<body>
<div class="container" style="margin:200px;">
<table class="table">
<tbody>
<tr>
<td></td>
<td>
查重項目
</td>
<td>
查重字段
</td>
<td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td>
<td>
<select name="tableSelect" id="tableSelect">
                   <option value="" >選擇查重項目</option>        
                <c:forEach var="item" items="${dataList}" >
                   <option value="${item.tablename}" >${item.tablecomment} </option>                                
                </c:forEach>
             </select>
</td>
<td>
<select name="fieldSelect" id="fieldSelect">
                   <option value="" >選擇查重字段</option>        
                <c:forEach var="item" items="${fieldList}" >
                   <option value="${item.fieldname}" >${item.fieldcomment} </option>                                
                </c:forEach>
             </select>
</td>
<td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
</tbody>
</table>

             </div>
</body>
</html>

後端:html

package com.wbh.wbhsq.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wbh.wbhsq.entity.SqData;
import com.wbh.wbhsq.service.SqdataService;



@Controller
@RequestMapping(value = "${adminPath}/home")
public class HomeController extends BaseController {
	
	@Autowired
	private SqdataService  sqdataService;
	
	@RequestMapping(value = {"view"})
	public String view(@RequestParam(required=true) String loadCode)
	{
	  
		return "wbhsq/home";
	}
	
	@RequestMapping(value="getList")
	public ModelAndView getList(ModelAndView  model) throws Exception{
	    List<SqData> list=sqdataService.findAll();
	    model.addObject("dataList", list);
		model.setViewName("wbhsq/getList");
		return model;	
	}
	
	@RequestMapping(value="setsql")
	public ModelAndView setsql(ModelAndView  model) throws Exception{
	    List<SqData> list=sqdataService.findAllTable();
	    model.addObject("dataList", list);
	    list=sqdataService.findAll();
	    model.addObject("fieldList", list);
		model.setViewName("wbhsq/setsql");
		return model;	
	}
	
	/**
	 * 獲取字段列表
	 * 2015年12月27日
	 * By hoge
	 * @param model
	 * @param rid
	 * @return
	 */
	@RequestMapping(value = {"findfields"})
	public  @ResponseBody List<SqData> findfields(Model model,String tablename) {
		Map<String,Object> filter = new HashMap<String, Object>();
		filter.put("tablename", tablename);
		List<SqData> fieldList = sqdataService.query(filter);
		return fieldList;
	}
	
}
相關文章
相關標籤/搜索