Исправить заголовок таблицы HTML и столбец с помощью CSS - PullRequest
0 голосов
/ 08 мая 2019

Я пытаюсь исправить thead таблицы HTML и первые два столбца моей таблицы. Я отображаю таблицу HTML с данными JSON и исправляю первый столбец и полный thead

ПРОБЛЕМА

  • Я хочу исправить первые два столбца моей таблицы, т.е. Billdate и Total (всего 2 столбца в боковой области)
  • У меня естьуже зафиксировал первый столбец и заголовок, но когда я прокручиваю vertically, первый столбец прокручивается выше thead, как like this
  • , когда я применяю эту CSS
thead {
   position: sticky;
   top: 0;
   z-index: 10;
}
  • Как исчезла граница моей теады?

Любой, кто-нибудь здесь за любую помощь будет оценен

function format(number, decimals = 2, locale = 'en-in') {
  const fixed = parseInt(number).toFixed(decimals);
  const [int, dec] = fixed.split('.')
  const intFormatted = (+int).toLocaleString(locale)
  return intFormatted + (dec ? '.' + dec : '');
}

var data = [{
  "billdate": "2018-08-04",
  "outlet": "JAYANAGAR",
  "gross": 490465,
  "discount": 839,
  "GST": 28465,
  "amount": 518212
}, {
  "billdate": "2018-08-04",
  "outlet": "MALLESHWARAM",
  "gross": 99212,
  "discount": 0,
  "GST": 5567,
  "amount": 104801
}, {
  "billdate": "2018-08-04",
  "outlet": "KOLAR",
  "gross": 131349,
  "discount": 0,
  "GST": 6676,
  "amount": 138151
}, {
  "billdate": "2018-08-05",
  "outlet": "JAYANAGAR",
  "gross": 594466,
  "discount": 591,
  "GST": 34374,
  "amount": 628358
}, {
  "billdate": "2018-08-05",
  "outlet": "MALLESHWARAM",
  "gross": 109029,
  "discount": 0,
  "GST": 6062,
  "amount": 115113
}, {
  "billdate": "2018-08-05",
  "outlet": "KOLAR",
  "gross": 127449,
  "discount": 0,
  "GST": 6511,
  "amount": 134107
}, {
  "billdate": "2018-08-06",
  "outlet": "JAYANAGAR",
  "gross": 167811,
  "discount": 0,
  "GST": 9968,
  "amount": 177866
}, {
  "billdate": "2018-08-06",
  "outlet": "KOLAR",
  "gross": 62796,
  "discount": 0,
  "GST": 3257,
  "amount": 66095
}, {
  "billdate": "2018-08-07",
  "outlet": "JAYANAGAR",
  "gross": 267398,
  "discount": 268,
  "GST": 15898,
  "amount": 283124
}, {
  "billdate": "2018-08-07",
  "outlet": "MALLESHWARAM",
  "gross": 55381,
  "discount": 0,
  "GST": 3383,
  "amount": 58789
}, {
  "billdate": "2018-08-07",
  "outlet": "KOLAR",
  "gross": 64586,
  "discount": 6,
  "GST": 3285,
  "amount": 67886
}, {
  "billdate": "2018-08-08",
  "outlet": "JAYANAGAR",
  "gross": 295544,
  "discount": 246,
  "GST": 17716,
  "amount": 313128
}, {
  "billdate": "2018-08-08",
  "outlet": "MALLESHWARAM",
  "gross": 56453,
  "discount": 0,
  "GST": 3462,
  "amount": 59939
}, {
  "billdate": "2018-08-08",
  "outlet": "KOLAR",
  "gross": 65159,
  "discount": 0,
  "GST": 3381,
  "amount": 68558
}, {
  "billdate": "2018-08-09",
  "outlet": "JAYANAGAR",
  "gross": 303778,
  "discount": 201,
  "GST": 18115,
  "amount": 321797
}, {
  "billdate": "2018-08-09",
  "outlet": "MALLESHWARAM",
  "gross": 60795,
  "discount": 0,
  "GST": 3620,
  "amount": 64431
}, {
  "billdate": "2018-08-09",
  "outlet": "KOLAR",
  "gross": 54495,
  "discount": 0,
  "GST": 2841,
  "amount": 57352
}, {
  "billdate": "2018-08-10",
  "outlet": "JAYANAGAR",
  "gross": 305223,
  "discount": 53,
  "GST": 18287,
  "amount": 323556
}, {
  "billdate": "2018-08-10",
  "outlet": "MALLESHWARAM",
  "gross": 55584,
  "discount": 36,
  "GST": 3207,
  "amount": 58772
}, {
  "billdate": "2018-08-10",
  "outlet": "KOLAR",
  "gross": 41584,
  "discount": 0,
  "GST": 2128,
  "amount": 43722
}, {
  "billdate": "2018-08-11",
  "outlet": "JAYANAGAR",
  "gross": 439024,
  "discount": 177,
  "GST": 25148,
  "amount": 464127
}, {
  "billdate": "2018-08-11",
  "outlet": "MALLESHWARAM",
  "gross": 88009,
  "discount": 0,
  "GST": 5090,
  "amount": 93110
}, {
  "billdate": "2018-08-11",
  "outlet": "KOLAR",
  "gross": 59188,
  "discount": 0,
  "GST": 3156,
  "amount": 62213
}, {
  "billdate": "2018-08-12",
  "outlet": "JAYANAGAR",
  "gross": 593776,
  "discount": 809,
  "GST": 33689,
  "amount": 626772
}, {
  "billdate": "2018-08-12",
  "outlet": "MALLESHWARAM",
  "gross": 119723,
  "discount": 45,
  "GST": 7245,
  "amount": 126933
}, {
  "billdate": "2018-08-12",
  "outlet": "KOLAR",
  "gross": 59926,
  "discount": 0,
  "GST": 3170,
  "amount": 63119
}, {
  "billdate": "2018-08-13",
  "outlet": "JAYANAGAR",
  "gross": 157580,
  "discount": 340,
  "GST": 10053,
  "amount": 167391
}, {
  "billdate": "2018-08-13",
  "outlet": "KOLAR",
  "gross": 25730,
  "discount": 0,
  "GST": 1368,
  "amount": 27110
}, {
  "billdate": "2018-08-14",
  "outlet": "JAYANAGAR",
  "gross": 260106,
  "discount": 298,
  "GST": 15181,
  "amount": 275115
}, {
  "billdate": "2018-08-14",
  "outlet": "MALLESHWARAM",
  "gross": 55145,
  "discount": 19,
  "GST": 3480,
  "amount": 58633
}, {
  "billdate": "2018-08-14",
  "outlet": "KOLAR",
  "gross": 36664,
  "discount": 0,
  "GST": 1916,
  "amount": 37920
}, {
  "billdate": "2018-08-15",
  "outlet": "JAYANAGAR",
  "gross": 478163,
  "discount": 688,
  "GST": 27138,
  "amount": 504753
}, {
  "billdate": "2018-08-15",
  "outlet": "MALLESHWARAM",
  "gross": 98179,
  "discount": 0,
  "GST": 5661,
  "amount": 103855
}, {
  "billdate": "2018-08-15",
  "outlet": "KOLAR",
  "gross": 98536,
  "discount": 0,
  "GST": 4964,
  "amount": 103519
}, {
  "billdate": "2018-08-16",
  "outlet": "JAYANAGAR",
  "gross": 277139,
  "discount": 594,
  "GST": 16406,
  "amount": 293049
}, {
  "billdate": "2018-08-16",
  "outlet": "MALLESHWARAM",
  "gross": 52828,
  "discount": 0,
  "GST": 3227,
  "amount": 56071
}, {
  "billdate": "2018-08-16",
  "outlet": "KOLAR",
  "gross": 53312,
  "discount": 0,
  "GST": 2730,
  "amount": 56061
}, {
  "billdate": "2018-08-17",
  "outlet": "JAYANAGAR",
  "gross": 329539,
  "discount": 91,
  "GST": 19882,
  "amount": 349456
}, {
  "billdate": "2018-08-17",
  "outlet": "MALLESHWARAM",
  "gross": 62946,
  "discount": 0,
  "GST": 3659,
  "amount": 66624
}, {
  "billdate": "2018-08-17",
  "outlet": "KOLAR",
  "gross": 69126,
  "discount": 0,
  "GST": 3501,
  "amount": 72643
}, {
  "billdate": "2018-08-18",
  "outlet": "JAYANAGAR",
  "gross": 443783,
  "discount": 724,
  "GST": 25712,
  "amount": 468771
}, {
  "billdate": "2018-08-18",
  "outlet": "MALLESHWARAM",
  "gross": 95622,
  "discount": 0,
  "GST": 5507,
  "amount": 101151
}, {
  "billdate": "2018-08-18",
  "outlet": "KOLAR",
  "gross": 107235,
  "discount": 0,
  "GST": 5683,
  "amount": 112950
}, {
  "billdate": "2018-08-19",
  "outlet": "JAYANAGAR",
  "gross": 517922,
  "discount": 181,
  "GST": 28972,
  "amount": 546845
}, {
  "billdate": "2018-08-19",
  "outlet": "MALLESHWARAM",
  "gross": 96821,
  "discount": 0,
  "GST": 5490,
  "amount": 102334
}, {
  "billdate": "2018-08-19",
  "outlet": "KOLAR",
  "gross": 94158,
  "discount": 0,
  "GST": 4909,
  "amount": 99089
}, {
  "billdate": "2018-08-20",
  "outlet": "JAYANAGAR",
  "gross": 156224,
  "discount": 35,
  "GST": 9423,
  "amount": 165700
}, {
  "billdate": "2018-08-20",
  "outlet": "KOLAR",
  "gross": 45547,
  "discount": 0,
  "GST": 2347,
  "amount": 47905
}, {
  "billdate": "2018-08-21",
  "outlet": "JAYANAGAR",
  "gross": 289268,
  "discount": 214,
  "GST": 17613,
  "amount": 306776
}, {
  "billdate": "2018-08-21",
  "outlet": "MALLESHWARAM",
  "gross": 57684,
  "discount": 0,
  "GST": 3374,
  "amount": 61080
}, {
  "billdate": "2018-08-21",
  "outlet": "KOLAR",
  "gross": 57725,
  "discount": 0,
  "GST": 2950,
  "amount": 60682
}, {
  "billdate": "2018-08-22",
  "outlet": "JAYANAGAR",
  "gross": 395657,
  "discount": 159,
  "GST": 22808,
  "amount": 418418
}, {
  "billdate": "2018-08-22",
  "outlet": "MALLESHWARAM",
  "gross": 82752,
  "discount": 0,
  "GST": 4618,
  "amount": 87390
}, {
  "billdate": "2018-08-22",
  "outlet": "KOLAR",
  "gross": 74048,
  "discount": 0,
  "GST": 3953,
  "amount": 77922
}, {
  "billdate": "2018-08-23",
  "outlet": "JAYANAGAR",
  "gross": 302731,
  "discount": 1124,
  "GST": 17774,
  "amount": 319472
}, {
  "billdate": "2018-08-23",
  "outlet": "MALLESHWARAM",
  "gross": 63555,
  "discount": 0,
  "GST": 3565,
  "amount": 67142
}, {
  "billdate": "2018-08-23",
  "outlet": "KOLAR",
  "gross": 53637,
  "discount": 0,
  "GST": 2860,
  "amount": 56506
}, {
  "billdate": "2018-08-24",
  "outlet": "JAYANAGAR",
  "gross": 284354,
  "discount": 774,
  "GST": 16423,
  "amount": 300111
}, {
  "billdate": "2018-08-24",
  "outlet": "MALLESHWARAM",
  "gross": 48130,
  "discount": 0,
  "GST": 2857,
  "amount": 50997
}, {
  "billdate": "2018-08-24",
  "outlet": "KOLAR",
  "gross": 55040,
  "discount": 0,
  "GST": 2871,
  "amount": 57926
}]




