SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript table help

    hello!

    i'll go straight to the point, i have 8 rows and 3 columns, i want to populate each <td> starting from:
    row 0 column 0 a value of 1
    row 0 column 1 a value of 2
    row 0 column 2 a value of 3
    row 1 column 0 a value of 4
    row 1 column 1 a value of 5
    ...
    row 7 column 2 a value of 24

    also if i choose say row 2 column 2 as starting point the rest of the <td>'s before it will not contain any value...

    any help from the experts is really appreciated!

    here's the code
    Code JavaScript:
    <script type="text/javascript">
    	function popIt() {
    		var t = document.getElementById('tblVisaStk').rows;
    		//var t = document.getElementById('tblVisaStk');
    		var k = getCookie('matrix'); // starting point, set in a different file
    		var r = k[1]; // e.g. from R2C1, extract coordinates row 2
    		var c = k[3]; // column 1
    		var col;
    		for(i = r; i < 8; i++) {
                            // can't get it to work
    			//if (c == 1) {	col = 0; } else	if (c == 2) { col = 2; } else { col = 0; }
    			for (j = c; j < 3; j++) {
    				if (i == r) { j++; }
    				//if (k == 'R'+i+'C'+j) {
    					var y = t[i].cells;
    					y[j].innerHTML = i+j;
    				//}
    				/*var oCell = t.rows(i).cells(j);
    				oCell.innerHTML = 'test';*/
    			}
    		}
    		/*var oTable = document.getElementById("tblVisaStk");
    		var oCell = oTable.rows(2).cells(1);
    		oCell.innerHTML = 'test';*/
    	}
    </script>

  2. #2
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a few errors in your script.

    Try this and see how it works for ya.

    I'm assuming that r=1, and c=1 (per your cookie) refers to the first cell of the first row in the table (t.rows[0].cells[0]).

    Also assuming that if you start at r=2, c=2, you don't want ANY of the first cell to be filled... so you'll fill a 2x8 block (16 cells total) and the first column will be empty.

    If you just want 24 cells filled, wrapping around to full rows, that will be a different solution.

    Code JavaScript:
    <script type="text/javascript">
        function popIt() {
            var t = document.getElementById('tblVisaStk');
            var k = getCookie('matrix'); // starting point, set in a different file
            var r = k[1]; // e.g. from R2C1, extract coordinates row 2
            var c = k[3]; // column 1
            var col;
            for(var i = r-1; i < r+6; i++) {
                for (var j = c-1; j < 3; j++) {
                    var val = (i-r+1)*3 + j-c+1;
                    if (t.rows.length > i) {
                        t.rows[i].cells[j].innerHtml = val;
                    }
                }
            }
        }
    </script>

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Quote Originally Posted by transio View Post
    There are a few errors in your script.

    Try this and see how it works for ya.

    I'm assuming that r=1, and c=1 (per your cookie) refers to the first cell of the first row in the table (t.rows[0].cells[0]). - first row = 0 and first col = 0

    Also assuming that if you start at r=2, c=2, you don't want ANY of the first cell to be filled... so you'll fill a 2x8 block (16 cells total) and the first column will be empty. - ignore cells before it based on what is on the cookie

    If you just want 24 cells filled, wrapping around to full rows, that will be a different solution.

    Code JavaScript:
    <script type="text/javascript">
        function popIt() {
            var t = document.getElementById('tblVisaStk');
            var k = getCookie('matrix'); // starting point, set in a different file
            var r = k[1]; // e.g. from R2C1, extract coordinates row 2
            var c = k[3]; // column 1
            var col;
            for(var i = r-1; i < r+6; i++) {
                for (var j = c-1; j < 3; j++) {
                    var val = (i-r+1)*3 + j-c+1;
                    if (t.rows.length > i) {
                        t.rows[i].cells[j].innerHtml = val;
                    }
                }
            }
        }
    </script>
    thank you for your reply.

    i tried your code with cookie value at row 0 and col 0 and got an error
    'rows[...].cells' is null or not an object
    but for other row values and column values didn't display anthing.

  4. #4
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did some testing and got it working... apparently innerHtml doesn't wanna take!!!
    This should do it
    HTML Code:
    <script type="text/javascript">
        function popIt() {
            var t = document.getElementById('tblVisaStk');
            var k = getCookie('matrix'); // starting point, set in a different file
            var r = k[1]; // e.g. from R2C1, extract coordinates row 2
            var c = k[3]; // column 1
            var col;
            for(var i = r; i < r+8; i++) {
                for (var j = c; j < 3; j++) {
                    if (t.rows.length > i) {
    					if (t.rows[i].cells.length > j) {
    						//t.rows[i].cells[j].innerHtml = (i-r)*3 + j + 1;
    						t.rows[i].cells[j].appendChild(document.createTextNode(''+((i-r)*(3-c) + (j-c) + 1)));
    					}
                    }
                }
            }
        }
    </script>
    [/QUOTE]

  5. #5
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by transio View Post
    Did some testing and got it working... apparently innerHtml doesn't wanna take!!!
    This should do it
    HTML Code:
    <script type="text/javascript">
        function popIt() {
            var t = document.getElementById('tblVisaStk');
            var k = getCookie('matrix'); // starting point, set in a different file
            var r = k[1]; // e.g. from R2C1, extract coordinates row 2
            var c = k[3]; // column 1
            var col;
            for(var i = r; i < r+8; i++) {
                for (var j = c; j < 3; j++) {
                    if (t.rows.length > i) {
    					if (t.rows[i].cells.length > j) {
    						//t.rows[i].cells[j].innerHtml = (i-r)*3 + j + 1;
    						t.rows[i].cells[j].appendChild(document.createTextNode(''+((i-r)*(3-c) + (j-c) + 1)));
    					}
                    }
                }
            }
        }
    </script>
    [/QUOTE]

    thank you very much Steve, i did some testing too, now it's displaying...
    unfortunately, this is what it's supposed to do, please extend your patience more

    | x | x | x |
    | x | 1 | 2 |
    | 3 | 4 | 5 |
    .......etc.....

    x = blank

    thanks Steve, I just can't get it to work...

  6. #6
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here you go... just change the "24" to whatever number you want for the # of items to display...


    HTML Code:
    <script type="text/javascript">
        function popIt() {
            var t = document.getElementById('tblVisaStk');
            var k = getCookie('matrix'); // starting point, set in a different file
            var r = k[1]; // e.g. from R2C1, extract coordinates row 2
            var c = k[3]; // column 1
            var cols = 24;
            for(var i = 0; i < cols; i++) {
                var x = (i+r*3+c) % 3;
                var y = Math.floor((i+r*3+c)/3);
                if (t.rows.length > y) {
                    if (t.rows[y].cells.length > x) {
                        t.rows[y].cells[x].appendChild(document.createTextNode(''+(i+1)));
                    }
                }
            }
        }
    </script>

  7. #7
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by transio View Post
    Here you go... just change the "24" to whatever number you want for the # of items to display...


    HTML Code:
    <script type="text/javascript">
        function popIt() {
            var t = document.getElementById('tblVisaStk');
            var k = getCookie('matrix'); // starting point, set in a different file
            var r = k[1]; // e.g. from R2C1, extract coordinates row 2
            var c = k[3]; // column 1
            var cols = 24;
            for(var i = 0; i < cols; i++) {
                var x = (i+r*3+c) % 3;
                var y = Math.floor((i+r*3+c)/3);
                if (t.rows.length > y) {
                    if (t.rows[y].cells.length > x) {
                        t.rows[y].cells[x].appendChild(document.createTextNode(''+(i+1)));
                    }
                }
            }
        }
    </script>
    thanks steve, really appreciate it, no data is displayed?

  8. #8
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    Quote Originally Posted by corics15 View Post
    thanks steve, really appreciate it, no data is displayed?
    hey steve, thank you! i finally did it!

    Code:
    <script type="text/javascript">
    	function popIt() {
    		var t = document.getElementById('tblVisaStk');
    		var k = getCookie('matrix'); // starting point, set in a different file
    		var r = k[1]; // e.g. from R2C1, extract coordinates row 2
    		var c = k[3]; // column 1
    		var cnt = 0;
    		for(var i = 0; i < 8; i++) {
    			for (var j = 0; j < 3; j++) {
    				t.rows[i].cells[j].appendChild(document.createTextNode((i < r || (i == r && j < c))? '' : cnt++));
    			}
    		}
    	}
    </script>

  9. #9
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @transio

    I think you wanted: innerHTML

    Note:
    innerHtml - won't work


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •