0

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

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

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

<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 的示例(之前): 前 这就是它应该是什么样的..

我需要的

4

1 回答 1

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
    }],
}


更新 [..]
[..removed code from update..]
尝试编写可重用的代码(您想要项目和总计的类似输出)。
试着让这些方法做他们描述的事情(附加在容器之外,你无论如何都返回了该行)。
尝试使用正确的变量名。
尝试区分不同类型的功能。
即使在 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;
}

更新:

整个文件..但是我遇到了与原始代码相同的错误(方法 create 不存在)。但这与您的请求本身无关。

于 2012-07-31T10:31:10.390 回答