let formatData = function(data) {
  let billdates = [];
  let outlets = [];
  data.forEach(element => {
    if (billdates.indexOf(element.billdate) == -1) {
      billdates.push(element.billdate);
    }
    if (outlets.indexOf(element.outlet) == -1) {
      outlets.push(element.outlet);
    }
  });
  return {
    data: data,
    billdates: billdates,
    outlets: outlets,

  };
};

let renderTable = function(data) {
  billdates = data.billdates;
  outlets = data.outlets;
  data = data.data;
  let tbl = document.getElementById("dailySales");
  let table = document.createElement("table");
  let thead = document.createElement("thead");
  let headerRow = document.createElement("tr");
  let th = document.createElement("th");
  th.innerHTML = "BillDate";
  th.classList.add("text-center");
  headerRow.appendChild(th);
  let grandTotal = 0;
  let grandGross = 0;
  let grandDiscount = 0;
  let grandGst = 0;
  let outletWiseTotal = {};
  let outletWiseGross = {};
  let outletWiseDiscount = {};
  let outletWiseGst = {};
  th = document.createElement("th");
  th.colSpan = 4;
  th.innerHTML = "Total";
  th.classList.add("text-center");
  headerRow.appendChild(th);
  outlets.forEach(element => {

    th = document.createElement("th");
    th.colSpan = 4;
    th.innerHTML = element;
    th.classList.add("text-center");
    headerRow.appendChild(th);

    outletWiseTotal[element] = 0;
    outletWiseGross[element] = 0;
    outletWiseDiscount[element] = 0;
    outletWiseGst[element] = 0;
    data.forEach(el => {
      if (el.outlet == element) {
        outletWiseTotal[element] += parseInt(el.amount);
        outletWiseGross[element] += parseInt(el.gross);
        outletWiseDiscount[element] += parseInt(el.discount);
        outletWiseGst[element] += parseInt(el.GST);
      }
    });
    grandTotal += outletWiseTotal[element]; //calculating totals for Total column
    grandGross += outletWiseGross[element];
    grandDiscount += outletWiseDiscount[element];
    grandGst += outletWiseGst[element];
  });

  thead.appendChild(headerRow);
  headerRow = document.createElement("tr");
  th = document.createElement("th");
  th.innerHTML = "";
  headerRow.appendChild(th);

  for (i = 0; i < outlets.length + 1; i++) {
    th = document.createElement("th");
    th.innerHTML = "Discount";
    th.classList.add("text-center");
    th.classList.add("discount");
    headerRow.appendChild(th);

    th = document.createElement("th");
    th.innerHTML = "GST";
    th.classList.add("text-center");
    th.classList.add("gst");
    headerRow.appendChild(th);

    th = document.createElement("th");
    th.innerHTML = "Net_Amount";
    th.classList.add("text-center");
    th.classList.add("netAmount"); //adding class to column Net Amount
    headerRow.appendChild(th);

    th = document.createElement("th");
    th.innerHTML = "Gross_Amount";
    th.classList.add("text-center");
    th.classList.add("grossAmount"); //adding class to column Gross Amount
    headerRow.appendChild(th);
  }

  headerRow.insertBefore(th, headerRow.children[1]);
  thead.appendChild(headerRow);
  table.appendChild(thead);

  headerRow = document.createElement("tr");
  td = document.createElement("th");
  td.innerHTML = "Total";
  td.classList.add("text-center");
  headerRow.appendChild(td);

  outlets.forEach(element => { // these are the table rows for each column
    td = document.createElement("th");
    td.innerHTML = outletWiseGross[element].toLocaleString('en-IN');
    td.classList.add("text-right");
    headerRow.appendChild(td);

    td = document.createElement("th");
    td.innerHTML = outletWiseDiscount[element].toLocaleString('en-IN');
    td.classList.add("text-right");
    headerRow.appendChild(td);

    td = document.createElement("th");
    td.innerHTML = outletWiseGst[element].toLocaleString('en-IN');
    td.classList.add("text-right");
    headerRow.appendChild(td);

    td = document.createElement("th");
    td.innerHTML = outletWiseTotal[element].toLocaleString('en-IN');
    td.classList.add("text-right");
    headerRow.appendChild(td);


  });
  td = document.createElement("th");
  td.innerHTML = grandTotal.toLocaleString('en-IN');
  td.classList.add("text-right");
  headerRow.insertBefore(td, headerRow.children[1]);

  td = document.createElement("th");
  td.innerHTML = grandGst.toLocaleString('en-IN');
  td.classList.add("text-right");
  headerRow.insertBefore(td, headerRow.children[1]);

  td = document.createElement("th");
  td.innerHTML = grandDiscount.toLocaleString('en-IN');
  td.classList.add("text-right");
  headerRow.insertBefore(td, headerRow.children[1]);

  td = document.createElement("th");
  td.innerHTML = grandGross.toLocaleString('en-IN');
  td.classList.add("text-right");
  headerRow.insertBefore(td, headerRow.children[1]);


  thead.appendChild(headerRow);
  table.appendChild(thead);

  let tbody = document.createElement("tbody");
  billdates.forEach(element => {
    let row = document.createElement("tr");
    td = document.createElement("td");
    td.innerHTML = element;
    row.appendChild(td);

    let total = 0;
    let totalGross = 0;
    let totalDiscount = 0;
    let totalGST = 0;
    outlets.forEach(outlet => {
      let ta = 0;
      let tg = 0;
      let tdi = 0;
      let tgst = 0;
      data.forEach(d => {
        if (d.billdate == element && d.outlet == outlet) {
          total += parseInt(d.amount);
          totalGross += parseInt(d.gross);
          totalDiscount += parseInt(d.discount);
          totalGST += parseInt(d.GST);
          ta = d.amount;
          tg = d.gross;
          tdi = d.discount;
          tgst = d.GST;
        }
      });

      td = document.createElement("td");
      td.innerHTML = tg.toLocaleString('en-IN');
      td.classList.add("text-right");
      row.appendChild(td);

      td = document.createElement("td");
      td.innerHTML = tdi.toLocaleString('en-IN');
      td.classList.add("text-right");
      row.appendChild(td);

      td = document.createElement("td");
      td.innerHTML = tgst.toLocaleString('en-IN');
      td.classList.add("text-right");
      row.appendChild(td);

      td = document.createElement("td");
      td.innerHTML = ta.toLocaleString('en-IN');
      td.classList.add("text-right");
      row.appendChild(td);




    });


    td = document.createElement("td");
    td.innerHTML = total.toLocaleString('en-IN');
    td.classList.add("text-right");
    row.insertBefore(td, row.children[1]);

    td = document.createElement("td");
    td.innerHTML = totalGST.toLocaleString('en-IN');
    td.classList.add("text-right");
    row.insertBefore(td, row.children[1]);

    td = document.createElement("td");
    td.innerHTML = totalDiscount.toLocaleString('en-IN');
    td.classList.add("text-right");
    row.insertBefore(td, row.children[1]);



    td = document.createElement("td");
    td.innerHTML = totalGross.toLocaleString('en-IN');
    td.classList.add("text-right");
    row.insertBefore(td, row.children[1]);


    tbody.appendChild(row);
  });

  table.appendChild(tbody);
  tbl.innerHTML = "";
  tbl.appendChild(table);
  table.classList.add("table");
  table.classList.add("table-striped");
  table.classList.add("table-bordered");
  table.classList.add("table-hover");

}
let formatedData = formatData(data);
renderTable(formatedData);


