How to make HTML table expand on click?

前端 未结 6 1851
渐次进展
渐次进展 2020-12-06 05:05

I am rendering HTML table with the help of JavaScript. I have made the table successfully, but now I have one requirement to show some new data in a row like on click expand

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 06:04

    Do it this way, follow DRY and modular structure to keep it more readable and maintainable.

    class CellEntry {
      constructor() {
        this.sum = 0;
        this.percentage = 0;
      }
    }
    
    class OutletBasedRowEntry {
      constructor() {
        this.cells = {
          Total: new CellEntry()
        };
        this.childRows = {};
      }
      add(entry) {
        this.cells.Total.sum += entry.netamount;
        this.getOrCreateCellById(entry.outlet).sum += entry.netamount;
      }
      getOrCreateChildRowById(id) {
        if (!this.childRows[id]) this.childRows[id] = new OutletBasedRowEntry();
        return this.childRows[id];
      }
      getOrCreateCellById(id) {
        if (!this.cells[id]) this.cells[id] = new CellEntry();
        return this.cells[id];
      }
    }
    
    function tabulizeData(data) {
      let TotalRowEntry = new OutletBasedRowEntry();
      data.forEach(entry => {
        TotalRowEntry.add(entry);
        TotalRowEntry.getOrCreateChildRowById(entry.brandname).add(entry);
        TotalRowEntry.getOrCreateChildRowById(entry.brandname).getOrCreateChildRowById(entry.itemname).add(entry);
      });
      renderTable(TotalRowEntry);
    }
    
    function renderTable(TotalRowEntry) {
      let $table = $('#ConsumptionTable');
      let $thead = $('Brand NameTotal'),
        $tbody = $('');
      let $headingRows = $thead.find('tr');
    
      function addCellEntriesToRow(rowEntry, $row) {
        for (let cellName in TotalRowEntry.cells) {
          let cellEntry = rowEntry.getOrCreateCellById(cellName);
          $('').html(cellEntry.sum).appendTo($row);
          $('').html(cellEntry.percentage).appendTo($row);
        }
      }
    
      $.each(TotalRowEntry.cells, function(cellName, cellEntry) {
        $('').html(cellName).appendTo($headingRows.eq(0));
        $('Grn Entery').appendTo($headingRows.eq(1));
        $('Sales').appendTo($headingRows.eq(1));
        $('').html(cellEntry.sum).appendTo($headingRows.eq(2));
        $('').html(cellEntry.percentage).appendTo($headingRows.eq(2));
      });
    
      $.each(TotalRowEntry.childRows, function(brandName, rowEntry) {
        let $row = $('').appendTo($tbody);
        let rowId = 'row' + $row.index();
        let firstCell = $('' + brandName + '').appendTo($row);
        addCellEntriesToRow(rowEntry, $row);
        $.each(rowEntry.childRows, function(itemName, rowEntry) {
          $row = $('').addClass('collapse ' + rowId).appendTo($tbody);
          $('').html(itemName).appendTo($row);
          addCellEntriesToRow(rowEntry, $row);
        });
      });
    
      $thead.appendTo($table);
      $tbody.appendTo($table);
    }
    
    tabulizeData([{
        "outlet": "JAYANAGAR",
        "brandname": "Bakery FG",
        "itemname": "Khara Boondhi-L",
        "transactionType": "TransferIn",
        "netamount": 980
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Bakery FG",
        "itemname": "Samosa-L",
        "transactionType": "TransferIn",
        "netamount": 130
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Bakery FG",
        "itemname": "Corn Flakes Masala-L",
        "transactionType": "TransferIn",
        "netamount": 500
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Pastry & Cake FG",
        "itemname": "Plum Cake 250gm",
        "transactionType": "TransferIn",
        "netamount": 110
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Pastry & Cake FG",
        "itemname": "Butterscotch Cake",
        "transactionType": "TransferIn",
        "netamount": 720
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Pastry & Cake FG",
        "itemname": "Chocolate chips cake",
        "transactionType": "TransferIn",
        "netamount": 40000
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Pastry & Cake FG",
        "itemname": "Mango Delight Cake",
        "transactionType": "TransferIn",
        "netamount": 14000
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Pastry & Cake FG",
        "itemname": "Almond Honey Chocolate Cake",
        "transactionType": "TransferIn",
        "netamount": 500
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Pastry & Cake FG",
        "itemname": "Peach Cake",
        "transactionType": "TransferIn",
        "netamount": 5500
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Pastry & Cake FG",
        "itemname": "Black Forest Cake",
        "transactionType": "TransferIn",
        "netamount": 1000
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Chocolate Crazy Boom",
        "transactionType": "TransferIn",
        "netamount": 2360
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Hot Chocolate Fudge",
        "transactionType": "TransferIn",
        "netamount": 2340
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Chocolate Sugar Free Ice-Cream",
        "transactionType": "TransferIn",
        "netamount": 1000
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Kesar Badam Falooda",
        "transactionType": "TransferIn",
        "netamount": 4430
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Strawberry Ice-cream",
        "transactionType": "TransferIn",
        "netamount": 1231
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "TOP- Chocochips",
        "transactionType": "TransferIn",
        "netamount": 2200
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Cheese Cake Ice-Cream",
        "transactionType": "TransferIn",
        "netamount": 500
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Sundae Large",
        "transactionType": "TransferIn",
        "netamount": 2350
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Mango Ice-cream",
        "transactionType": "TransferIn",
        "netamount": 8000
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "TOP- Shooting Star",
        "transactionType": "TransferIn",
        "netamount": 2360
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Ice Blue Sundae",
        "transactionType": "TransferIn",
        "netamount": 2340
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Creamy Litchi Boom",
        "transactionType": "TransferIn",
        "netamount": 2200
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Cookies Ice-cream",
        "transactionType": "TransferIn",
        "netamount": 7000
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "TOP- Wafer",
        "transactionType": "TransferIn",
        "netamount": 88000
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Litchi cherry Sundae",
        "transactionType": "TransferIn",
        "netamount": 2440
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Peach Malaba",
        "transactionType": "TransferIn",
        "netamount": 2230
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "Ice Cream FG",
        "itemname": "Cherry Mania Ice-Cream",
        "transactionType": "TransferIn",
        "netamount": 2700
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "North Indian FG",
        "itemname": "Fruit Mixture",
        "transactionType": "TransferIn",
        "netamount": 324
      },
      {
        "outlet": "JAYANAGAR",
        "brandname": "NA",
        "itemname": "NA",
        "transactionType": "Sales",
        "netamount": 476426
      },
      {
        "outlet": "KOLAR",
        "brandname": "NA",
        "itemname": "NA",
        "transactionType": "Sales",
        "netamount": 115313
      },
      {
        "outlet": "MALLESHWARAM",
        "brandname": "NA",
        "itemname": "NA",
        "transactionType": "Sales",
        "netamount": 92141
      }
    ]);
    .add-btn {
      color: green;
      cursor: pointer;
      margin-right: 6px;
    }
    
    
    
    
    

提交回复
热议问题