How to Hide Table Row Overflow

How to hide Table Row Overflow?

Need to specify two attributes, table-layout:fixed on table and white-space:nowrap; on the cells. You also need to move the overflow:hidden; to the cells too

table { width:250px;table-layout:fixed; }
table tr { height:1em; }
td { overflow:hidden;white-space:nowrap; }

Here's a Demo . Tested in Firefox 3.5.3 and IE 7

How to hide table row overflow (take 2)

If you fear for unforeseen consequences of display:block for the TD elements, then I’d suggest that you instead wrap the content inside the TD inside a container element (DIV), and give that height:1em and overflow:hidden … works the same, but has no risk of table layout beeing messed up because of TD elements displayed as something else then table-cell.

td { height: 1.2em; overflow: hidden; } […] doesn't work for reasons that are still a mystery to me.

Not much of a mystery, look at the CSS spec for overflow:

“Applies to: block containers”

A way to have table-rows with height 0 and overflow hidden?

After about one week of searching for a solution, the answer is:

no, it's still not possible. At least, it's not possible in a reliable and versatile way. It's only possible in ways that somewhat limit elements or future actions.

If one doesn't strictly need css-tables (like me in this specific case), you can successfully mimic the same behaviour in 2 ways:

  • use real tables, apply table-layout:fixed and a width to the table (doesn't matter the unit, can be percentage, for ex.). Than just height:0/oveflow:hidden as usual.

  • use flexbox. It's the css construct that, with the right rules applied, can better approximate the table behaviour.

Hope it helps

How to hide table row?

You could try modifying the editUser() and deleteUser() Javascript methods to accept a userId parameter. It would probably clean up some of the code as well on the JS side. Just a suggestion though. That snippet then might look something like this:

<form method="post" id="editForm"> 
<table class="table">
<tbody>
<tr>
<th scope="col">FULL NAME</th>
<th scope="col">USER NAME</th>
<th scope="col">ACTIONS</th>
</tr>

<?php $a = 0?>

<?php while ($row = mysqli_fetch_array($res)) {

echo "<tr id='".$a++."'>
<td class='row-data'>".$row['full_name']."</td>
<td class='row-data'>".$row['user_name']."</td>
<td><input type='button'
value='EDIT'
onclick='editUser(".$row['user_id'].")'' /></td>
<td><input type='button'
value='DELETE'
onclick='deleteUser(".$row['user_id'].")' /></td>
</tr>

</tbody>";
}; ?>

Otherwise you could use Ken's solution which would probably work with your existing code.

Why does overflow:hidden not work in a td ?

Here is the same problem.

You need to set table-layout:fixed and a suitable width on the table element, as well as overflow:hidden and white-space: nowrap on the table cells.



Examples

Fixed width columns

The width of the table has to be the same (or smaller) than the fixed width cell(s).

With one fixed width column:

* {
box-sizing: border-box;
}
table {
table-layout: fixed;
border-collapse: collapse;
width: 100%;
max-width: 100px;
}
td {
background: #F00;
padding: 20px;
overflow: hidden;
white-space: nowrap;
width: 100px;
border: solid 1px #000;
}
<table>
<tbody>
<tr>
<td>
This_is_a_terrible_example_of_thinking_outside_the_box.
</td>
</tr>
<tr>
<td>
This_is_a_terrible_example_of_thinking_outside_the_box.
</td>
</tr>
</tbody>
</table>

Show/Hide specific rows in table when link is clicked

Issues in your code

  1. You need to identify your table rows by category.

    • Using id to assign a category to multiple rows is wrong (Duplicate ID values is invalid HTML).
    • You can use class, but personally I prefer to attributes since that value is meant to use within JS and not styling.
  2. The default behavior of anchors is to redirect, refresh (or move the scrollbar), to make it short this isn't the element you need to use. I will replace it with a button.

A solution

// Selecting all the filters (buttons)
document.querySelectorAll('[catFilter]').forEach((el)=>{
//console.log(el);

// Listenning to clicks on the filters
el.addEventListener('click', (ev)=>{

// Selecting all the table rows when the click happens
// This will happen everytime you click!
document.querySelectorAll('table tr').forEach((row)=>{
//console.log(row);

if(ev.target.value === "*"){
// Show all
row.classList.remove('hidden');
}else if(row.hasAttribute(ev.target.value)){
// Make sure that the filtered rows are shown
row.classList.remove('hidden');
}else{
// Hide everything else
row.classList.add('hidden');
}
})
})
})
.hidden {
display: none;
}
<button value="cat1" catFilter>cat1</button>
<button value="cat2" catFilter>cat2</button>
<button value="*" catFilter>All categories</button>

<table border="1">
<tr cat1>
<td>some info</td>
</tr>
<tr cat2>
<td>blah blah</td>
</tr>
<tr cat1>
<td>more blah</td>
</tr>
</table>

how to hide specific table row with jquery?

This could easily be done with the help of a button in each row of the table (I've created a simple table of my own because yours wasn't reproducible). When you click the button the JQuery gets activated and finds the closest tr (table row) and hides it.

$(document).on('click', '.hide_row', function() {
$(this).closest('tr').hide()
})
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}

.hide_row {
width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
<th></th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
<td><button type="button" class="hide_row">Hide</button></td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
<td><button type="button" class="hide_row">Hide</button></td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
<td><button type="button" class="hide_row">Hide</button></td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
<td><button type="button" class="hide_row">Hide</button></td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
<td><button type="button" class="hide_row">Hide</button></td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
<td><button type="button" class="hide_row">Hide</button></td>
</tr>
</table>

Hide table row based on table cell pure Javascript

From what I can see, you want to filter based on department and you're trying to display the data based on the selected department.

Here what you need to do is keep your data stored in a variable and render the table based on another variable which changes when the filter information is selected.

If you're going for a simple approach then use a file(json, csv, text, xlsx) of you choice. Write a function to read the file and save the info in "var data". Everytime while submitting a form, add it to your file by writing another write function. Also, call the read function after the write is done so that "var data" has the updated data with the newly added information

If you're using csv file the jQuery-CSV library has a function called $.csv.toObjects(csv) that does the mapping automatically.

File is save in the below format
heading1,heading2,heading3,heading4,heading5
value1_1,value2_1,value3_1,value4_1,value5_1
value1_2,value2_2,value3_2,value4_2,value5_2

To read the file use this
var data = $.csv.toObjects(csv):

The output would look like the data below in the "var data"
For example:-

var data = [
{Student_Id : 123
First_Name: John
Last_Name: doe
...
Department: ABC
...}
{Student_Id :3456
First_Name:Jane
Last_Name: Doe
.....
Department:DEF
....}
]

Use another variable for populating the table data say "studentTableData"

Now in default condition when no filter is applied do

var studentTableData = data

When a filter is selected instead of hiding the table rows,filter the data of "studentTableData" based on the selected department and render the table again(in simpler words replace the table)

var department_selected = "ABC"

var studentTableData = data.filter(function(el){
return el.Department == department_selected
})
//var studentTableData will now have an array of data where department name matches the selected name


Related Topics



Leave a reply



Submit