Forum Moderators: not2easy

Message Too Old, No Replies

Challenging Problem! Table with Truncated Column

Complications with table-layout:fixed property

         

justinhambleton

3:11 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



In summary, I am attempting to truncate data in a column using the overflow: hidden property on the cell holding this data. In order to do so, the table needs the table-layout property set to fixed. I want to maximize the available space by not specifying widths on all of the other columns and setting the truncated column width to 100%. This should force all of the remaining columns to condense to the absolute minimum width per their enclosed data. This is rather complicated to explain, but the bottom line is when the table-layout:fixed property is specified and none of the other columns have a width assigned, the column set to 100% shoves all of the remaining columns closed, so you can't see the data. I have enclosed sample code to illustrate my problem. Remove the width="100%" property and see what I mean. Remove table-layout:fixed from the table style and you get an almost perfect scenario, less the truncation! Any solutions would be highly appreciated.

<style type="text/css">
table.datatable {margin: 0; padding: 0; border-collapse: collapse; table-layout: fixed; font-family: tahoma,arial,verdana,sans-serif; clear: both; text-align: left;}
.datatable thead th {padding: 4px 7px; background: #E6E6E6; border-width: 1px 1px 1px 0; border-style: solid; border-top-color: #FFF; border-bottom-color: #C1C1C1; border-right-color: #C1C1C1; font-size: 10px; font-weight: bold; white-space: nowrap; cursor: pointer; cursor: hand;}
.datatable tbody tr {cursor: pointer; cursor: hand;}
.datatable tbody td {padding: 4px; border: 1px solid #E6E6E6; font-size: 10px; vertical-align:top; white-space: nowrap;}
.datatable tbody td.trunc {overflow: hidden; text-overflow: ellipsis;}
</style>
<table class="datatable" cellpadding="0" cellspacing="0" border="0" width="100%">
<thead>
<tr>
<th width="30"></th>
<th width="20"></th>
<th width="100%">Header</th>
<th align="right">Header</th>
<th align="right">Header</th>
<th align="right">Header</th>
<th align="right">Header</th>
<th align="right">Header</th>
<th align="right">Header</th>
<th align="right">Header</th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">1.</td>
<td></td>
<td class="trunc">This column contains potentially long strings that I would like to truncate.</td>
<td align="right">100,000</td>
<td align="right">100,000</td>
<td align="right">100,000</td>
<td align="right">100,000</td>
<td align="right">100,000</td>
<td align="right">100,000</td>
<td align="right">100,000</td>
</tr>
</tbody>
</table>

SuzyUK

3:15 pm on Aug 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi justinhambleton and Welcome to the Forums!

I'm sorry this has gone unanswered for so long.. I only spotted it and it is indeed a challenge ;)

anyhow.. I think I have a solution? but it involves some hacking for IE, do you mind and/or do you need it to work in all browsers or is it for intranet..

Here's how I see it, you cannot use table-layout: fixed; if you want those last 7 columns to have "auto" width.. but then that invokes IE's auto stretch facilty for the column you want to truncate and you can't get it to crop.

anyhow let me know about which browsers and.or you still need a solution ~ I'm just trying something else too..

Suzy

SuzyUK

6:08 pm on Aug 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok here's one possible solution I found.. the long way about I admit!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /></meta>
<title>Untitled</title>
<style type="text/css" media="screen">
table.datatable {
margin: 0; padding: 0;
border-collapse: collapse;
/* table-layout: fixed; */ /* remove this */
width: 100%;
font-family: tahoma,arial,verdana,sans-serif;
clear: both;
text-align: right; /* change this to right as most of table is right aligned */
}

.datatable thead th {
padding: 4px 7px;
background: #E6E6E6;
border-width: 1px 1px 1px 0;
border-style: solid;
border-top-color: #FFF;
border-bottom-color: #C1C1C1;
border-right-color: #C1C1C1;
font-size: 10px;
font-weight: bold;
white-space: nowrap;
cursor: pointer;
cursor: hand;
}

.datatable tbody tr {cursor: pointer; cursor: hand;}

.datatable tbody td {
padding: 4px; border: 1px solid #E6E6E6;
font-size: 10px;
vertical-align:top;
white-space: nowrap;
}

/* set only elements with the trunc class to align left either th or td */
.datatable .trunc {width: 100%; text-align: left;}

/* wrap the content in .trunc cells in a div and set the overflow properties on it instead see IE conditional below */
.datatable .trunc div {overflow: hidden; text-overflow: ellipsis; }
</style>

<!-- now this bit is for IE only, apparently it will register the width of the cell if position: absolute is used that along with the width: 100%; then makes the div size and overflow properly! -->
<!--[if IE]>
<style type="text/css">
.datatable .trunc div {position: absolute; width: 100%; }
</style>
<![endif]-->

</head>
<body>
<table class="datatable" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>

<!--
ugly way of forcing the first 2 cells to be the 30px and 20px respectively
I could get the others to do it the right way, but not IE!
you could use spacer gifs.. but remember the cells inbuilt padding for their width
-->

<th><div style="width: 16px;"><!-- spacer div inside th padding --></div></th>
<th><div style="width: 6px;"><!-- spacer div inside th padding --></div></th>

<--
add the trunc class to the header as well if you want to control it's left alignment easily
I removed the "td" specificity iin the CSS to cover both th and td
-->
<th class="trunc">Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.</td>
<td></td>
<!--
wrap the content inside these cells in a div, it's actually the div that's being truncated
-->
<td class="trunc"><div>This column contains potentially long strings that I would like to truncate. This column contains potentially long strings that I would like to truncate.</div></td>
<td>100,000</td>
<td>100,000</td>
<td>100,000</td>
<td>100,000</td>
<td>100,000</td>
<td>100,000,000</td>
<td>100,000</td>
</tr>
</tbody>
</table>
</body>
</html>

Hope the comments cover it all, let us know..

re: the spacers on the first 2 x cells, this is to force their column width to what you had specified in original post. I'm not sure if you need this, but I thought this was the easiest way..
I could get FF and Opera to honour the widths properly if I went the <colgroup> and <col> route but then it needed conditional HTML for IE and it still wouldn't force the first 2 columns.. so I figured a good ol' spacer was easier ;)

Suzy

justinhambleton

6:22 pm on Aug 9, 2005 (gmt 0)

10+ Year Member



At first glance this appears to work. You're a gem! I will spend some time to understand what you did, but so far so good. It also works in Firefox, which is a requirement. Thanks for taking the time to look into this. I'll keep you posted on the outcome.

justinhambleton

6:58 pm on Aug 9, 2005 (gmt 0)

10+ Year Member



SuzyUK, many thanks for your help with this. Funny thing is I was along the same track as you, but didn't get as far. The IE hack saved the day! This appears to be pretty solid so far.

SuzyUK

7:07 pm on Aug 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you're welcome! just glad if it helped

>>track
I've been down all sorts of <table> tracks today, fairly blew out the cobwebs :o

Suzy