0

我正在根据我在 ajax 调用中获得的 XML 数据制作表格。通话工作正常,我得到了正确的数据。但是,当我创建表时,第一个表中的数据被添加到第二个表中,然后都被添加到第三个表中,依此类推。

这是代码:

    function loadAcct(acctid,acctName){
            //removes the current li elements from our list before making new ones.



            $("#title-name").append(" - "+acctName);

            //ajax call to the server to get the needed information.

            $.ajax({

                url: "/_admin_/"+ acctid + "-grps.xml",

                contentType: "xml",

                type: "GET",

                async: false,

                success: function(xml){

                    //loop to find the name of the <SavedCart> elements

                    $("#demo").append('<div id="tabs"><ul></ul></div>');

                    $(xml).find('SavedCart').each(function(){

                        $(this).find("Name").each(function(){

                            groupName = $(this).text();

                            groupNameUp = groupName.toUpperCase(groupName);

                            // We only want to output the groups that have CRIB in the name

                            if(groupNameUp.indexOf("CRIB") != -1){

                                $("ul").append("<li><a href=\"#tabs-" + p + "\">" + groupName + "<\/a><\/li>");

                                $(this).parent().find('ItemList').each(function(){

                                    $(this).find('Item').each(function(){

                                        partNums.push( $('PartNumber',this).text())

                                        descs.push( $('Description',this).text())

                                        //cpns.push( $('CustomerPartNumber',this).text())

                                    })

                                })

                                for(c=0;c<partNums.length;c++){
                                    for(d=0;d<pn.length;d++){
                                        if(partNums[c] == pn[d]){
                                            cpns1[c] = cpns[d];
                                            mins1[c] = mins[d];
                                            maxs1[c] = maxs[d];
                                            boxqtys1[c] = boxqtys[d];
                                            break;
                                        }else{
                                            cpns1[c] = "";
                                            mins1[c] = "";
                                            maxs1[c] = "";
                                            boxqtys1[c] = "";
                                        }
                                    }
                                }

                                if (partNums.length > 0){

                                    var table = '<div id="tabs-' + p + '" class = remove><form id="orderform' + p + '" name="orderform' + p + '" action="order.php" method="POST" enctype="multipart/form-data" onSubmit="return checkpo(' + acctid + ', ' + p + ')"><table border="1" cellpadding="10" name="mytable' + p + '" id="mytable' + p + '" class="sortable"><thead><tr><th>On Hand<\/th><th>Eclipse ID<\/th><th>Customer Part Number<\/th><th>Description<\/th><th>Minimum<\/th><th>Maximum<\/th><\/th><th>Box Qty<\/th><th>Amount to Order<\/th><th>Select For Order<\/th><\/tr><\/thead><tbody id="maintablebody" >';

                                    for(l=0;l<partNums.length;l++){

                                        table += '<tr><td><input type="text" id="onhand' + l + '" class="onhand' + l + '" size="5" onchange="autocheckbox(' + l + ')"></td><td>' + partNums[l] + '<\/td><td>' + cpns1[l] + '<\/td><td>' + descs[l] + '<\/td><td class="min' + l + '">' + mins1[l] + '<\/td><td class="max' + l + '">' + maxs1[l] + '<\/td><td class="bxqty' + l + '">' + boxqtys1[l] + '<\/td><td class="tdamt'+ l +'"><input type="text" name="amt[]" id="amt[]" class="amt' + l +'" size="6" onchange="chkfrmamt(' + l + ')" /><\/td><td style="text-align: center;"><input type="checkbox" name="check[]" id="check[]" value="' + partNums[l] + '" class="check' + l +'" onClick="autofill(' + l + ')" /><\/td><\/tr>';

                                    }

                                    table += '<tr><td colspan="8"><label>PO Number:<\/label><input type="text" id="PO" name="PO" class="PO' + p + '" width="20" \/><\/td><\/tr><\/tbody><\/table><div id="submit"><input type="submit" id="sub" value="Order" /><input type="reset" value="Reset" name="reset" /></div><input type="hidden" id="SessID" value="' + sessID + '" name="SessID" \/><input type="hidden" id="eID" value="' + acctid + '" name="eID" \/><\/form><\/div>';

                                    $("#tabs").append(table);

                                    p++;

                                    m = p;

                                }

                            }

                        })
                        //clearArr();
                    })

                }

            })
            for(q=0;q<=m;q++){
                $("table#mytable" + q + " tr:odd").addClass("alt");
            }

            $( "#tabs" ).tabs();
        }

