<script type="text/javascript" src="../commons/js/jquery.js"></script>javascript
jsjava
//獲取呈報單位信息
function showDept(showId){
var deptText="";
if(showId.length>0){
deptText=document.getElementById(showId).value; //獲取輸入信息
}
var showTab=""; //顯示縮進
$("#showDeptDiv").empty();//清空idv
//異步
$.ajax({
cache:false,
type:'post',
async:false,
height:350,
url:'<%=path%>/work/workVlidateServlet',
dataType: 'json',
timeout: 1000,
data:{"deptText":deptText},
success: function(data){
//alert(data.length);
if(data.length>0){
$.each(data, function(i, item) {
if(item.parent=="false")
$("#showDeptDiv").append(" <input type='checkbox' id='"+item.id+"' name='checkDeptName' value='"+item.groupcode+"' aa='"+item.pid+"' > "+item.name+" </input> <br/>");
else
$("#showDeptDiv").append("<input type='checkbox' id='"+item.id+"' name='checkDeptName' value='"+item.groupcode+"' bb='"+item.pid+"' onclick=chekVlidate('"+item.pid+"') > "+item.name+" </input><br/>");
});
//$("#showDeptDiv").append("<input type='button' name='selBtn' id='selBtn' value='肯定' class='btn-search' onclick='selBtn();' align='right' >")
document.getElementById('showDeptDiv').style.display="";
}else{
document.getElementById('showDeptDiv').style.display="none";
}
},
error: function(XMLHttpRequest, textStatus, errorThrown){
//alert("提醒失敗,請聯繫管理員");
return false;
}
});
}node
<form>jquery
查詢單位:ajax
<span >json
<inputapp
class="inbox"異步
id="deptID"async
type="text"ide
style="width:150px"
onkeyup="showDept(this.id) ;"
onkeypress="showDept(this.id) ;"
onkeydown="showDept(this.id) ;"
/>
</span>
</form>
java
/** * 驗證查詢部門信息 * * @author wangsl * */public class WorkValidateDeptServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/json;charset=utf-8"); response.setCharacterEncoding("utf-8"); String deptText = request.getParameter("deptText").trim(); // 獲取傳入部門 StringBuffer queryString = new StringBuffer( "from KoofOrganizeDept where 1=1"); List<KoofOrganizeDept> results = new ArrayList<KoofOrganizeDept>(); if (!deptText.isEmpty() && !"".equals(deptText)) { queryString.append(" and depname like '%" + deptText + "%'"); results = this.getDao().findByHql(queryString.toString()); } else { queryString.append(" and upperdepid is null"); List<KoofOrganizeDept> list = this.getDao().findByHql( queryString.toString()); // results = this.getDao().findByHql(queryString.toString()); try { results = this.listDept(list.get(0).getDepid(), list); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } PrintWriter out = response.getWriter(); String result = null; try { result = this.formatJson(results); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //System.out.println("result===" + result); //System.out.println("results===" + results.size()); out.flush(); out.write(result); out.close(); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } public IDao getDao() { return (IDao) InitializatedManager.getInstance() .getApplicationContext().getBean("BaseDao"); } public List<KoofOrganizeDept> listDept(Integer id, List<KoofOrganizeDept> deptList) throws Exception { KoofOrganizeDept dept = new KoofOrganizeDept(); dept.setUpperdepid(id); StringBuffer queryString = new StringBuffer( "from KoofOrganizeDept where 1=1"); if (id != null) { queryString.append(" and upperdepid=" + id); } queryString.append(" order by depserialid"); List<KoofOrganizeDept> oaDeptList = this.getDao().findByHql( queryString.toString()); for (KoofOrganizeDept result : oaDeptList) { //System.out.println(result.getDepname()); deptList.add(result); listDept(result.getDepid(), deptList); } return deptList; } /** * 轉換爲json對象 * * @param deptList * @return * @throws Exception */ private String formatJson(List<KoofOrganizeDept> deptList) throws Exception { JSONArray jsonAarry = new JSONArray(); for (KoofOrganizeDept dept : deptList) { JSONObject node = new JSONObject(); if (dept.getUpperdepid() == null || dept.getUpperdepid() == 100000) { continue; } if (dept.getUpperdepid() == 100001 || dept.getUpperdepid() == 100002 || dept.getUpperdepid() == 100003 || dept.getUpperdepid() == 100004 || dept.getUpperdepid() == 100005 || dept.getUpperdepid() == 100006) { node.put("parent", "true"); } else { node.put("parent", "false"); } node.put("id", dept.getDepid()); node.put("pid", dept.getUpperdepid()); node.put("name", dept.getDepname()); node.put("groupcode", dept.getDepsigncode()); jsonAarry.put(node); // map.put("", value) } return jsonAarry.toString(); }}