$(".checkbox-menu").on("change", "input[type='checkbox']", function() { // this one to select multiple options as check box

  $("#save").show();
  $(this).closest("li").toggleClass("active", this.checked);
  var sList = "";
  $('input[type=checkbox]').each(function() {
    if (this.checked) {
      sList += $(this).val() + ","
    }
  });

  $("#To").val(sList.slice(0, -1));
});

$(document).on('click', '.allow-focus', function(e) {
  e.stopPropagation();
});


$("#save").on("click", function() {

  var classes = $("#To").val().replace(/\s+/g, "").split(",");

  $(".table thead tr:not(:first) th:not(:first-child)").hide();
  $(".table tbody tr td:not(:first-child)").hide();

  $(".table thead tr:first th:not(:first)").attr("colspan", classes.length);

  for (i = 0; i < classes.length; i++) {
    var classToShow = classes[i].substr(0, 3).toLowerCase() + classes[i].substr(3);

    $("." + classToShow).each(function() {
      var index = $(this).index();

      $(".table thead tr:not(:first)").each(function() {
        $(this).find("th").eq(index).show();
      });

      $(".table tbody tr").each(function() {
        $(this).find("td").eq(index).show();
      });
    });
  }
});
.table-responsive {
  height: 400px;
  overflow: scroll;
}