一个正确方向的点会有很大帮助。

这是所要求的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<SP-XML>
   <SavedCartItemList>
  <SavedCart>
     <EntityID>7429</EntityID>
     <ContactID>0</ContactID>
     <Type>group</Type>
     <Name>CRIB2</Name>
     <Description/>
     <Min/>
     <Max/>
     <Location/>
     <ShipTo>0</ShipTo>
     <ShipVia/>
     <ShipComplete>No</ShipComplete>
     <BlindShip>No</BlindShip>
     <ShipComments/>
     <ReleaseNumber/>
     <Date>08/01/2011</Date>
     <Total>0.0</Total>
     <DataSource>XML</DataSource>
     <SiteCode/>
     <CustomerPO/>
     <OrderNotes/>
     <ShippingInstructions/>
     <ShipToName/>
     <ShipAddrLine1/>
     <ShipAddrLine2/>
     <ShipCity/>
     <ShipState/>
     <ShipZip/>
     <ShipPhone/>
     <ShipEmailAddr/>
     <ShipReqDate/>
     <OrderByName/>
     <OrderByEmail/>
     <ItemList>
        <Item>
           <PartNumber>494261</PartNumber>
           <Description>EGS  BL-50 1/2 STEEL LOCKNUT</Description>
           <Quantity>1</Quantity>
           <UOM>ea</UOM>
           <CustomerPartNumber>2</CustomerPartNumber>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>494265</PartNumber>
           <Description>EGS  BL75 3/4 STEEL LOCKNUT</Description>
           <Quantity>1</Quantity>
           <UOM>ea</UOM>
           <CustomerPartNumber>3</CustomerPartNumber>
           <Comment/>
           <Thumbnail/>
        </Item>

     </ItemList>
  </SavedCart>
  <SavedCart>
     <EntityID>7429</EntityID>
     <ContactID>0</ContactID>
     <Type>group</Type>
     <Name>CRIB3</Name>
     <Description/>
     <Min/>
     <Max/>
     <Location/>
     <ShipTo>0</ShipTo>
     <ShipVia/>
     <ShipComplete>No</ShipComplete>
     <BlindShip>No</BlindShip>
     <ShipComments/>
     <ReleaseNumber/>
     <Date>01/01/2000</Date>
     <Total>0.0</Total>
     <DataSource>XML</DataSource>
     <SiteCode/>
     <CustomerPO/>
     <OrderNotes/>
     <ShippingInstructions/>
     <ShipToName/>
     <ShipAddrLine1/>
     <ShipAddrLine2/>
     <ShipCity/>
     <ShipState/>
     <ShipZip/>
     <ShipPhone/>
     <ShipEmailAddr/>
     <ShipReqDate/>
     <OrderByName/>
     <OrderByEmail/>
     <ItemList>
        <Item>
           <PartNumber>496987</PartNumber>
           <Description>IDEA 31-388 CLEARGLIDE QT BOTTLE</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>559215</PartNumber>
           <Description>SIEM Q120 BREAKER 20A 1P 120V 10K QP</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber>215</CustomerPartNumber>
           <Comment/>
           <Thumbnail/>
        </Item>
     </ItemList>
  </SavedCart>
  <SavedCart>
     <EntityID>7429</EntityID>
     <ContactID>0</ContactID>
     <Type>group</Type>
     <Name>JIM</Name>
     <Description/>
     <Min/>
     <Max/>
     <Location/>
     <ShipTo>0</ShipTo>
     <ShipVia/>
     <ShipComplete>No</ShipComplete>
     <BlindShip>No</BlindShip>
     <ShipComments/>
     <ReleaseNumber/>
     <Date>01/01/2000</Date>
     <Total>0.0</Total>
     <DataSource>XML</DataSource>
     <SiteCode/>
     <CustomerPO/>
     <OrderNotes/>
     <ShippingInstructions/>
     <ShipToName/>
     <ShipAddrLine1/>
     <ShipAddrLine2/>
     <ShipCity/>
     <ShipState/>
     <ShipZip/>
     <ShipPhone/>
     <ShipEmailAddr/>
     <ShipReqDate/>
     <OrderByName/>
     <OrderByEmail/>
     <ItemList>
        <Item>
           <PartNumber>1950428</PartNumber>
           <Description>AB   1794-AENT ETHERNET ADAPTER W/24V DC POWEL SUPPLY, TWIST PR</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>13083</PartNumber>
           <Description>AB   1794-ASB RIO ADAPTER</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>2917</PartNumber>
           <Description>AB   1794-IA16 85-132V AC INPUT MODULE, 16 POINT</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>2693</PartNumber>
           <Description>AB   1794-IB16 24V DC SINK INPUT MODULE, 16 POINT</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>23729</PartNumber>
           <Description>AB   1794-IB32 24 VDC INPUT MODULE, 32 POINTS</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>2696</PartNumber>
           <Description>AB   1794-IE8 ANALOG INPUT MODULE, 12 BIT, 8 POINT</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>2923</PartNumber>
           <Description>AB   1794-OA16 85-132V AC OUTPUT MODULE, 16 POINT</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>2715</PartNumber>
           <Description>AB   1794-OB16 24V DC SOURCE OUTPUT MODULE, 16 POINT</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>13084</PartNumber>
           <Description>AB   1794-OW8 ISOL RELAY OUT</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>13086</PartNumber>
           <Description>AB   1794-TB3 3 WIRE BASE</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>522685</PartNumber>
           <Description>CH   BR120 TYPE BR BREAKER 20A/1 POLE 1</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>524818</PartNumber>
           <Description>CH   CH120 TYPE CH BREAKER 20A/1 POLE</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>1769893</PartNumber>
           <Description>GEIN THQL1120 1P-20A 120V PLUG-IN CB</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>1812401</PartNumber>
           <Description>MIDW CB120 SP 20A 120/240V CB</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber/>
           <Comment/>
           <Thumbnail/>
        </Item>
        <Item>
           <PartNumber>559215</PartNumber>
           <Description>SIEM Q120 BREAKER 20A 1P 120V 10K QP</Description>
           <Quantity>1</Quantity>
           <UOM>0</UOM>
           <CustomerPartNumber>215</CustomerPartNumber>
           <Comment/>
           <Thumbnail/>
        </Item>

     </ItemList>
  </SavedCart>

function clearArr(){
            mins.length = 0;
            maxs.length = 0;
            namearr.length = 0;
            acctidarr.length = 0;
            partNums.length = 0;
            descs.length = 0;
            cpns.length = 0;
            cpns.length = 0;
            data.length = 0;
            mins1.length = 0;
            maxs1.length = 0;
            cpns1.length = 0;
            pns.length = 0;
            pn.length = 0;
            boxqtys.length = 0;
            boxqtys1.length = 0;
            p = 1; 
        }
4

1 回答 1

0

在不查看 XML 的情况下,我认为您可能需要为每个新元素重置partNums和?也许如果你发布一些 xml 文件,你做错了什么会更清楚。descsSavedCart

尝试这个

 $(xml).find('SavedCart').each(function(){
 partNums = []; 
 descs = []; 
于 2012-04-13T18:01:59.380 回答