Ich habe eine Bootstrap-Tabelle erstellt, in der Zeilen dynamisch hinzugefügt werden. Es gibt jedoch eine Diskrepanz in der Tabellenkörper- und Tabellenkopfausrichtung, wie unten im Abschnitt gezeigt.Nicht übereinstimmende Tabellenkopf- und Tabellenkörperspalte
function display() {
var tableRef = document.getElementById('myTable1').getElementsByTagName('tbody')[0];
var rowsAdd = tableRef.insertRow(tableRef.rows.length);
// var m = moment().format('YYYY-MM-DD h:mm a');
var newCell = rowsAdd.insertCell();
newCell.innerHTML = "<input type='text' form ='form1' class= 'form-control input-sm' value=' ' id = 'typeofdr' name= 'typeofdr' required>";
newCell.style.width = '100px';
newCell = rowsAdd.insertCell();
newCell.innerHTML = "<tr><td><input form ='form1' class= 'form-control input-sm' value=' ' type='text' id = 'drugname' name= 'drugname' required> </td></tr>";
newCell.style.width = '100px';
newCell = rowsAdd.insertCell();
newCell.innerHTML = "<tr><td><input form ='form1' class= 'form-control input-sm' value=' ' type='text' id = 'strdrug' name= 'strdrug' required> </td></tr>";
newCell.style.width = '100px';
newCell = rowsAdd.insertCell();
newCell.innerHTML = "<tr><td><input form ='form1' class= 'form-control input-sm' value=' ' type='text' id = 'dosage' name= 'dosage' required> </td></tr>";
newCell.style.width = '50px';
newCell = rowsAdd.insertCell();
newCell.innerHTML = "<tr><td><input form ='form1' class= '' type='checkbox' id = 'dm' value= 'on' name= 'dm' ><input type='hidden' name='dm' value='off' form='form1'> </td></tr>";
newCell.style.width = '37.5px';
newCell = rowsAdd.insertCell();
newCell.innerHTML = "<tr><td><input form ='form1' class= '' type='checkbox' id = 'da' name= 'da' value='on' ><input type='hidden' name='da' value='off' form='form1'> </td></tr>";
newCell.style.width = '37.5px';
newCell = rowsAdd.insertCell();
newCell.innerHTML = "<tr><td><input form ='form1' class= '' type='checkbox' id = 'de' name= 'de' value='on' ><input type='hidden' name='de' value='off' form='form1'> </td></tr>";
newCell.style.width = '37.5px';
newCell = rowsAdd.insertCell();
newCell.innerHTML = "<tr><td><input form ='form1' class= '' type='checkbox' id = 'dn' name= 'dn' value='on' ><input type='hidden' name='dn' value='off' form='form1'> </td></tr>";
newCell.style.width = '37.5px';
newCell = rowsAdd.insertCell();
newCell.innerHTML = "<tr><td><select form ='form1' class= 'form-control input-sm' value=' ' id = 'baf' name= 'baf' required><option>Before</option><option>After</option></select> </td></tr>";
newCell.style.width = '90px';
newCell = rowsAdd.insertCell();
newCell.innerHTML = "<tr><td><input form ='form1' class= 'form-control input-sm' value=' ' type='text' id = 'totn' name= 'totn' required> </td></tr>";
newCell.style.width = '70px';
newCell = rowsAdd.insertCell();
newCell.innerHTML = "<tr><td><input form ='form1' class= 'form-control input-sm' value=' ' type='text' id = 'nofdays' name= 'nofdays' required> </td></tr>";
newCell.style.width = '50px';
newCell = rowsAdd.insertCell();
newCell.innerHTML = "<tr><td class='tds'><i class='fa fa-trash-o' font-size:20px' onclick='deleteRow(this)'></i></td></tr>";
newCell.style.width = '50px';
}
function deleteRow(r) {
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("myTable1").deleteRow(i);
}
table.alpha th {
background-color: #009999;
color: white;
}
table.alpha .tbalpha {
height: 200px;
overflow-y: auto;
}
table.alpha .thalpha,
.tbalpha {
display: block;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<table class="table table-striped table-bordered table-responsive table-hover table-condensed alpha" style="width: 760px; " id="myTable1">
<thead class="thalpha">
<tr>
<th rowspan="2" style="width:100px;">Item</th>
<th rowspan="2" style="width:100px;">Name of Drug</th>
<th rowspan="2" style="width:100px;">Strength of Drug</th>
<th rowspan="2" style="width:50px;">Dosage</th>
<th colspan="4" style="width:150px;">Frequency</th>
<th rowspan="2" style="width:90px;">Before /After Food</th>
<th rowspan="2" style="width:70px;">Total No. of Tablets to be dispensed</th>
<th rowspan="2" style="width:50px;">No. of Days</th>
<th rowspan="2" style="width:30px;">Delete Row</th>
</tr>
<tr>
<th style="width:37.5px;">M</th>
<th style="width:37.5px;">A</th>
<th style="width:37.5px;">E</th>
<th style="width:37.5px;">N</th>
</tr>
</thead>
<tbody class="tbalpha">
</tbody>
<tr id="hide">
<td><i class='fa fa-plus' style='font-size:20px; color : #ff9900;' onclick="display()"></i></td>
</tr>
</table>
</div>
Da beide Tabellenspaltenüberschriften und Spaltentabellenkörper Reihen ausgerichtet sind, nicht richtig nicht genau unterhalb ihrer coressponding Kopfzellen sind Tabellenzellen i, e in der obigen Schnipsel, gezeigt. Ich denke, dass etwas mit der CSS falsch sein könnte, aber wenn display: block in CSS entfernt wird, wird die Körpergröße des Tisches reduziert, obwohl seine Höhe eingestellt ist (im obigen css-Code ist tbody height auf 200px eingestellt).
Danke, aber Höhe des Tischkörpers wird reduziert. Wir können die Standardhöhe von tbody nicht auf einen Wert einstellen. – coder12346
Display-Block ist erforderlich, um Scroll auf tbody zu implementieren, wenn die Höhe von tbody mehr als Standardhöhe Ritus ist? – coder12346
Wenn Sie eine Bildlaufleiste wünschen, können Sie 'max-height' auf' tbody' verwenden, keine 'display: block', wenn Sie eine bestimmte Höhe haben möchten, wenn es keine oder wenig Zeilen gibt, können Sie' height' verwenden anstelle von "min-height" auf dem tbody, aber die Reihen würden auf die Höhe von 'tbody' erweitert werden – am05mhz