[英]fill Json data in to a dynamically generated table
I generate a table dynamically using jquery. 我使用jquery动态生成一个表。 this is the code for it. 这是它的代码。
$(document).ready(function(){
// to generate the table for given number of blocks
var numberOfBlocks = ${projectDetails.noOfBlocks};
var i;
var _html = '<table class="table table-bordered table-striped">';
_html += '<tr>';
_html += '<th>Blockk No</th>';
_html += '<th>Lott No</th>';
_html += '<th>Extentt</th>';
_html += '<th>Land Value</th>';
_html += '<th>On Booking Amount</th>';
_html += '</tr>';
for (i = 1; i <= parseInt(numberOfBlocks); i++) {
_html += '<tr class="tblRw" id="row'+i+'">';
_html += '<td><input type="text" class="form-control" size="10" id="blockNo'+i+'" value="'+i+'"></td>';
_html += '<td><input type="text" class="form-control" size="10" id="lotNo'+i+'" ></td>';
_html += '<td><input type="text" class="form-control" size="10" id="extent'+i+'"></td>';
_html += '<td><input type="text" class="form-control" id="land_value'+i+'"></td>';
_html += '<td><input type="text" class="form-control" size="10" id="onbookingamount'+i+'"></td>';
_html += '</tr>';
}
_html += '</table>';
document.getElementById('results').innerHTML = _html;
});
I need to populate the table rows with the data which are in a Jason
response returned from my controller class. 我需要用从控制器类返回的Jason
响应中的数据填充表行。 This is how I do it. 这就是我的方法。
public @ResponseBody JsonResponse edit_blocks(@ModelAttribute(value="editblockbean") EditBlockBean editBlockBean , BindingResult result,ModelMap model) {
JsonResponse res = new JsonResponse();
if (result.hasErrors()) {
res.setStatus("FAIL");
res.setResult(result.getAllErrors());
}else{
List<EditBlockBean> ebb = branchservice.getBlocksForEdit(editBlockBean.getTitle());
res.setStatus("SUCCESS");
res.setResult(ebb);
}
return res;
}
My Jason
response from controller class consists of a list of class objects. 我来自控制器类的Jason
响应包含一个类对象列表。 This is the class; 这是课程;
public class BlockBean {
private String blockNo;
private String lotNo;
private String extent;
private String landValue;
private String onBookingAmount;
// getters and setters
}
In Jason
response, I have a list of BlockBean
objects. 在Jason
响应中,我有一个BlockBean
对象列表。 I need to assign those object attributes such as blockNo, lotNo, extent,....
in to the columns of each row in the dynamic table generated in the jsp page. 我需要将这些对象属性(例如blockNo, lotNo, extent,....
分配给jsp页面中生成的动态表中每一行的列。
This is my ajax
call to take the jason response. 这是我的ajax
调用,以进行jason响应。
jQuery.ajax({
type : "GET",
url : "/TFProject/edit_blocks.htm",
data : "title=" + title + "&type=" +type,
success : function(response) {
if (response.status == "SUCCESS") {
// I need to know the code here.
} else {
errorInfo = "";
for (i = 0; i < response.result.length; i++) {
errorInfo += "<br>" + (i + 1) + ". "
+ response.result[i].code;
}
jQuery('#error').html(
"Please correct following errors: " + errorInfo);
jQuery('#info').hide('slow');
jQuery('#error').show('slow');
}
},
error : function(e) {
alert('Errorrrr: ' + e);
}
});
What I need to know is the code segment for populating the table rows with jason response details if the Jason
response status is success. 我需要知道的是如果Jason
响应状态为成功,则使用jason响应详细信息填充表行的代码段。 Could you please help me? 请你帮助我好吗? Thank you! 谢谢!
Your JSON response returning method should be like: 您的JSON响应返回方法应类似于:
public@ResponseBody String edit_blocks(@ModelAttribute(value = "editblockbean") EditBlockBean editBlockBean, BindingResult result, ModelMap model) {
List <EditBlockBean> editBlockBeanList = branchservice.getBlocksForEdit(editBlockBean.getTitle());
String jsonResult = "";
try {
if (editBlockBeanList != null && !editBlockBeanList.isEmpty()) {
JSONArray jsonArray = new JSONArray();
for (EditBlockBean ebb: editBlockBeanList) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("blockNo", ebb.getBlockNo());
jsonObject.put("lotNo", ebb.getLotNo());
jsonObject.put("extent", ebb.getExtent());
jsonObject.put("landValue", ebb.getLandValue());
jsonObject.put("onBookingAmount", ebb.getOnBookingAmount());
jsonArray.put(jsonObject);
}
jsonResult = jsonArray.toString();
}
} catch (Exception ex) {
jsonResult = "";
// append exception to log
}
return jsonResult;
}
And this is success part of AJAX method that will write HTML table: 这是将编写HTML表的AJAX方法的成功部分:
if (response != null && response != "") {
var EditBlockBeanArray = JSON.parse(response);
var _html = '<table class="table table-bordered table-striped">';
_html += '<tr>';
_html += '<th>Blockk No</th>';
_html += '<th>Lott No</th>';
_html += '<th>Extentt</th>';
_html += '<th>Land Value</th>';
_html += '<th>On Booking Amount</th>';
_html += '</tr>';
var i = 0;
while (i < EditBlockBeanArray.length) {
var ebbObject = EditBlockBeanArray[i];
_html += '<tr class="tblRw" id="row' + i + '">';
_html += '<td><input type="text" class="form-control" size="10" id="blockNo' + i + '" value="' + i + '">' + ebbObject.blockNo + '</td>';
_html += '<td><input type="text" class="form-control" size="10" id="lotNo' + i + '">' + ebbObject.lotNo + '</td>';
_html += '<td><input type="text" class="form-control" size="10" id="extent' + i + '">' + ebbObject.extent + '</td>';
_html += '<td><input type="text" class="form-control" id="land_value' + i + '">' + ebbObject.landValue + '</td>';
_html += '<td><input type="text" class="form-control" size="10" id="onbookingamount' + i + '">' + ebbObject.onBookingAmount + '</td>';
_html += '</tr>';
i++;
}
_html += '</table>';
document.getElementById('results').innerHTML = _html;
} else {
/* Your error code goes here */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.