我已经坚持了一天了,我快疯了。可能真的很简单,但这里有。
我有一个 jQuery.Ajax 方法,它调用一个检索订单列表的 WebMethod。jQuery方法是这样的......
/* Get orders by the current logged in employee's department */
function getOrdersByDept(department, filter, dateFrom, dateTo) {
var dto = {};
dto['department'] = department;
dto['filter'] = filter;
dto['dateFrom'] = dateFrom;
dto['dateTo'] = dateTo;
$.ajax({
type: 'POST',
url: 'ProcessPO.aspx/GetOrdersByDept',
data: JSON.stringify(dto),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
displayOrders(data.d);
},
error: function (xhr, status, errorThrown) {
alert(xhr.responseText);
}
});
}
返回此 JSON...
从这个 WebMethod...
[WebMethod]
public static List<Order> GetOrdersByDept(Department department, Filter filter, DateTime? dateFrom = null, DateTime? dateTo = null)
{
return OrderLists.GetOrdersByDepartment(department, filter, dateFrom, dateTo);
}
现在这里是 displayOrders 代码...
/* Fill the orders table. */
function displayOrders(data) {
$('#gdvOrders tbody').empty();
for (var key in data) {
var altRow;
if (key % 2 == 0)
altRow = 'class="alt-row"';
else
altRow = '';
/* Convert JSON formatted date to readable short date format */
var orderDate = data[key].OrderDate.substr(6);
var currentTime = new Date(parseInt(orderDate));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
orderDate = day + "/" + month + "/" + year;
var orderStatus;
switch (data[key].Status) {
case 0:
orderStatus = 'Pending';
break;
case 1:
orderStatus = 'Closed';
break;
}
$('#gdvOrders tbody').append($('<tr ' + altRow + '><td>' + data.d[key].OrderId + '</td>' +
'<td>' + orderDate + '</td>' +
'<td>' + data[key].EmployeeName + '</td>' +
'<td>' + data[key].Items + '</td>' +
'<td>$' + data[key].SubTotal.toFixed(2) + '</td>' +
'<td>$' + data[key].TaxTotal.toFixed(2) + '</td>' +
'<td>$' + data[key].GrandTotal.toFixed(2) + '</td>' +
'<td>' + orderStatus + '</td></tr>'));
}
$('#gdvOrders tbody td:nth-child(4)').hide();
$('#gdvOrders tbody td:nth-child(5)').hide();
$('#gdvOrders tbody td:nth-child(6)').hide();
}
每个订单都有一个 Items 的属性,基本上是一个 List。我将其存储在表格的第 4 列中并将其隐藏。当用户单击表中的行/顺序时,我想将每个项目的另一个列表加载到另一个表中。为您查看而剥离的订单行的点击功能是这样的......
$('#gdvOrders tbody tr').live('click', function () {
displayItems($(this).find('td:nth-child(4)').text());
});
现在 displayItems 函数被定义为...
function displayItems(data)
我尝试了许多不同的方法来遍历数据,当我...
alert(data)
我收到...
[object Object], [object Object]
当我尝试遍历它(尝试了几种不同的方式)时,我得到了未定义的字符,或者当我尝试数据 [0] 时,数据1将显示 o 等字符,以此类推。基本上只是给我返回 [object Object] 的每个字符而不是数组中的对象。我究竟做错了什么?