javascript - How can I to create a table and create 2 columns for each row on the fly -
how can create table , create 2 columns each row , each column hold checkbox variable time , assign table object property.
var time have string in form of following examples: 08:00 - 09:00 am;09:00 - 10:00 am;10:00 - 11:00 09:30 - 10:30 am;11:30 - 12:30 pm
if there semicolon in variable time string should break string pieces , create checkbox each piece.
for example in case of "08:00 - 09:00 am;09:00 - 10:00 am;10:00 - 11:00 am" should create table 2 rows , 2 columns , first row have “08:00 - 09:00 am” in first column , “09:00 - 10:00 am” in second column , second row have “10:00 - 11:00 am" in first column , noting in second column.
in other words bookarray[i-1][2] hold different table rows , each rows have 2 columns , each column have checkbox each record.
for(var = 0; < gassessorsarray.length; i++) { var time = gassessorsarray[i].time; var currenttable; bookarray[i] = new array(); bookarray[i][0] = gassessorsarray[i].id; bookarray[i][1] = '<input type="checkbox" id="bk_' + gassessorsarray[i].id + '" value="' + gassessorsarray[i].name + '" onchange="bookappointment(this)" />'; bookarray[i][2] = currenttable; }
let me give try:
my setup
html
<div id="output"></div> javascript
var gassessorsarray = []; gassessorsarray[0] = { id: 0, name: 'robert', time: '12:00' }; gassessorsarray[1] = { id: 1, name: 'chocolate', time: '9:00;4:35;12:30 - 1:00;00:00' }; gassessorsarray[2] = { id: 2, name: 'hot chocolate', time: '3:00;4:35;5:30' }; solution [demo]
var table = ""; var checkboxes = ""; (var = 0; < gassessorsarray.length; i++) { var times = gassessorsarray[i].time.split(';'); if (times.length < 3) { times[0] = times[1] == undefined ? '<td colspan="2"><input type="checkbox"/>' + times[0] + '</td>' : '<td><input type="checkbox"/>' + times[0] + '</td>' times[1] = times[1] == undefined ? '' : '<td><input type="checkbox"/>' + times[1] + '</td>'; checkboxes = (times[1] != undefined && times[1].length > 1) ? '<input type="checkbox" />' : ''; table = '<table><thead><tr><th colspan="2">' + gassessorsarray[i].name + '</th></tr></thead><tbody>'; table += '<tr id="' + gassessorsarray[i].id + '">' + times[0] + times[1] + '</tr></tbody></table>'; } else { if (times.length % 2 == 0) { (var j = 0; j < times.length; j++) { times[j] = j % 2 == 0 ? '<tr><td><input type="checkbox"/>' + times[j] + '</td>' : '<td><input type="checkbox"/>' + times[j] + '</td></tr>'; } } else { (var j = 0; j < times.length; j++) { if (j == times.length - 1) { times[times.length - 1] = '<tr><td colspan="2"><input type="checkbox"/>' + times[times.length - 1] + '</td></tr>'; continue; } times[j] = (j % 2 == 0) ? ('<tr><td><input type="checkbox"/>' + times[j] + '</td>') : ('<td><input type="checkbox"/>' + times[j] + '</td></tr>'); } } table = '<table><thead><tr><th colspan="2">' + gassessorsarray[i].name + '</th></tr></thead><tbody>' + times.join("") + '</tbody></table>'; } document.queryselector('#output').innerhtml += table; }
Comments
Post a Comment