This could be useful for anyone working with a 3rd party library that takes an object as input or for APIs and stored procs. It converts the table object in LF Forms classic to an object ready to plug in to whatever your working with.
function ParseLFTable(tableClass) { var dataArray = []; var data = ''; $('.' + tableClass + ' tbody tr').each(function() { data += '{'; $(this).find('td').each(function() { let className = $(this).attr('class'); if (className && (className.indexOf("action") >= 0 || className.indexOf("hideRowLabel") >= 0 || className.indexOf("col0") >= 0)) return; let label = $(this).find('label').html(); let value = $(this).find('input').val(); data += '"' + label + '":"' + value + '",'; }); data = data.slice(0, -1); data += '}'; dataArray.push(JSON.parse(data)); data = ''; }); return dataArray; }
Example usage with the datatables.net library to create an auto adjusting, searchable, sortable table.
var myData = ParseLFTable('cf-table-block') var colsArray = []; var column = ''; $.each(myData[0], function(key, value) { column = '{"data": "' + key + '", "title": "' + key + '"}'; colsArray.push(JSON.parse(column)); column = ''; }); $.getScript("https://cdn.datatables.net/2.1.5/js/dataTables.min.js", function() { $('#outputTable').html('<table id="dataTable" class="display" width="100%"></table>'); $('#dataTable').DataTable({ pageLength: 25, data: myData, columns: colsArray }); });
You can use the following method to convert it to a JSON object instead