jquery的ajax 處理後臺傳來json格式的二維數組

@SuppressWarnings("unchecked")
@Path("getUsers.html")
@POST
@Produces({ MediaType.APPLICATION_JSON })
public Map getUserByName(MultivaluedMap<String, String> formParams) throws Exception {
String name=formParams.getFirst("name");
String project_id=formParams.getFirst("part_id");
List Users=null;
List<Part> parts=null;
users= jpaTemplate.find("select usp.user,usp.part.id as bid from USP usp where usp.user.name like '"+"%"+name+"%'");
parts= jpaTemplate.find("select usp.part from USP usp where usp.part.id='"+part_id+"'");
Map map=new HashMap();
map.put("users", users);
map.put("parts", parts);
return map;
}



<script type="text/javascript">
function cleanVal(){
document.getElementById('name').value="";
}
function setDefault(){
var value=document.getElementById('name').value;
if(value=="" || value==null){
document.getElementById('name').value="請輸入姓名";
}
}

$(document).ready(function(){
$("#search").click(function(){
$.ajax({
url:"../../app/userManage/getUsers.html",
type:"POST",
data:$("#searchCondition").serialize(),
dataType:"json",
success:function(data){
try{
var html=' ';
$.each(data.users,function(i,item){
html += '<tr><td>'+ item[0].name +'</td><td>'+ item[0].sex+'</td><td>'+ item[0].addr +'</td>';
if(item[1]==" " || item[1] == null){

html += '<td>'+"未分配"+'</td></tr>';
}else{
$.each(data.parts,function(entryIndex,entry){
if(item[1]==entry.id){
html += '<td><a href="../userManage/to-user-detail.html?part_id='+entry.id+'&user_id=${user.id}" rel="external">'+entry.name+'</a></td></tr>';
return false;
}
});

}

return true;

});
$("#puser").html(html);
}catch(e){
alert("查詢員工出現異常");
return;
}
},
error:function(result,status){
if(status == 'error'){
alert(status);
}
}
});
});
});
</script>


<form id="searchCondition" rel="external" data-ajax="false" enctype1="multipart/form-data">
<input name="part_id" type="hidden" value="${part.id}" />
<table width="90%" border="0" cellpadding="0" cellspacing="0"
style="margin: 4px">
<tr style="border: 0">
<td style="border: 0">
<label>搜索員工屬於哪個部門:</label>
</td>
<td style="border: 0">
<input id="name" name="name" type="search" οnfοcus="cleanVal()" οnblur="setDefault()" value="請輸入姓名" style="width: 100%" />
</td>
<td style="width: 1px; border: 0">
<input id="search" type="button" value="搜索" />
</td>
</tr>
</table>
</form>

<h3>員工屬於哪個部門</h3>
<table id="title" >
<thead class="ui-bar-b">
<tr>
<th>姓名</th>
<th>性別</th>
<th>地址</th>
<th>所在部門</th>
</tr>
</thead>
<tbody id="puser">
</tbody>
</table>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章