thead {
  position: sticky;
  top: 0;
  z-index: 10;
  border: 1px solid red;
}

table tr>th:first-child {
  position: sticky;
  left: 0;
  z-index: 10;
  border: 1px solid red;
}

table tr>td:first-child {
  background: white;
  position: sticky;
  left: 0;
  z-index: 10;
  border: 1px solid red;
}

table.table-bordered>thead>tr>th {
  border: 1px solid white;
  white-space: nowrap;
  border-collapse: collapse;
  font-family: Verdana;
  font-size: 9pt;
  padding: 5px 5px 5px 5px;
  background-color: #697649;
  font-weight: normal;
  text-align: center;
  color: white;
}

table.table-bordered>tbody>tr>td {
  border: 1px solid rgba(29, 150, 178, 1);
  white-space: nowrap;
  border-collapse: collapse;
  font-family: Verdana;
  font-size: 8pt;
  background-color: #C5E5EC;
  padding: 5px 5px 5px 5px;
  color: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<div class="input-group">
  <input type="text" class="form-control" aria-label="Text input with dropdown button" name="To" id="To" readonly>
  <div class="input-group-append">
    <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>
    <ul class="dropdown-menu checkbox-menu allow-focus" aria-labelledby="dropdownMenu1">

      <li><label> <input type="checkbox"
							value="Gross Amount"> Gross Amount
					</label></li>

      <li><label> <input type="checkbox"
							value="Discount"> Discount
					</label></li>

      <li><label> <input type="checkbox"
							value="GST"> GST
					</label></li>
      <li><label> <input type="checkbox"
							value="Net Amount"> Net Amount	
					</label></li>


    </ul>
  </div>
  <button type="button" class="commonButton" id="save">
						<i class="fa fa-search"></i>&nbsp;Go
					</button>
</div>
<div class="table table-responsive">
  <table id="dailySales"></table>
</div>

как в crome thead:sticky не работает, я только что нашел это Добавил фрагмент кода также я пробовал таблицы данных, но, как вы можете видеть, яесть выпадающий список, когда я выбираю любую опцию, затем нажимаю кнопку ОК, я отображаю только выбранные столбцы **, но при использовании таблиц данных граница перекрывается и не совпадает с thead

, все еще первый столбец при перекрытии прокруткиthead

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...