Advertisement
braveheart1989

04. Table Builder

Nov 7th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function tableBuilder(selector) {
  2.    
  3.     function deleteItems() {
  4.        $(this).parent().parent().remove()
  5.  
  6.     }
  7.  
  8.     return {
  9.         createTable: function createTable(columnNames) {
  10.             let htmlHeading = $('<table>');
  11.             let tr = $('<tr>');
  12.             htmlHeading.append(tr);
  13.             for (let names of columnNames) {
  14.                 let th = $('<th>').text(names);
  15.                 // th.append((names));
  16.                 th.appendTo(tr);
  17.             }
  18.             let actionTh = $('<th>');
  19.             actionTh.text('Action');
  20.             actionTh.appendTo(tr);
  21.             $(selector).append(htmlHeading);
  22.         },
  23.  
  24.         fillData: function fillData(dataRows) {
  25.             let dataRow = $('<tr>');
  26.             for (let row = 0; row < dataRows.length; row++) {
  27.                 for (let col = 0; col < dataRows[row].length; col++) {
  28.                     let td = $('<td>').text(dataRows[row][col]);
  29.                     dataRow.append(td);
  30.                 }
  31.                 let tableData = $('<td>');
  32.                 let deleteBtn = $('<button>Delete</button>').on('click',(deleteItems));
  33.                 deleteBtn.appendTo(tableData);
  34.                 dataRow.append(tableData);
  35.                 $('table').append(dataRow);
  36.                 dataRow = $('<tr>');
  37.             }
  38.         }
  39.     };
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement