当前位置: 代码迷 >> Java Web开发 >> Jquery+json解决思路
  详细解决方案

Jquery+json解决思路

热度:5058   发布时间:2013-02-25 21:13:49.0
Jquery+json
谁能来个从后台获取list数据。然后动态生成表格。并往表格中填充数据的demo吗?要java的 谢谢
51921182@qq.com

------解决方案--------------------------------------------------------
直接贴代码行吗?
$.ajax({
type: "POST",
url: "${pageContext.request.contextPath}/AP03002.do?param=initproject",
dataType: "json",
success: function(data)
{
var ret = data.mydata;
for(var i = 0; i < ret.length; i++)
{
var id = ret[i].id;
var name = ret[i].name;
$('#xmclass').append('<option value="'+id+'">'+name+'</option>');
}
},
error: function(msg)
{
// top.tabpanel.addTab({id: "error", title: "出错页面", html:'<iframe src="${pageContext.request.contextPath}/login.do?param=error" width="100%" height="100%" frameborder="0"></iframe>'});
}

java

net.sf.json.JSONObject json = new net.sf.json.JSONObject();
JSONArray array = null;
net.sf.json.JSONObject member = null;

DaoOut out = DaoController.execute(D_projectS01Dao.class, null);
List list = out.getDataList();
if (list != null)
{
array = new JSONArray();
Iterator itr = list.iterator();
while (itr.hasNext())
{
member = new net.sf.json.JSONObject();
D_project info = (D_project)itr.next();
member.put("id", info.getProject_id()== null ? "" :info.getProject_id());
member.put("name", info.getProject_category()== null ? "" :info.getProject_category());

array.add(member);
}
}

json.put("mydata", array);
response.getWriter().write(json.toString()); 
response.getWriter().flush();
response.getWriter().close();
});
这个是一个初始化select下拉列表的,感觉和你要的差不多
------解决方案--------------------------------------------------------
HTML code
//ajax初始化上載列表,供修改使用      function initFileUpload()      {              //ajax刪除文件                                $.get("<%=request.getContextPath()%>/<%=PFMConstants.PFM_MODULE_FILE_UPLOAD_INFO%>/initUploadFile.htm",//URL                  {},//傳入的參數                  function(data, textStatus){//回調方法                  var fileLists = JSON.parse(data);                  //先移除之前的信息,再加载                  $("#tab1 tbody").find('tr:not(:first)').remove();                  var html = "";                                                                 $.each(fileLists,function(index,comm){                       html+="<tr class='table-odd-row'>"                           +"<td class='table-other-column'><a href='javascript:void(0)' onclick='return deleteFile("+fileLists[index].fileNo+");'><img src='${ctx}/images/u78.png' width='16' height='16' border='0' alt='刪除'></a></td>"                           +"<td class='table-string-column'><a href='<%=request.getContextPath()%>/<%=PFMConstants.PFM_MODULE_FILE_UPLOAD_INFO%>/downloadFile.htm?wh=real&fileName="+fileLists[index].aliasFileName+"&srcFileName="+fileLists[index].fileName+"' onclick='return checkFileExist(\""+fileLists[index].aliasFileName+"\");'>"+fileLists[index].fileName+"</a></td>"                           +"<td class='table-string-column'>"+fileLists[index].strUploadDate+"</td>"                           +"<td class='table-string-column'>"+fileLists[index].uploadUser+"</td>"                           +"<td class='table-num-column'>"+fileLists[index].fileSize+"</td>"                           +"<td class='table-string-column'>"+fileLists[index].comments+"</td>"                           +"</tr>";                                          });                                                                                                                   $("#tab1 tbody")(html);                              });              return false;      }
  相关解决方案