How to display table records in an HTML variable in a catalog item from the onload catalog script
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
I want to display the contents of a table filtering on a specific field in an HTML variable on a catalog item. I put the code I found in an onload script but can't seem to get it to work. Is there an out of the box server script include I can use? I assume TableDataLoader is an example of a custom server side script that needs to be created?
function onLoad() {
var ga = new GlideAjax('TableDataLoader');
ga.addParam('sysparm_name', 'getMatchingRecords');
ga.addParam('sysparm_table_name', 'myTable');
ga.addParam('sysparm_encoded_query', 'tableColumn=xxxx');
ga.getXML(processResponse);
g_form.setValue('myHTMLvariable', response);
}
function processResponse(response) {
alert('function start');
var answer = response.responseXML.documentElement.getAttribute("answer");
var records = JSON.parse(answer);
var tableHeaders = ['Column1, 'Column2']; // Headers for your table
var tableHTML = '<table border="2" style="width: 100%; border-collapse: collapse;"><thead><tr>';
// Add headers
for (var i = 0; i < tableHeaders.length; i++) {
tableHTML += '<th style="padding: 5px;">' + tableHeaders[i] + '</th>';
}
tableHTML += '</tr></thead><tbody>';
// Add rows
if (records.length > 0) {
for (var j = 0; j < records.length; j++) {
tableHTML += '<tr>';
tableHTML += '<td style="padding: 5px;">' + records[j].u_column1 + '</td>';
tableHTML += '<td style="padding: 5px;">' + records[j].u_column2 + '</td>';
tableHTML += '</tr>';
}
document.getElementById('no_records').style.display = 'none';
} else {
document.getElementById('no_records').style.display = '';
}
tableHTML += '</tbody></table>';
document.getElementById('dvTable').innerHTML = tableHTML;
}
0 REPLIES 0