Avatar billede newbie_dk Nybegynder
29. januar 2007 - 15:12 Der er 9 kommentarer

Find højde og bredde af tabel, hvordan?

Jeg har brug for at finde ud af hvor høj en tabel er... Men jeg snakker ikke om den højde som er specificeret af evt. style tags.

Jeg har brug for den højde som tabellen får når der er indhold i.

What to do?
Avatar billede mcgoat Nybegynder
29. januar 2007 - 15:27 #1
Avatar billede newbie_dk Nybegynder
29. januar 2007 - 15:32 #2
Det koster penge at få den løsning =)

Ville gerne slippe for at blive medlem der =)
Avatar billede mcgoat Nybegynder
29. januar 2007 - 15:54 #3
Du skal skam ikke være medlem. bare scroll helt nederst på siden, så står koden der
Avatar billede newbie_dk Nybegynder
29. januar 2007 - 15:56 #4
Så må du meget gerne cp den her ind.. For jeg kan ikke se nogen kode...
Avatar billede mcgoat Nybegynder
29. januar 2007 - 15:57 #5
o.k.
I did some testing:

I must say that using clientHeight as archrajan sugested seems like the basis to the solution.

what you you should exactly do depends on the structure of your table.
suppose it is a single td and formattign is always the same you can just premeasure and
see what client hights you get per No. of text rows.

If you could tell us more about the table you are try to cound the rows in then I could suggest how to handle it,
basicly if its a row by row table
I would not measure the clientHeight for the TABLE but for the TD's of each row
this way I would not get marin and border measures effecting the rowcount.


below is my testground :) maybe it will give you additional ideas.

Too bad that the only elements that supports createTextRange are:
BODY, BUTTON, INPUT type=button, INPUT type=hidden, INPUT type=password, INPUT type=reset, INPUT type=submit, INPUT type=text, TEXTAREA

otherwise you could use .getClientRects().length on it (which was my original thought).
as you can see below I tried to use a textarea to mimic the div / TD
until I realized They have a different for alltogether :(

SnowFlake



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>

<script language=javascript>

function NoOfTextLinesInATD(obj){

   
   
    r=obj.getBoundingClientRect();
    bdr.style.left=r.left;
    bdr.style.top=r.top;
    bdr.style.width=r.right-r.left;
    bdr.style.height=r.bottom-r.top;

alert(obj.clientHeight + '    ' + (r.bottom-r.top));

    ta.style.left=r.left + 150;
    ta.style.top=r.top ;
    ta.style.width=r.right-r.left+25;
    ta.style.height=r.bottom-r.top;
    ta.value = obj.innerText;
    ta.style.font.family=obj.style.font.family;

    ta.style.overflow="visible";
   
    oTextRange = ta.createTextRange();             
 
    // Get bounding rect of the range
    oRcts = oTextRange.getClientRects();
    alert(oRcts.length);

}
function go(){
    NoOfTextLinesInATD(document.getElementById('div1'));
    NoOfTextLinesInATD(document.getElementById('td1'));
    NoOfTextLinesInATD(document.getElementById('td2'));
}
</script>
</head>
<body onload="go()">
<div id=bdr style="position:absolute;border:1px solid red;"></div>

<textarea id=ta style="position:absolute;"></textarea>

<Div style="width:100px;" id="div1">The quick brown fox jumps<br>over the lazy dogs</td></tr>
</Div>
<br>

<table style="width:100px;"><tr><td id="td1">The quick brown fox jumps over the lazy dogs</td></tr>
</table>
<br>

<table style="width:150px;" ID="Table1"><tr><td id="td2">The quick brown fox jumps over the lazy dogs</td></tr>
</table>


</body>
</html>
Avatar billede mcgoat Nybegynder
29. januar 2007 - 15:57 #6
document.getElementById('tableID').clientHeight  shud work in IE
Avatar billede fixxxer Nybegynder
29. januar 2007 - 15:58 #7
document.getElementById('foo').offsetHeight

document.getElementById('foo').offsetWidth
Avatar billede olebole Juniormester
29. januar 2007 - 16:06 #8
<ole>

Når tabellen er tegnet i browseren, kan du aftaste dens dimensioner med:

var myTable = document.getElementById("TABEL_ID");
var w = myTable.offsetWidth;
var h = myTable.offsetHeight;
alert(w + "\n" + h)

/mvh
</bole>
Avatar billede olebole Juniormester
29. januar 2007 - 16:07 #9
Hehe ... man skal huske at opdatere sin browser, når man henter kaffe, m.m.  ;oD
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester