0

I have a multi-dimensional array where not all elements will be populated. I want to output the contents of that array into an HTML table. I currently have this:

for (var basketRowJuice = 0; basketRowJuice < 10; basketRowJuice++){
    var outputName = "";
    outputName = (basketArray[0][basketRowJuice]);
    document.getElementById("basketJuiceName" + basketRowJuice).innerHTML = outputName;
}

for (var basketRowQuant = 0; basketRowQuant < 10; basketRowQuant++){
    var outputQuant = 0;
    outputQuant = (basketArray[1][basketRowQuant]);
    document.getElementById("basketJuiceQuant" + basketRowQuant).innerHTML = outputQuant;
}

for (var basketRowPrice = 0; basketRowPrice < 10; basketRowPrice++){
    var outputPrice = 0;
    outputPrice = (basketArray[2][basketRowPrice]);
    document.getElementById("basketJuicePrice" + basketRowPrice).innerHTML = outputPrice;
}

HTML table:

<table id="basket" cellpadding="0" cellspacing="0" summary="This table lists the items you have in your shopping basket">
                                <caption>Your basket</caption>
                                <tr>
                                    <th scope="col">Juice name</th>
                                    <th scope="col">Number of crates</th>
                                    <th scope="col">Total price</th>
                                </tr>
                                <tr>
                                    <td id="basketJuiceName0"></td>
                                    <td id="basketJuiceQuant0"></td>
                                    <td id="basketJuicePrice0"></td>
                                </tr> 
                                <tr>
                                    <td id="basketJuiceName1"></td>
                                    <td id="basketJuiceQuant1"></td>
                                    <td id="basketJuicePrice1"></td>
                                </tr>   
                                <tr>
                                    <td id="basketJuiceName2"></td>
                                    <td id="basketJuiceQuant2"></td>
                                    <td id="basketJuicePrice2"></td>
                                </tr>
                                <tr>
                                    <td id="basketJuiceName3"></td>
                                    <td id="basketJuiceQuant3"></td>
                                    <td id="basketJuicePrice3"></td>
                                </tr> 
                                <tr>
                                    <td id="basketJuiceName4"></td>
                                    <td id="basketJuiceQuant4"></td>
                                    <td id="basketJuicePrice4"></td>
                                </tr>  
                                <tr>
                                    <td id="basketJuiceName5"></td>
                                    <td id="basketJuiceQuant5"></td>
                                    <td id="basketJuicePrice5"></td>
                                </tr>  
                                <tr>
                                    <td id="basketJuiceName6"></td>
                                    <td id="basketJuiceQuant6"></td>
                                    <td id="basketJuicePrice6"></td>
                                </tr>  
                                <tr>
                                    <td id="basketJuiceName7"></td>
                                    <td id="basketJuiceQuant7"></td>
                                    <td id="basketJuicePrice7"></td>
                                </tr>  
                                <tr>
                                    <td id="basketJuiceName8"></td>
                                    <td id="basketJuiceQuant8"></td>
                                    <td id="basketJuicePrice8"></td>
                                </tr>  
                                <tr>
                                    <td id="basketJuiceName9"></td>
                                    <td id="basketJuiceQuant9"></td>
                                    <td id="basketJuicePrice9"></td>
                                </tr>
                            </table>

However if my array is only populated in columns [0][9], [1][9] and [2][9] then i end up with a huge gap of empty rows in my HTML, before the output. Can anyone suggest a better way to do this? Perhaps so that my javascript inserts a table row for each populated array element?

I'm a newbie to programming so please excuse my no-doubt verbose and inelegant code.

Thank you, Jon

4
  • That's weird format. Can you post your exact html please? Commented Jul 18, 2012 at 13:53
  • I tried to paste my html code but it wouldn't display! Commented Jul 18, 2012 at 13:58
  • Paste it, then select it and press Ctrl + K. Commented Jul 18, 2012 at 14:04
  • Do your arrays have 'undefined' where there is no data or some other content? (null, 0 (zero) ). If you have 'no data' to display for first row, I believe you don't want it at all, right? And third question: is it possible to have for a row values in just one array? (that is only one or two from name, price or quantity? Commented Jul 19, 2012 at 7:20

1 Answer 1

1

You probably want something more like the following inside your loops.

var outputPrice = basketArray[2][basketRowPrice];
if (outputPrice) {
    // do something here.  the if statement will be true for non-null, non-empty, non-zero values of the var outputPrice.
}

I'll also note that you can replace your 3 loops by one like:

for (var j = 0; j < 10; j++) {
    for (var i = 0; i < 3; i++) {
        var outputName = basketArray[i][j];
        var outputQuant = basketArray[1][basketRowQuant];
        //.. do the rest of the code
    }
}

The final step would be to remove the hard coded HTML and generate it dynamically, the following is without checking, so it probably is off a bit, look up the DOM methods.

var tbody = document.getElementById('basket').tbodies[0];
for (var j = 0; ...) {
    var tr = document.createElement('tr');
    tbody.appendChild(tr);
    for (var i = 0; ..) {
        var td = document.createElement('td');
        td.innerHTML = ...;
        tr.appendChild(td);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.