2015-01-10 5 views
0

동적으로 생성 된 테이블이 있습니다. 사용자는 표에있는 객체를 배열에 넣는 버튼을 클릭 할 수 있습니다. 그러면 총 가격을 계산하는 "합계"테이블이 생성됩니다. 이 테이블에서 제거 단추가 있지만 제거 ("X") 단추와 제거 단추 모두를 클릭하면 아무 것도 수행되지 않습니다.동적으로 생성 된 테이블에서 행 제거

코드는 여기에서 찾을 수 있습니다 :

http://xsltransform.net/3NzcBt2

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method = "html" omit-xml-declaration = "no" doctype-system ="http://www.w3.org.TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public = "-//W3C//DTD XHTML 1.0 Strict//EN" /> 

<xsl:template match = "/data-set"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <link rel="stylesheet" type="text/css" href="Basket.css"/> 
    <title> My Shopping Basket </title> 
    <xsl:text disable-output-escaping="yes"> 
    &lt;script type=&quot;text/javascript&quot;&gt; 

    var basket = []; 

    // display basket and fill cells 
    function displaybasket(){ 
     var shoppingBasketTblBody = document.getElementById(&quot;shoppingBasketTblBody&quot;); 
     while(shoppingBasketTblBody.rows.length&gt;0) { 
     shoppingBasketTblBody.deleteRow(0); 
     } 

     var basket_total = 0.00; 

     //populating the table 

     for(var product in basket){ 
     var removeButton = document.createElement('input'); 
     removeButton.type = 'button'; 
     removeButton.value = 'X'; 
     removeButton.onclick = 'removeItem(this)'; 

     /* could not work out how to implement without using cookies, ran out of time. 
     var addButton = document.createElement('input'); 
     addButton.type = 'button'; 
     addButton.value = '+'; 
     addButton.onclick = 'addItem'; 

     var minusButton = document.createElement('input'); 
     minusButton.type = 'button'; 
     minusButton.value = '-'; 
     minusButton.onclick = 'minusItem;' */ 

     var row = shoppingBasketTblBody.insertRow(); 
     var cellName = row.insertCell(0); 
     var cellDescription = row.insertCell(1); 
     var cellPrice = row.insertCell(2); 
     var cellAmount = row.insertCell(3) 
     var cellRemove = row.insertCell(4); 

     cellPrice.align = &quot;right&quot;; 
     cellName.innerHTML = basket[product].Name; 
     cellDescription.innerHTML = basket[product].Description; 
     cellPrice.innerHTML = &quot;£&quot;+basket[product].Price.toFixed(2); 
     cellAmount.innerHTML = basket[product].Quantity; 
     cellRemove.appendChild(removeButton); 
     basket_total += parseFloat(basket[product].Price); 
     console.log(basket_total); 
     } 

     // display total cost 
     document.getElementById(&quot;cart_total&quot;).innerHTML=&quot;£&quot;+basket_total.toFixed(2); 
    } 


    function AddtoCart(name,Description,price, Quantity){ 
     //create product 
     var item = {}; 
     item.Name = name; 
     item.Description = Description; 
     item.Price = parseFloat(price); 
     item.Quantity = Quantity; 

     //push to basket 
     basket.push(item); 
     //call display basket function 
     displaybasket(); 
    } 

    function removeAll() { 
     basket.length = 0.0; 
     basket_total = 0.00; 
     document.getElementById(&quot;shoppingBasketTblBody&quot;).deleteRow(); 
    } 

    function removeItem(r) { 
     var i = r.parentNode.parentNode.rowIndex; 
     document.getElementById(&quot;shoppingBasketTblBody&quot;).deleteRow(i); 
     //subtract price of item from basket_total ??? 
    } 

    /*could not work out how to implement without using cookies, ran out of time. 
    function addItem() { 

    } 

    function minusItem() { 

    }*/ 

    &lt;/script&gt; 
    </xsl:text> 
    </head> 
    <body> 
    <table> 
     <thead> 
     <tr> 
      <th>Item Number</th> 
      <th>Item Name</th> 
      <th>Item Description</th> 
      <th>Quantity</th> 
      <th>Price</th> 
     </tr> 
     </thead> 
     <xsl:for-each select="basket"> 
     <tr> 
     <xsl:variable name="myId" select="@iId" /> 
      <td> 
      <xsl:value-of select="@id"/> 
      </td> 
      <td> 
      <xsl:value-of select="Product" /> 
      </td> 
      <td> 
      <xsl:value-of select="Description" /> 
      </td> 
      <td> 
      <xsl:value-of select="stockLevel" /> 
      </td> 
      <td> 
      £<xsl:value-of select="price" /> 
      </td> 
      <td> 
      <button type="button" onclick="AddtoCart('{Product}','{Description}','{price}','1')">Add one to cart</button> 
      </td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    <table id="shoppingBasketTbl"> 
       <thead> 
        <tr> 
         <td> 
          Name 
         </td> 
         <td> 
          Description 
         </td> 
         <td> 
          Price 
         </td> 
        </tr> 
       </thead> 
       <tbody id="shoppingBasketTblBody"> 

       </tbody> 
       <tfoot> 
        <tr> 
         <td colspan="3" align="right" id="cart_total"></td> 
         <td colspan="3" align="right" id="removeAll"><button type="button" id="remove all" onclick="removeAll" > Remove All </button></td> 
        </tr> 
       </tfoot> 
      </table> 
     </body> 
</html> 

<?xml version="1.0" encoding="iso-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="basket.xsl"?> 
<data-set> 
    <basket id="001"> 
    <Product>Shorts (F) </Product> 
    <Description>Stone Wash Denim Shorts</Description> 
    <stockLevel>20</stockLevel> 
    <price>25.90</price> 
    </basket> 
    <basket id="002"> 
    <Product>Bag (F)</Product> 
    <Description>Leather Shoulder Bag</Description> 
    <stockLevel>4</stockLevel> 
    <price>50.45</price> 
    </basket> 
    <basket id="003"> 
    <Product>Blouse (F)</Product> 
    <Description>Vintage Blue Silk Polka Dot Blouse</Description> 
    <stockLevel>8</stockLevel> 
    <price>45.99</price> 
    </basket> 
    <basket id="004"> 
    <Product>Boots (F)</Product> 
    <Description>Soft Leather Brown Ankle Boots</Description> 
    <stockLevel>3</stockLevel> 
    <price>65.35</price> 
    </basket> 
    <basket id="005"> 
    <Product>Belts (F)</Product> 
    <Description>Woven Finish Fashion Belt</Description> 
    <stockLevel>15</stockLevel> 
    <price>21.99</price> 
    </basket> 
    <basket id="006"> 
    <Product>Shirt (M)</Product> 
    <Description>Jacquard Pattern Wrangler Western Shirt</Description> 
    <stockLevel>19</stockLevel> 
    <price>34.87</price> 
    </basket> 
    <basket id="007"> 
    <Product>Shoes (M) </Product> 
    <Description>Suede Ankle Boots</Description> 
    <stockLevel>6</stockLevel> 
    <price>55.00</price> 
    </basket> 
    <basket id="008"> 
    <Product>Trousers (M)</Product> 
    <Description>Izod Peach Chinos</Description> 
    <stockLevel>23</stockLevel> 
    <price>31.75</price> 
    </basket> 
    <basket id="009"> 
    <Product>Belt (M)</Product> 
    <Description>Suede Casual Belt</Description> 
    <stockLevel>4</stockLevel> 
    <price>22.98</price> 
    </basket> 
    <basket id="010"> 
    <Product>Hat (M)</Product> 
    <Description>Trilby Style Brown Woven Fix</Description> 
    <stockLevel>2</stockLevel> 
    <price>67.80</price> 
    </basket> 
</data-set> 
+0

문제를 표시하는 최소한의 예제로 코드를 줄이는 것이 훨씬 좋습니다. 이것은 게시하기 전에 종종 당신을 답으로 이끄는 것이며 그렇지 않다면 대답하는 일을 훨씬 간단하게 만듭니다. 실제 브라우저 마크 업을 게시하는 코드가 아닌 게시하는 것이 훨씬 더 좋습니다. – RobG

답변

1

라인 :

removeButton.onclick = 'removeItem(this)'; 
단순히 온 클릭 속성에 문자열을 할당

, 당신은 너무 함수 참조 지정해야합니다

function removeItem() { 
    var r = this; 
    var i = r.parentNode.parentNode.rowIndex; // button -> cell -> row 
    document.getElementById(&quot;shoppingBasketTblBody&quot;).deleteRow(i); 
} 
다음 removeItem 기능에 다음

removeButton.onclick = removeItem; 

버튼 될 것입니다

var row = this.parentNode.parentNode; 
row.parentNode.removeChild(row); 
를 사용하면 버튼을 쉽게 제거 할 수 있습니다.

getElementById에 대한 종속성을 제거하십시오. 당신은 여전히 ​​DOM 구조에 묶여있다.

+0

onclick 코드를'removeButton.onclick = removeItem;'으로 변경하면, 새 제품을 바구니에 추가 할 때마다 함수가 실행됩니다. 사용자가 호출하지 않은 경우 함수를 종료하는 방법이 있습니까? – SilentUK

+0

내 댓글을 귀하의 게시물보기. xsltransform 링크에서 3 개의 빈 섹션이 나타납니다. 브라우저에 복사하여 붙여 넣을 수있는 형식으로 * 여기 * 코드를 게시하십시오. 즉, 브라우저가 가져온 것을 게시하고, 사용자가 묻는 것이 아니라면 브라우저를 생성하는 데 사용하지 않습니다. 한 번에 한 가지 우려를 제기하십시오. – RobG

관련 문제