1

am creating one SharePoint hosted app where am creating table from JavaScript and i there are many columns to which i need to set a unique ID am using this code

var itemInfo = '';
    var innerHtml = "<table border=1 class='content1'><tr><th> Room </th><th>8:00</th><th>9:00</th><th>10:00</th><th>11:00</th><th>12:00</th><th>13:00</th><th>14:00</th><th>15:00</th><th>16:00</th><th>17:00</th></tr> ";
    var enumerator = items.getEnumerator();
    var count = 0;
    var count = items.get_count();

    for (var r = 0; r <= count; r++)
    {
        for (var c = 0; c <= 10; c++)
        {
            while (enumerator.moveNext()) {

                var currentListItem = enumerator.get_current();
                if (currentListItem.get_item('Title') != null) {
                    innerHtml += "<tr><td  id=" + currentListItem.get_item('Room') + ">" + currentListItem.get_item('Room') + "</td><td id="+r+c+"></td><td id="+r+c+"></td><td id="+r+c+"></td><td id="+r+c+"></td><td id="+r+c+"></td><td id="+r+c+"></td><td id="+r+c+"></td><td id="+r+c+"></td><td id="+r+c+"></td><td id="+r+c+"></td></tr>";
                }

            }
        }
    }
    $("#rooms").html(innerHtml);

but here the values of r and c are not getting increament,can any one tell me what's wrong with my code?

2 Answers 2

2

Define for each row

    var tr = table.insertRow(0);

For the number of cells try to use

    var td = tr.insertCell(0);

and to set id to td try to use

td.id= currentListItem.get_item('Room');

And to set value of cell

td.innerHTML= "your value";

Regarding your situation it's prefered to create a function to create your rows and cells based on your requirment as paramter and call it in while.

Example

Give your main table ID as myTable

enter image description here

function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);


for (i = 0; i < 5; i++) { 
    var cell = row.insertCell(i);
    cell.id = i;
    cell.innerHTML = "Qassas" + i;
    }
}

OutPut

enter image description here

0

The while (enumerator.moveNext()) statement will iterate through all items before letting the indexes (c & r) to increment.

If you want one item per cell, try replacing 'while' with 'if'.

ps. not really a SharePoint question is it? :-)

2
  • no change in result after changing while to if
    – Madhav
    Aug 19, 2016 at 9:58
  • i posted this as because i thought am using list and all that if somebdy replied then there should not be any need of explaination regarding things user
    – Madhav
    Aug 19, 2016 at 9:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.