Creating a Shopping Cart using only HTML/JavaScript

前端 未结 3 342
南笙
南笙 2020-12-24 03:59

I\'m not sure what to do in order to complete this project. I need to create a shopping cart that uses only one HTML page. I have the table set up showing what is being so

相关标签:
3条回答
  • 2020-12-24 04:30

    You simply need to use simpleCart

    It is a free and open-source javascript shopping cart that easily integrates with your current website.

    You will get the full source code at github

    0 讨论(0)
  • 2020-12-24 04:31

    I think it is a better idea to start working with a raw data and then translate it to DOM (document object model)

    I would suggest you to work with array of objects and then output it to the DOM in order to accomplish your task.

    You can see working example of following code at http://www.softxml.com/stackoverflow/shoppingCart.htm

    You can try following approach:

    //create array that will hold all ordered products
        var shoppingCart = [];
    
        //this function manipulates DOM and displays content of our shopping cart
        function displayShoppingCart(){
            var orderedProductsTblBody=document.getElementById("orderedProductsTblBody");
            //ensure we delete all previously added rows from ordered products table
            while(orderedProductsTblBody.rows.length>0) {
                orderedProductsTblBody.deleteRow(0);
            }
    
            //variable to hold total price of shopping cart
            var cart_total_price=0;
            //iterate over array of objects
            for(var product in shoppingCart){
                //add new row      
                var row=orderedProductsTblBody.insertRow();
                //create three cells for product properties 
                var cellName = row.insertCell(0);
                var cellDescription = row.insertCell(1);
                var cellPrice = row.insertCell(2);
                cellPrice.align="right";
                //fill cells with values from current product object of our array
                cellName.innerHTML = shoppingCart[product].Name;
                cellDescription.innerHTML = shoppingCart[product].Description;
                cellPrice.innerHTML = shoppingCart[product].Price;
                cart_total_price+=shoppingCart[product].Price;
            }
            //fill total cost of our shopping cart 
            document.getElementById("cart_total").innerHTML=cart_total_price;
        }
    
    
        function AddtoCart(name,description,price){
           //Below we create JavaScript Object that will hold three properties you have mentioned:    Name,Description and Price
           var singleProduct = {};
           //Fill the product object with data
           singleProduct.Name=name;
           singleProduct.Description=description;
           singleProduct.Price=price;
           //Add newly created product to our shopping cart 
           shoppingCart.push(singleProduct);
           //call display function to show on screen
           displayShoppingCart();
    
        }  
    
    
        //Add some products to our shopping cart via code or you can create a button with onclick event
        //AddtoCart("Table","Big red table",50);
        //AddtoCart("Door","Big yellow door",150);
        //AddtoCart("Car","Ferrari S23",150000);
    
    
    
    
    
    
    <table cellpadding="4" cellspacing="4" border="1">
        <tr>
            <td valign="top">
                <table cellpadding="4" cellspacing="4" border="0">
                    <thead>
                        <tr>
                            <td colspan="2">
                                Products for sale
                            </td>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>
                                Table
                            </td>
                            <td>
                                <input type="button" value="Add to cart" onclick="AddtoCart('Table','Big red table',50)"/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Door
                            </td>
                            <td>
                                <input type="button" value="Add to cart" onclick="AddtoCart('Door','Yellow Door',150)"/>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Car
                            </td>
                            <td>
                                <input type="button" value="Add to cart" onclick="AddtoCart('Ferrari','Ferrari S234',150000)"/>
                            </td>
                        </tr>
                    </tbody>
    
                </table>
            </td>
            <td valign="top">
                <table cellpadding="4" cellspacing="4" border="1" id="orderedProductsTbl">
                    <thead>
                        <tr>
                            <td>
                                Name
                            </td>
                            <td>
                                Description
                            </td>
                            <td>
                                Price
                            </td>
                        </tr>
                    </thead>
                    <tbody id="orderedProductsTblBody">
    
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="3" align="right" id="cart_total">
    
                            </td>
                        </tr>
                    </tfoot>
                </table>
            </td>
        </tr>
    </table>
    

    Please have a look at following free client-side shopping cart:

    SoftEcart(js) is a Responsive, Handlebars & JSON based, E-Commerce shopping cart written in JavaScript with built-in PayPal integration.

    Documentation

    http://www.softxml.com/softecartjs-demo/documentation/SoftecartJS_free.html

    Hope you will find it useful.

    0 讨论(0)
  • 2020-12-24 04:36

    For a project this size, you should stop writing pure JavaScript and turn to some of the libraries available. I'd recommend jQuery (http://jquery.com/), which allows you to select elements by css-selectors, which I recon should speed up your development quite a bit.

    Example of your code then becomes;

    function AddtoCart() {
      var len = $("#Items tr").length, $row, $inp1, $inp2, $cells;
    
      $row = $("#Items td:first").clone(true);
      $cells = $row.find("td");
    
      $cells.get(0).html( len );
    
      $inp1 = $cells.get(1).find("input:first");
      $inp1.attr("id", $inp1.attr("id") + len).val("");
    
      $inp2 = $cells.get(2).find("input:first");
      $inp2.attr("id", $inp2.attr("id") + len).val("");
    
      $("#Items").append($row);
        }
    

    I can see that you might not understand that code yet, but take a look at jQuery, it's easy to learn and will make this development way faster.

    I would use the libraries already created specifically for js shopping carts if I were you though.

    To your problem; If i look at your jsFiddle, it doesn't even seem like you have defined a table with the id Items? Maybe that's why it doesn't work?

    0 讨论(0)
提交回复
热议问题