简体   繁体   English

knotckout js foreach在IE 7和8中自动追加行

[英]knotckout js foreach appending rows automatically in IE 7 and 8

I have a "mini-cart" in a eCommerce site that I am bulding. 我正在建立的一个电子商务网站中有一个“迷你购物车”。 For the "mini-cart" i use foreach to add items to a table. 对于“迷你推车”,我使用foreach将项目添加到表格中。

The "mini-cart" is a dropdown and it is rendered from a razor file. “迷你购物车”是一个下拉菜单,是从剃刀文件中渲染的。 When i close the "mini-cart" and opens it again it automatically appends all the rows one more time. 当我关闭“迷你购物车”并再次打开它时,它会自动将所有行追加一次。 So everytime I close the "mini-cart" and opens it again. 因此,每次我关闭“迷你购物车”并再次打开它时。 All the rows gets appended one more time. 所有行都被追加一次。

When the cart is opened this code is run. 打开购物车后,将运行此代码。

showCart = function () {
    WebService.PostJson("/services/CartService.svc/GetCart", {},
        function (cartDto) {
            updateCart(cartDto);
            postman.deliver('cartShown', 1);
        },
        function () {

        });
};

The table looks like this. 该表如下所示。

<table width="100%" >
    <thead data-bind="foreach: Items, stripe: Items, evenClass: 'even', oddClass: 'odd'">
    <tr>
        <td data-bind="text: ArticleNo">
        </td>
        <td data-bind="text: Name" style="width:390px;">
        </td>
        <td>
            <input type="text" data-bind="value: Quantity" class="quantity"/>
        </td>
        <td class="cart-price-column">
            <span class="cartRowSubTotal" style="display: none;" data-bind="text: SubTotal"></span>
        </td>
        <td>
            <a href="#" class="erase" data-bind="click: remove">Ta bort</a>
        </td>
    </tr>
    </thead>
    </table>

updateCart does a Items.splice(0); updateCart执行Items.splice(0); so it should reload it self each time. 因此它应该每次自行重新加载。 But it does not seem to work i internet explorer 7 and 8. 但是它似乎在Internet Explorer 7和Internet Explorer 8中不起作用。

Is there anyway to "clear" the table everytime? 无论如何,每次都可以“清除”表吗? Or can the viewmodel figure this out on its own? 还是viewmodel可以自己解决这个问题?

Thanks. 谢谢。

UPDATE: 更新:

It seemed that the splice method did not empty the array for some reason. 似乎splice方法由于某种原因没有清空数组。 When changed to cart.Items([]) it started to work. 当更改为cart.Items([])时,它开始工作。

Set cart.items(null); 设置cart.items(null); Nothing to do with IE! 与IE无关!

In IE, the length parameter is required. 在IE中,length参数是必需的。 Leaving it empty causes the function to do nothing. 将其保留为空会使该函数不执行任何操作。 Call it like this: Items.splice(0, Items().length) . 这样Items.splice(0, Items().length)Items.splice(0, Items().length)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM