0

I want to print a value in each table cell after creating it dynamically.

<table id="MapDetails"><tr>
<td/><td/><td/><td/>
var colIndex = 4;
foreach(MapDetail geMapDetail in Model.mapDetails)
{    
<td class="test">
       <script>{getPosition(@geResult.assessmentId, @colIndex, @rowIndex, '@geResult.ResultValue');}</script>
</td>
colIndex++;
}
</tr></table>

My script

THIS DOES NOT WORK

function getPosition(id, colIndex, rowIndex, resultValue) {

    var element = '#' + id;
    var cell = $('#MapDetails tr:eq(' + rowIndex + ') td:eq(' + colIndex + ')');
     if($(element).index() == colIndex){
        cell.innerHTML = resultValue;
     }

}

THIS WORKS ONLY FOR THE FIRST CELL

function getPosition(id, colIndex, rowIndex, resultValue) {

    var element = '#' + id;
    var cell = $(".test").closest('tr').find('td').get(colIndex);
     if($(element).index() == colIndex){
        cell.innerHTML = resultValue;
     }

}
Samra
  • 1,815
  • 4
  • 35
  • 71

2 Answers2

0

It is mostly a algorithm problem. You must loop through all TRs and nested TDs and write that one value that you want to write.

$('#tableid').find('tr').each(function(index, element){

  $(element).find('td').each(function(indexd, elementd){

    $(elementd).html('blah blah');

 });

});
Mukesh
  • 69
  • 7
  • what is meant by element here? in my code element is totally different from cell. also i am present inside the td itself when i call my getPosition function so traversing every time doesnt seem like a good idea...i have tried $(this).parent().children('td').get(colIndex) but even that doesnt work – Samra Mar 16 '17 at 05:30
  • please ask the question properly..I answered your question "I want to print a value in each table cell after creating it dynamically" – Mukesh Mar 16 '17 at 05:42
0

I found my answer here: How to set table cell value using jquery

so i did:

var cell = $("#MapDetails").children().children()[rowIndex].children[colIndex];

and that's it! Cheers

Community
  • 1
  • 1
Samra
  • 1,815
  • 4
  • 35
  • 71