0

Data Structure

var X = {
   a: [{name:"john", phone:777},{name:"john", phone:777},{name:"john", phone:777}],
   b: [{name:"john", phone:777},{name:"john", phone:777},{name:"john", phone:777}],
   c: [{name:"john", phone:777},{name:"john", phone:777},{name:"john", phone:777}],
   d: [{name:"john", phone:777},{name:"john", phone:777},{name:"john", phone:777}]
}

Function

function showTable(trnum,X,a) {
    var Tablecode = "<table><tbody>";
    for (var i=0; i< X.a.length;i++) {
        for (var j=0; j< trnum; j++) {
            Tablecode += "<tr><td>"+X.a[i].name+"</td><td>"+X.a[i].phone+"</td></tr>";
        }
    }
    Tablecode += "</tbody></table>";
    $("#elem").append(Tablecode);
}

Markup

<body>
    <button onclick="showTable(4,X,"a");"></button>
    <div id="elem">
    </div>
</body>

I'm trying to output data to table according to letter. one object on one table row.(all objects in table within one letter). this code doesn't work- unexpected token "}". oddly enough, it points to <div id="elem"> line. but there's no such character! How can I fix this?

UPDATE: thanks, error disappeared... but it outputs the whole structure, how do I make it output only 'a' objects?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DrStrangeLove
  • 11,227
  • 16
  • 59
  • 72

3 Answers3

1
<button onclick="showTable(4,X,"a");"></button>

should be like that (doublequotes):

<button onclick="showTable(4,X,'a');"></button>

Compare this one to yours (see .a and [a])

for (var i=0; i< X[a].length;i++){
 for (var j=0; j< trnum; j++){
 Tablecode += "<tr><td>"+X[a][i].name+"</td><td>"+X[a][i].phone+"</td></tr>";
 }

}

Feature reading about javascript notations

Community
  • 1
  • 1
pmaruszczyk
  • 2,157
  • 2
  • 24
  • 49
  • thanks, error disappeared... but it outputs the whole structure, how do i make it output only 'a' objects?? – DrStrangeLove May 11 '11 at 17:31
  • 12 beacuse `X[a]length(=3) * trnum (=4) = 12` You have 2 loops – pmaruszczyk May 11 '11 at 17:46
  • i see.. is there a chance to fix this?? to get only 3 (under 'a')johns?? – DrStrangeLove May 11 '11 at 17:50
  • yes, remove `for (var j=0; j< trnum; j++){` and `}` after `Tablecode += ...` . When you done this you can also change: `showTable(4,X,'a');` to `showTable(X,'a');` and `function showTable(trnum,X,a) { ` for `function showTable(X,a) {` – pmaruszczyk May 11 '11 at 17:57
  • thanks a lot!! you nailed it!! :) sorry but i have last question: how to disable button after first click?? to make it clickable but unexecutable after first click?? – DrStrangeLove May 11 '11 at 18:16
  • The shortest but, not so style-good result, is to add at the beggining of showTable function this line of code: `if ( window.mem__ ) { return;} window.mem__ = 1;` – pmaruszczyk May 11 '11 at 18:24
  • Thanks!! Definitely THE BEST ANSWER!! :) – DrStrangeLove May 11 '11 at 18:31
0

From tossing your code into a fiddle and getting it to run, I noticed that your button is a failure point. You have to surround the 'a' with SINGLE quotes.

http://jsfiddle.net/YaZp2/1/

EDIT: Also, edited the output code added the ability to actually see if it works.

josh.trow
  • 4,861
  • 20
  • 31
0

Double quotes used to delimit the onclick and also the params within.

Try

<button onclick="showTable(4,X,'a');">
James
  • 20,957
  • 5
  • 26
  • 41