How to Read HTML table data and set in to String variable?

priyankajailia
Kilo Expert

Hi, I have a HTML table created on Catalog item. And On click on Add line button , one line gets added in to HTML area. So on submit I need the data from this HTML area to another String variable to process it further. Please help me on this.

find_real_file.png

<p>  </p>

<p>  </p>

<table id="dataTable2" style="border-collapse: collapse; border: 1px solid black;" width="100%">

<tbody>

<tr>

<td style="text-align: center; font: 14pt Calibri, sans-serif; background: #1f4e78; color: white; border: 1px solid black;" colspan="9">

<p><strong>New eSourcing Vendor Setup</strong></p>

</td>

</tr>

<tr><th style="text-align: center; font: 12pt Calibri, sans-serif; background: #c0c0c0; border: 1px solid black;" width="7%">

<p><strong>Supplier Type</strong></p>

</th><th style="text-align: center; font: 12pt Calibri, sans-serif; background: #c0c0c0; border: 1px solid black;" width="10%">

</th></tr>

</tbody>

</table>

===========================================

find_real_file.png

=============================

I tried below code but getElementById is   not working

var table_rows = g_form.getValue('app_html') + '';

var table = document.getElementById("dataTable2").value;

var table = document.getElementById("dataTable2");

var arrRows = table.getElementsByTagName("tr");

var arrAllCells = table.getElementsByTagName("td");

var arrHeaders = table.getElementsByTagName("th");

//cells by rows

for(var i=0;i<arrRows.length;i++){

  var arrRCells = arrRows[i].getElementsByTagName("td");

  alert("arrRCells " +arrRCells);

}

g_form.setValue("hold_html_data", table);

9 REPLIES 9

ramireddy
Mega Guru

you can use gel function instead of document.getElementByID. Once you get the table element, for table rows, directly use "table.rows" instead of getElementsByTagName("tr");



Instead of getElementsByTagName("td"), you might need through each table row and then needs to loop through cells.


HI Ram,



Do you have any sample code to achieve this?



Regards,


Check this. I didn't compile it though. Its plain javascript, iterating a table rows and columns.



var table_rows = g_form.getValue('app_html');


var table = gel("dataTable2");


var arrRows = table.rows;


//cells by rows


for(var i=0;i<arrRows.length;i++){


  for(var cellcounter = 0 ; cellcounter < arrRows[i].cells.length ; cellCounter++)


{


 


  alert("Cell text: " +arrRows[i].cells[cellCounter].innerHTML]);


}


}


g_form.setValue("hold_html_data", table.innerHTML);



One question I didn't understand is: Once you get the table, what are you trying to achieve? you wants to get entire inner html of table and wants to bind to some other control? if so, you don't need that loop. Directly that last line of assigning innerHTML is enough.


Actually I want the table row data in one variable like ABC,123 @ BCD,2300 @... so that I can use this in workflow. In workflow i want to process each row data.