file1.xml:
<CustomersRoot>
<Customer>
<CustomerID>1</CustomerID>
<CustomerName>Lucy</CustomerName>
</Customer>
...
file2.xml:
<OrdersRoot>
<Order>
<OrderID>1</OrderID>
<CustomerID>1</CustomerID> <!--foreign key-->
<OrderDescription>Order #1</OrderDescription>
</Order>
<Order>
<OrderID>2</OrderID>
<CustomerID>1</CustomerID> <!--foreign key-->
<OrderDescription>Order #2</OrderDescription>
</Order>
...
目前,我使用ajax将每个文件加载到单独的数组中,并在html页面上显示每个数组。但是,我想基于一对多关系组合数据。基本上,我想显示如下数据:
露 订单#1 订单#2 ... 实现这一目标的最简单方法是什么?
答案 0 :(得分:0)
请检查一下。
根据你的问题,我已经做到了。
$(document).ready(function() {
var orderSummery = new Array();
var orderXml ='<OrdersRoot> <Order> <OrderID>1</OrderID> <CustomerID>1</CustomerID> <!--foreign key--> <OrderDescription>Order #1</OrderDescription> </Order> <Order> <OrderID>2</OrderID> <CustomerID>1</CustomerID> <!--foreign key--> <OrderDescription>Order #2</OrderDescription> </Order> </OrdersRoot>';
var customerXml='<CustomersRoot> <Customer> <CustomerID>1</CustomerID> <CustomerName>Lucy</CustomerName> </Customer> </CustomersRoot>';
var customerObj = $.parseXML(customerXml);
$cust= $(customerObj);
$cust.find('CustomersRoot').find('Customer').each(function(i) {
var custid= $(this).find('CustomerID').text();
var custname = $(this).find('CustomerName').text();
var orderProduct='';
var orderObj = $.parseXML(orderXml);
$odr= $(orderObj);
$odr.find('OrdersRoot').find('Order').each(function(j) {
var custrefid= $(this).find('CustomerID').text();
if(custid === custrefid){
orderProduct+=$(this).find('OrderDescription').text() + " ";
}
});
if(orderProduct !== '')
orderSummery.push({
name: custname,
orderProduct : orderProduct
});
});
console.log(orderSummery);
});
让我知道它的工作与否......