打印运费和表格总费用

时间:2012-07-31 08:22:28

标签: javascript jquery e-commerce

我正在使用simplecart.js在我之前创建的网站上建立一个小商店。我有一个问题。整个购物车只显示一行代码:

<div class="simpleCart_items"></div>

并且javascript打印表,您无法在源代码中看到它。但是,chrome中的inspect元素是我的好朋友,创建的表格看起来像这样:

<table>
    <tbody>
        <tr class="headerRow">
            <th class="item-name">Tuote</th>
            <th class="item-price">Hinta</th>
            <th class="item-decrement"></th>
            <th class="item-quantity">Määrä</th>
            <th class="item-increment"></th>
            <th class="item-total">Yhteensä</th>
            <th class="item-remove"></th>
        </tr>
        <tr class="itemRow row-0 odd" id="cartItem_SCI-3">
            <td class="item-name">Teipit (Valkoinen hiilikuitu)</td>
            <td class="item-price">€48.00</td>
            <td class="item-decrement">
                <a href="javascript:;" class="simpleCart_decrement">-</a>
            </td>
            <td class="item-quantity">1</td>
            <td class="item-increment">
                <a href="javascript:;" class="simpleCart_increment">+</a>
            </td>
            <td class="item-total">€48.00</td>
            <td class="item-remove">
                <a href="javascript:;" class="simpleCart_remove">Poista</a>
            </td>
        </tr>
    </tbody>
</table>

我希望它能打印出这样的东西:

<table>
    <tbody>
        <tr class="headerRow">
            <th class="item-name">Tuote</th>
            <th class="item-price">Hinta</th>
            <th class="item-decrement"></th>
            <th class="item-quantity">Määrä</th>
            <th class="item-increment"></th>
            <th class="item-total">Yhteensä</th>
            <th class="item-remove"></th>
        </tr>
        <tr class="itemRow row-0 odd" id="cartItem_SCI-3">
            <td class="item-name">Teipit (Valkoinen hiilikuitu)</td>
            <td class="item-price">€48.00</td>
            <td class="item-decrement">
                <a href="javascript:;" class="simpleCart_decrement">-</a>
            </td>
            <td class="item-quantity">1</td>
            <td class="item-increment">
                <a href="javascript:;" class="simpleCart_increment">+</a>
            </td>
            <td class="item-total">€48.00</td>
            <td class="item-remove">
                <a href="javascript:;" class="simpleCart_remove">Poista</a>
            </td>
        </tr>
        <tr class="shippingtotal" id="shipping">
            <td class="item-name">Shipping</td>
            <td class="shipping-cost">€5.00</td>
            <td class="item-decrement">
                <a href="javascript:;" class="simpleCart_decrement">-</a>
            </td>
            <td class="item-quantity">1</td>
            <td class="item-increment">
                <a href="javascript:;" class="simpleCart_increment">+</a>
            </td>
            <td class="item-total">€48.00</td>
            <td class="item-remove">
                <a href="javascript:;" class="simpleCart_remove">Poista</a>
            </td>
        </tr>
    </tbody>
</table>

所以,这是simplecart.js本身: http://pastebin.com/j5VKGkV1

我认为我应该在代码的这一部分添加一些内容:

// write out cart
                writeCart: function (selector) {
                    var TABLE = settings.cartStyle.toLowerCase(),
                        isTable = TABLE === 'table',
                        TR = isTable ? "tr" : "div",
                        TH = isTable ? 'th' : 'div',
                        TD = isTable ? 'td' : 'div',
                        cart_container = simpleCart.$create(TABLE),
                        header_container = simpleCart.$create(TR).addClass('headerRow'),
                        container = simpleCart.$(selector),
                        column,
                        klass,
                        label,
                        x,
                        xlen;

                    container.html(' ').append(cart_container);

                    cart_container.append(header_container);


                    // create header
                    for (x = 0, xlen = settings.cartColumns.length; x < xlen; x += 1) {
                        column = cartColumn(settings.cartColumns[x]);
                        klass = "item-" + (column.attr || column.view || column.label || column.text || "cell") + " " + column.className;
                        label = column.label || "";

                        // append the header cell
                        header_container.append(
                        simpleCart.$create(TH).addClass(klass).html(label));
                    }

                    // cycle through the items
                    simpleCart.each(function (item, y) {
                        simpleCart.createCartRow(item, y, TR, TD, cart_container);
                    });

                    return cart_container;
                },

                // generate a cart row from an item
                createCartRow: function (item, y, TR, TD, container) {
                    var row = simpleCart.$create(TR).addClass('itemRow row-' + y + " " + (y % 2 ? "even" : "odd")).attr('id', "cartItem_" + item.id()),
                        j,
                        jlen,
                        column,
                        klass,
                        content,
                        cell;

                    container.append(row);

                    // cycle through the columns to create each cell for the item
                    for (j = 0, jlen = settings.cartColumns.length; j < jlen; j += 1) {
                        column = cartColumn(settings.cartColumns[j]);
                        klass = "item-" + (column.attr || (isString(column.view) ? column.view : column.label || column.text || "cell")) + " " + column.className;
                        content = cartCellView(item, column);
                        cell = simpleCart.$create(TD).addClass(klass).html(content);

                        row.append(cell);
                    }
                    return row;
                }

            });
但是什么?运输可以显示如下: <span class="simpleCart_shipping"></span>我试过在推车后添加它,但它看起来有点傻。

更新:这是一个没有mofication(之前)的例子: Before 这应该是它的样子......

What I need

1 个答案:

答案 0 :(得分:0)

首先添加:

    // [..]
    header_container = simpleCart.$create(TR).addClass('headerRow'),

    // Added line
    total_container = simpleCart.$create(TR).addClass('shippingtotal').attr('id', "shipping" ),

    container = simpleCart.$(selector),
    // [..]

    // create header
    // [..]

    // cycle through the items
    // [..]

    // create total
    cart_container.append(total_container);
    total_container.append( createTotalShippingRow() );   // Even though invoked just once

    return cart_container;

createCartRow: function (item, y, TR, TD, container) {
    // [..]
},

createCartTotalShippingRow: function() {

    // Impl. look other update
}

createTotalShippingRow: function() {

    // Impl. look other update
}

我会在impl上工作。现在:-)


将以下内容添加到您的设置中..(来自http://pastebin.com/j5VKGkV1

settings = {

    cartTotalColumns: [{
        attr: "name",
        label: "Name"
    }, {
        attr: "price",
        label: "Price",
        view: 'currency'
    }, {
        view: "empty",      // replaces decrement
        label: false
    }, {
        attr: "empty",      // replaces quantity
        label: false        // Qty
    }, {
        view: "empty",      // replaces increment
        label: false
    }, {
        attr: "total",
        label: "SubTotal",
        view: 'currency'
    }, {
        view: "empty",      // replaces remove
//      text: "Remove",
        label: false
    }],
}


更新[..]
[..删除更新的代码..]
尝试编写可重复使用的代码(你想要物品和总数的similair输出) 尝试让方法按照他们描述的方式进行操作(在容器外面添加,无论如何都返回了行) 尝试使用正确的变量名称 尝试分离不同类型的功能 即使在writeCart方法中,您也可以拆分'createHeader(..)createItems(..)createTotal(..)'。


此外,尝试使用语义名称。至少应该将'y'变量重命名为'rowIndex'或其他东西。

更新

createTotalShippingRow: function( TR, TD ) {

    // I think we shouldn't instantiate the item here, but you could test if this works?
    var item = {
        name: "Shipping",
        price: 5,               
//      view: "empty",      // decrement
//      attr: "empty",      // quantity
//      view: "empty",      // increment
        total: this.total(),
//      view: "empty",      // remove
        };

    var rowid = "shipping";

    return createCartTotalShippingRow( item, rowid, TR, TD );
},

createCartTotalShippingRow: function( item, rowclass, rowid, TR, TD ) {
    var row = simpleCart.$create(TR).addClass( rowclass ).attr( 'id', rowid ),
        i = 0,
        cartTotalColumns = settings.cartTotalColumns,
        ln = cartTotalColumns.length,
        column,
        classname;

    for ( ; i < ln; i++ ) {
        column = cartColumn( cartTotalColumns[ i ] );
        classname = "item-" + ( column.attr || ( isString( column.view ) ? column.view : column.label || column.text || "cell" ) ) + " " + column.className;
        content = cartCellView( item, column );
        cell = simpleCart.$create(TD).addClass(classname).html(content);
        row.append(cell);
    }

    return row;
}

更新:

whole file..但是我遇到了与原始代码相同的错误(方法创建不存在)。但这与你自己的请求无关。