Bootstrap overflow y problem on table dynamic height - css

I have some problem with table-responsive class because it is generating scroll on y axis when it is not indicated on the css.
Here is picture of the problem and I also replicated the bug
<div id="app">
<div class="container">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>N°</th>
<th>Test</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>
<multiselect
v-model="value"
:options="options"
:multiple="true"
track-by="library"
:custom-label="customLabel"
>
</multiselect>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
https://jsfiddle.net/ggalvez92/3d8h4sqy/
I have tried to put overflow-y: visible but nothing seems to work.
I would appreciate if someone can help me to fix it.

If we change:
.multiselect__content-wrapper { display: contents; }
It can grow inside the table cell but affecting the table height.
Or if you use:
.table-responsive { overflow: unset; }
It can grow outside of the cell without affecting the table height.
So to fix the dropdown outside the table cell and keep overflow on the .table-responsive I don't believe this can be fixed CSS only. JavaScript solutions have been reported to:
add some bottom padding (only applicable when the dropdowns are on the last rows)
append the dropdown to the body and recalculate the offset
add position: fixed when open, and recalculate width, left and right offset
So, after some struggle with Vue (first time) I've found a solution that might suit your needs. This solution uses the position: fixed as mentioned earlier.
repositionDropdown (id){
const el = document.getElementById(id);
const parent = el.closest('.multiselect');
const content = parent.querySelector('.multiselect__content-wrapper');
const { top, height, left } = parent.getBoundingClientRect();
content.style.width = `${parent.clientWidth}px`;
content.style.position = 'fixed';
content.style.bottom = 'auto';
content.style.top = `${top + height}px`;
content.style.left = `${left}px`;
}
By adding the #open event to the setup it also requires an :id. For some reason this id needs to be numeric, I don't know why at this point. It might need some extra finetuning but at least it solves one of the bigger issues.
DEMO

Related

Inline styles for tables in ReactJS/JSX

Hi I'm having trouble adding inline styling to table components in React. Basically what I'm trying to do is so that the table header/cells are divided equally spacing so I'm adding width: '50%' styling to make this work. I added in the console and it works, but when I return to add it in my code, it doesn't.
I tried adding it to anything just to see if it work and it doesn't. Is there something I'm missing?
What it looks like:
What I want it to look like (after adding width styling to console):
JSX:
<table className="table table-striped">
<thead>
<tr styles={{width: '50%'}}>
<th styles={{width: '50%'}}>Hello</th>
<th styles={{width: '50%'}}>World</th>
</tr>
</thead>
<tbody>
{(data.length == 0)&&
<tr>
<td>I'm</td>
<td>Sam</td>
</tr>
}
</tbody>
</table>
As mentioned in the comments
Change 'styles' to 'style' – cidicles
Usually plural styles is the convention people use when passing a variable to another component, while singular style is the keyword that jsx-html tags will receive to inline the css.
Other answers recommend adding styles to html tags directly in the css. While adding styles on html tags directly without using classes may work it is worth it to note that it may not scale well This will require more work on us to come back and maintain/update the original code.
You can use table-layout: fixed; width: 100% on the table to force equal column widths:
table {
width: 100%;
table-layout: fixed;
}
table tr th {
text-align: left;
background: gray;
color: white;
}
<table className="table table-striped">
<thead>
<tr style={{width: '50%'}}>
<th style={{width: '50%'}}>Hello</th>
<th style={{width: '50%'}}>World</th>
</tr>
</thead>
<tbody
<tr>
<td>I'm</td>
<td>Sam</td>
</tr>
</tbody>
</table>
Your width: 50% isn't working most likely because your parent .table doesn't have a width set. You can try adding width: 100% to the table and then your 50% might work.
EDIT
As other users have mentioned, change styles to style as well.

How to horizontally scroll through columns in materialize css

I am using materialize css, even if I add lot of columns, they adjusts themselves according to the size of the page. I want to make them horizontally scrollable. How to make it scrollable? Here is my table code:
<table class="striped centered responsive-table">
<thead>
<tr>
<th></th>
<th>USN</th>
<th>Name</th>
<th>Department</th>
<th>Address</th>
<th>Student's Phone</th>
<th>Parent's Phone</th>
<th>Student's Email</th>
<th>Parent's Email</th>
</tr>
</thead>
<tbody>
<?php
$result=$db->query($semqy);
$i=0;
while($semdata=$result->fetch_assoc())
{
echo "<tr><td>
<input type='checkbox' id='chkbx$i' name='chkbx$i'/>
<label for='chkbx$i'></label>
</td><td>".$semdata['USN']."</td><td>".$semdata['Name']."</td><td>".$semdata['Dept']."</td><td>".$semdata['Address']."</td><td>".$semdata['Sphone']."</td><td>".$semdata['Pphone']."</td><td>".$semdata['Semail']."</td><td>".$semdata['Pemail']."</td></tr>";
$i++;
}
?>
<tr><td>Send Email</td></tr>
<tr>
<td>Student</td><td>
<input type="checkbox" id="schkbx" name="schkbx"/><label for="schkbx"></label></td>
<td>Parent</td><td><input type="checkbox" id="pchkbx" name="pchkbx"/><label for="pchkbx"></label></td>
</tr>
</tbody>
</table>
A proposed way to go around this is to wrap your table within a who's function is going to be a container and an overflow window.
<div class='table-wrapper'>
<!-- Table Content goes here -->
</div>
With table wrapper in place, you can hide the exceeding content in any overflow you wish. You can check the overflow properties and what they do here: https://developer.mozilla.org/en/docs/Web/CSS/overflow?v=example
For your need, since it's horizontal, we'd want to use the property overflow-x and to force a scroll bar to the division, we would add scroll to it.
Of course, you will have to define the max-width of your wrapper div to the desired measure. Additionnaly, you will ahve to force a size on your table cell to make them take the appropriate space you desire.
Also, very importantly, you will need to set the properties of your table as such:
#tableName {
width: 100%;
table-layout: fixed;
}
This will prevent your table from resizing with the container size.
You can look at an example here:
https://codepen.io/DrSandwich/pen/ZKyvoP

How to easily add/remove cells from a Table (Shifting existent cells)

I want a table to automatically align cells to be two per row, meaning that if a add/remove a cell, cells will shift to be two per row (with one on last row if total number of cells is odd).
Example:
<table>
<tr>
<td>old1</td> <td>old2</td>
</tr>
<tr>
<td>old3</td> <td>old4</td>
</tr>
</table>
To this:
<table>
<tr>
<td>NEW</td> <td>old1</td>
</tr>
<tr>
<td>old2</td> <td>old3</td>
</tr>
<tr>
<td>old4</td>
</tr>
</table>
Notice the automatically added row.
I'll show you three ways to accomplish what you need,
css3 Using flex (modern browsers only)
css Using inline-table (problem with cell height but usable as sort-of fallback)
jquery Using standard <table> (All browsers!)
Using CSS3's FLEX
.table{
display: flex;
flex-wrap: wrap;
}
.cell{
width:50%;
background:#ddd;
}
<div class="table">
<div class="cell">NEW</div>
<div class="cell">OLD1</div>
<div class="cell">OLD2</div>
<div class="cell">OLD3<br>new line</div>
<div class="cell">OLD4</div>
</div>
Using inline-table (issue with cell height)
display: table;   for the parent element and
display: inline-table for the inner DIVs:
.table{
display:table;
}
.cell{
display:inline-table;
width:50%; /* With percentage you simulate "how-many-per-Row" */
background:#ddd;
}
<div class="table">
<div class="cell">NEW</div>
<div class="cell">OLD1</div>
<div class="cell">OLD2</div> <!-- Works but notice this cell height -->
<div class="cell">OLD3<br>new line</div>
<div class="cell">OLD4</div>
</div>
You could use the inline-table as a fallback for older browsers - it's not the exact same result but without JavaScript it's the best you could get.
Take a look in the specs about the display property: https://developer.mozilla.org/en-US/docs/Web/CSS/display
Using <table>, <tr> and <td> and JavaScript (jQuery)
If you want to stick to good 'ol table elements you can use "a bit" of jQuery.
In that case it's a bit more complicated (see code-comments):
jQuery(function($){ // DOM is now ready
var $table = $("table"); // Get your table
var maxRowCells = 2; // only 2-per-Row
var counter = 0; // just a dummy counter
function prependNewCell(){
var $newCell = $("<td/>", {html: "NEW"+(++counter)});
// Is the last row full?
if($table.find("tr:last td").length == maxRowCells){
// If true, append a new TR to TABLE to shift TD's to
$table.append( $("<tr/>") );
}
// Shift all last TD's to the next row:
$table.find("tr:not(:last)").each(function(){
var $tdLast = $(this).find("td").last();
$(this).next("tr").prepend( $tdLast );
});
// Finally. Prepend the new TD element to the first TR
$table.find("tr").first().prepend( $newCell );
}
$("button").on("click", prependNewCell);
});
td{
width:50%;
background:#ddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>ADD NEW CELL ON TOP</button>
<table>
<tr>
<td>OLD1</td>
<td>OLD2</td>
</tr>
<tr>
<td>OLD3</td>
<td>OLD4</td>
</tr>
</table>
table {
empty-cells: hide;
}

emulate two column table inside div in pure css

so basically this is my layout. 3 row div (header content and footer)
what i want to do is emulate inside the content div a table format so i can display info as such
picture1 detail1
picture2 detail2
picture3 detail3
there are at least 10 rows of such info. doesn't have to be picture but can also be text
picture div will be about 150px while detail will be the rest. for the sake of example lets say 600px wrapper
i tried a few setups but it didn't come out the way i desired. in a table this would be a cinch but i would like a pure css table-less layout
something i tried but doesn't come out into columns
HTML
picture
item 1 goes here
picture2
item2 goes here
CSS
.itemwrapper{width:600px;}
picture{width:150px;float:left;}
item{width:450px;float:left;}
.clear {clear:both;}
please tell me how i can do this. jsfiddle example here - http://jsfiddle.net/4qvz220b/1/
table would be as simple as
<table width="500">
<tr>
<td width="150">picture1</td>
<td width="450">item1 goes here</td>
</tr>
<tr>
<td>
<td width="150">picture2</td>
<td width="450">item2 goes here</td>
</tr>
</table>
can someone point me in the right direction, i don't know much about css except what i can do through trial and error. the solution must be cross browser compatible css with no js or hacks etc.
please note that this is not to layout the entire page but just a two column content inside another div. if a unordered list can be used instead somehow, please let me know.
You almost got the structure right. You just need to use the right properties...
With css you can build the actual table structure using the display properties, as you have:
display: table
display: table-row
display: table-cell
And other, such as header and footer, if you use the right syntax.
So a basic example (without any style customization) would be something like:
FIDDLE LINK
<div class="mainwrapper">
<div class="itemwrapper">
<div class="picture">picture</div>
<div class="item">item 1 goes here</div>
</div>
<div class="itemwrapper">
<div class="picture">picture2</div>
<div class="item">item2 goes here</div>
</div>
</div>
css:
.mainwrapper {
display: table;
}
.itemwrapper {
display: table-row;
}
.picture, .item {
display: table-cell;
}

Responsive table alignment of columns

In responsive table design to refer I got this link jsfiddle.net/n4rUG/27/
This gives me below output:
Now I want to align title to left and its value to right
means it need to look aligned properly
How to do this?
The text-align: right doesn't work for your tds because it's overridden by the Bootstrap style with higher specifity. The quickest solution is just to add !important. Also, I'd suggest to make the labels floating to left (see modified fiddle):
td {
...
text-align: right !important;
overflow: hidden; /* for containg floats */
}
td:before {
...
float:left;
}
Your JSFiddle is different then the image in your post.
Refering to your image, you could make it like this, using colspan to set your colums width:
<table>
<colgroup>
<col width="100px" />
<col width="200px" />
</colgroup>
<tr>
<td>title</td>
<td>value</td>
</tr>
<tr>
<td>title</td>
<td>value</td>
</tr>
...
</table>
As for your JSFiddle, you have 5 <th>'s and 6 <td>'s. Which is possible, however, one of the <th> should cover two <td>'s, you can make that by using this:
<th colspan="2">Title</th>

Resources