我使用ajax通过控制器从模型中获取数据,我认为每件事都运行得很好,当我使用console.log(响应)进行检查时,我得到了
Object {buyTemps: Object}
在物体的侧面,我找到了阵列中的所有数据,到目前为止,所有东西都是好的
ajax
$(".buy-tr").click(function(e){
var
data = {},
$row = $(this).closest("tr"), // Finds the closest row <tr>
$tds = $row.find("td:nth-child(1)"); // Finds the 2nd <td> element
$.each($tds, function() { // Visits every single <td> element
data={buyId:$(this).text()};
});
// $(this).find('input:radio').prop('checked', true);
$(".buy-tr").click(function(e){
var
data = {},
$row = $(this).closest("tr"), // Finds the closest row <tr>
$tds = $row.find("td:nth-child(1)"); // Finds the 2nd <td> element
$.each($tds, function() { // Visits every single <td> element
data={buyId:$(this).text()};
});
$.ajax({
url : "/buy/selectTable",
type : 'GET',
dataType : 'json',
data : data,
success : function(response) {
console.log(response);
$('#buyItem-table tbody').empty();
$.each(response,function(index, v){
$('#buyItem-table tbody').append(
"<tr><td>" + v.buyItemTempId
+ "</td><td>" + v.itemName
+ "</td><td>" + v.itemExpire
+ "</td><td>" + v.buyPrice
+ "</td><td>" + v.buyBox
+ "</td><td>" + v.itemPacking
+ "</td><td>" + v.buyQty
+ "</td></tr>" );
});
},
error : function(response) {
swal("error");
}
在我的控制器
public function selectItemTable()
{
$buyId = Input::get('buyId');
$buyTemps = DB::table('vbuytemp')->where('buyId',$buyId)->paginate(10);
return Response::json(compact('buyTemps'));
}
控制台日志(v)
Object {total: 1, per_page: 10, current_page: 1, last_page: 1, next_page_url: null…}current_page: 1data: Array[1]0: Objectbarcode: "08815408"buyBox: 30buyId: 2buyItemTempId: 2buyPrice: "2.500"buyQty: 90itemExpire: "2018-01-04"itemId: 2itemName: "Panadol Extra tab"itemPacking: 2minQty: 1roofId: 1sellingForm: 1__proto__: Objectlength: 1__proto__: Array[0]from: 1last_page: 1next_page_url: nullper_page: 10prev_page_url: nullto: 1total: 1__proto__: Object__defineGetter__: __defineGetter__()__defineSetter__: __defineSetter__()__lookupGetter__: __lookupGetter__()__lookupSetter__: __lookupSetter__()constructor: Object()hasOwnProperty: hasOwnProperty()isPrototypeOf: isPrototypeOf()propertyIsEnumerable: propertyIsEnumerable()toLocaleString: toLocaleString()toString: toString()valueOf: valueOf()get __proto__: get __proto__()set __proto__: set __proto__()
结果所有表格单元格都填充了未定义的
您的问题是在错误的点上循环处理响应数据。当你调用$.each
时,你实际上是在循环所有的分页元数据(total、per_page等)。你想要的实际数据包含在数据数组中。因此,只需循环遍历该数组,它就可以工作了。
$.each(response.data,function(index, v){
//now v.buyItemTempId and other properties exist
}