Advertisement
braveheart1989

04. Table Builder

Nov 7th, 2016
159
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.             for (let names of columnNames) {
  12.                 let th = $('<th>');
  13.                 th.append((names));
  14.                 htmlHeading.append(th)
  15.             }
  16.             let actionTh = $('<th>');
  17.             actionTh.text('Action');
  18.             htmlHeading.append(actionTh);
  19.             $(selector).append(htmlHeading);
  20.  
  21.         },
  22.  
  23.         fillData: function fillData(dataRows) {
  24.             let dataRow = $('<tr>');
  25.             for (let row = 0; row < dataRows.length; row++) {
  26.                 for (let col = 0; col < dataRows[row].length; col++) {
  27.                     let td = $('<td>').text(dataRows[row][col]);
  28.                     dataRow.append(td);
  29.                 }
  30.                 let tableData = $('<td>');
  31.                 let deleteBtn = $('<button>Delete</button>').on('click',(deleteItems));
  32.                 deleteBtn.appendTo(tableData);
  33.                 dataRow.append(tableData);
  34.                 $('table').append(dataRow);
  35.                 dataRow = $('<tr>');
  36.             }
  37.         }
  38.     };
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement