2017-11-07 3 views
0

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).

Antwort

0

, die, weil Sie die Art und Weise sind zu verändern ist die tbody und die thead mit angezeigt:

table.alpha .thalpha, .tbalpha { 
    display: block; 
} 

, wenn Sie es entfernen, werden die Spalten

+0

Danke, aber Höhe des Tischkörpers wird reduziert. Wir können die Standardhöhe von tbody nicht auf einen Wert einstellen. – coder12346

+0

Display-Block ist erforderlich, um Scroll auf tbody zu implementieren, wenn die Höhe von tbody mehr als Standardhöhe Ritus ist? – coder12346

+0

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

0

Sie sind wohl nicht die display Eigenschaft ändern ausgerichtet werden von tbody, wie es Standardeigenschaft von display: table-row-group hat. Durch Entfernen von display wird es richtig ausgerichtet.

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 { 
 
    display: block; 
 
    height: 300px; 
 
    overflow-y: auto; 
 
}
<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>

Hinweisdisplay: block zu table Einstellung wird width-100% dehnen.

+0

Danke für Ihre Antwort! aber wir können die tbody height nicht auf 200px setzen; Ich habe es auf 200px in css gesetzt, aber wenn wir die Körpergröße des Tisches entfernen, wird – coder12346

+0

@ coder12346 reduziert, überprüfen Sie das aktualisierte Snippet. Sie können die 'display' -Eigenschaft nicht auf tbody setzen. Hier stelle ich 'height' und' display' (auch dieses wird nicht empfohlen) auf 'table'. was die gewünschte Ausgabe ergeben könnte. –

+0

Aber, wie man die Überschrift in der gleichen Position repariert und Scroll nur zum Tabellenkörper zur Verfügung stellt? – coder12346

Verwandte Themen