Précédent   Forum des professionnels en informatique > Webmasters - Développement Web > JavaScript > Bibliothèques & Frameworks > jQuery
jQuery Forum d'entraide sur le framework jQuery. Avant de poster : Tutoriels jQuery, FAQ jQuery, Tous les tutoriels JavaScript, Toutes les FAQ JavaScript
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 05/01/2012, 09h12   #1
Membre confirmé
 
Inscription : septembre 2009
Messages : 749
Détails du profil
Informations forums :
Inscription : septembre 2009
Messages : 749
Points : 223
Points : 223
Par défaut Dimensionner largeur de colonne

Bonjour,

voici mon code html :
Code html :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<div class="fix_table">
	<div class="fix_table_header">
		<table>
			<thead>
				<tr>
					<th>mon header 1</th>
					<th>mon header 2</th>
					<th>mon header 3</th>
					<th>mon header 4</th>
				</tr>
			</thead>
			<tbody style="display:none"><tr><td></td></tr></tbody> <!-- pour etre valide W3C -->
		</table>
	</div>
 
	<div class="fix_table_body" style="height:10em;overflow-Y:auto;">
		<table>				
			<tbody>
				<tr>
					<td>col 1</td>
					<td>col 2</td>
					<td>col 3</td>
					<td>col 4</td>
				</tr>
			</tbody>
		</table>
	</div>
</div>

Je cherche à faire un tableau avec une entete fixe et le corps scrollable verticalement : c'est pourquoi, on peut voir que mon tableau est divisé en deux tableaux.

Ce que je voudrais c'est dimensionner la largeur des colonnes du second tableau en fonctions de la largeur des colonnes du premier tableau.

Comment feriez-vous ?

Aussi, comment faites-vous pour récupérer le nombre de colonnes et de lignes d'un tableau ?

Merci d'avance,
boboss123 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/01/2012, 09h49   #2
Membre Expert
 
Avatar de rotrevrep
 
Homme yannick inizan
Secrétaire d'état à la procrastination
Inscription : février 2011
Messages : 304
Détails du profil
Informations personnelles :
Nom : Homme yannick inizan
Âge : 25
Localisation : France, Cher (Centre)

Informations professionnelles :
Activité : Secrétaire d'état à la procrastination
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : février 2011
Messages : 304
Points : 1 118
Points : 1 118
Envoyer un message via MSN à rotrevrep Envoyer un message via Skype™ à rotrevrep
si ton nombre de colonnes est limité, je te conseille d'attribuer la même classe à chaque colonne.
__________________
(marquer un post résolu si vous êtes satisfait de la réponse )
le chat caramail de retour ? :/ http://www.tchats.net/beta.php
les projets web en cours sont sur : https://github.com/rotrevrep
rotrevrep est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/01/2012, 16h56   #3
Membre confirmé
 
Inscription : septembre 2009
Messages : 749
Détails du profil
Informations forums :
Inscription : septembre 2009
Messages : 749
Points : 223
Points : 223
Citation:
Envoyé par rotrevrep Voir le message
si ton nombre de colonnes est limité, je te conseille d'attribuer la même classe à chaque colonne.
Je ne connais pas à l'avance le nombre de colonne donc ça ne sera pas possible (à moins que tu connaisses une méthode pour créer des class à la volée).

Actuellement je fais ça pour configurer mon tableau mais ça ne fonctionne correctement uniquement sous firefox :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
 
// ******************************************************
// creation des tableaux avec scrollbar verticale
function initFixedTable(){
 
	var divDivTabList = $("div.fix_table");
 
	for (var i=0; i<divDivTabList.length; i++){ // parcours de tous les "div.fix_table"
 
		var divTabList = $(divDivTabList[i]).children("div"); // récupération de de tous les "div.fix_table > div"
		var tabHeader = $(divTabList[0]).children("table")[0]; // récupération de de tous les "div.fix_table > div.fix_table_header > table"
		var tabBody = $(divTabList[1]).children("table")[0]; // récupération de de tous les "div.fix_table > div.fix_table_body > table"
 
		var tabHeaderLines = $(tabHeader).find("tr");
		var tabBodyLines = $(tabBody).find("tr");
 
		//alert(tabHeaderLines.length); // nombre de ligne		
		//alert($(tabHeaderLines[0]).children().length); // nombre de colonnes de la première ligne (fonctionne pour thead et tbody)
 
 
		//*
		var scrollBarWidth = getScrollerWidth();
 
		var widthLine = $(tabHeaderLines[0]).width();
		var widthDiv = $(divTabList[0]).width();
		$(divTabList[1]).width(widthDiv + scrollBarWidth);
 
		//$(divTabList[0]).width(widthDiv);
 
		$(tabBodyLines[0]).width(widthLine);
 
		var headColumnCount = $(tabHeaderLines[0]).children().length; // nombre de colonnes de la première ligne (fonctionne pour thead et tbody)
		for(j=0; j<headColumnCount; j++){
			var columnTmp = $(tabHeaderLines[0]).children()[j];
			var widthColumn = $(columnTmp).width(); //alert(widthColumn);
			$($(tabBodyLines[0]).children()[j]).width(widthColumn);
		}
		// => TODO : on fait quoi si le tableau est vide ?
		// */
	}
}
 
function getScrollerWidth() {
    var scr = null;
    var inn = null;
    var wNoScroll = 0;
    var wScroll = 0;
 
    // Outer scrolling div
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '-1000px';
    scr.style.left = '-1000px';
    scr.style.width = '100px';
    scr.style.height = '50px';
    // Start with no scrollbar
    scr.style.overflow = 'hidden';
 
    // Inner content div
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';
 
    // Put the inner div in the scrolling div
    scr.appendChild(inn);
    // Append the scrolling div to the doc
    document.body.appendChild(scr);
 
    // Width of the inner div sans scrollbar
    wNoScroll = inn.offsetWidth;
    // Add the scrollbar
    scr.style.overflow = 'auto';
    // Width of the inner div width scrollbar
    wScroll = inn.offsetWidth;
 
    // Remove the scrolling div from the doc
    document.body.removeChild(
        document.body.lastChild);
 
    // Pixel width of the scroller
    return (wNoScroll - wScroll);
}
boboss123 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/01/2012, 17h12   #4
Membre confirmé
 
Inscription : septembre 2009
Messages : 749
Détails du profil
Informations forums :
Inscription : septembre 2009
Messages : 749
Points : 223
Points : 223
code complet pour debuger :
Code html :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
	<title>test</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 
	<style type='text/css'>
body{
min-width: 650px;
margin:0;
padding:0;
border:none;
font-family: Verdana, Arial, sans-serif;
color:black;
font-size:0.8em;
background-color:#ebecf6;
height:100%; /* pour layout ie6 */
}
 
div {
font-size:1em;
display:block;
margin:0;
padding:0;
}
 
a, h1, h2, h3, h4, h5, p{
font-size:1em;
margin:0.3em 0;
padding:0;
border:none
}
 
.spacer { 
clear: both; 
} 
 
.spacer hr{     
  display: none; 
}
 
 
div.blocTab{
clear: both;
float:left;
margin: 5px 0px 10px 5px ;
background-color:red;
color:white;
font-size:1em;
}
 
div.blocTab h3{
font-weight:bold;
font-size:1em;
margin:0 4em 0 0;
padding:3px 5px 3px 20px;
background-position : -171px -65px;
}
 
div.blocTab table{
padding:0;
margin:2px;
font-size:0.9em;
border-collapse:collapse;
border:none;
background-color:red;
color:#00039e}
 
div.blocTab th{
font-weight:bold;
background-color:blue;
color:white;
font-weight:bold;
}
 
div.blocTab th a{
display:block;
float:right;
 
height:10px;
width:10px;
padding:0;
margin:2px 0 0 2px;
 
background-position : -86px -65px;
}
 
div.blocTab th, td{
border:2px solid white;
text-align:center;
padding:2px 10px;
margin:0}
 
 
div.blocTab tr{
background-color:green;
margin:0;
padding:0;
}
div.blocTab tr.tr_imp{background-color:#dbe5f6;}
div.blocTab select{
margin:0;
padding:0;
font-size:1.1em;
}
div.blocTab input{
margin:0 0.5em;
padding:0.1em 0.2em;
font-size:1.1em;
}
 
 
div.blocTab div.tabPageSelect{
padding:2px;
font-weight:bold;
text-align:center;
/*border-bottom: 2px solid white;*/
/*margin-bottom: 4px;*/
}
 
 
	</style>
 
	<script type="text/javascript" src="jquery-1.4.2.js"></script>
 
	<script type="text/javascript"> 
//<![CDATA[ 
 
 
// code executé au demarrage
$(document).ready(function(){	// Traitement à effectuer lorsque le DOM est prêt
//$(window).load(function(){		// Traitement à effectuer lorsque tous les elements de la page sont chargés
 
	initFixedTable();
 
});   
 
 
// ******************************************************
// creation des tableaux avec scrollbar verticale
function initFixedTable(){
 
	var divDivTabList = $("div.fix_table");
 
	for (var i=0; i<divDivTabList.length; i++){ // parcours de tous les "div.fix_table"
 
		var divTabList = $(divDivTabList[i]).children("div"); // récupération de de tous les "div.fix_table > div"
		var tabHeader = $(divTabList[0]).children("table")[0]; // récupération de de tous les "div.fix_table > div.fix_table_header > table"
		var tabBody = $(divTabList[1]).children("table")[0]; // récupération de de tous les "div.fix_table > div.fix_table_body > table"
 
		var tabHeaderLines = $(tabHeader).find("tr");
		var tabBodyLines = $(tabBody).find("tr");
 
		//alert(tabHeaderLines.length); // nombre de ligne		
		//alert($(tabHeaderLines[0]).children().length); // nombre de colonnes de la première ligne (fonctionne pour thead et tbody)
 
 
		//*
		var scrollBarWidth = getScrollerWidth();
 
		var widthLine = $(tabHeaderLines[0]).width();
		var widthDiv = $(divTabList[0]).width();
		$(divTabList[1]).width(widthDiv + scrollBarWidth);
 
		//$(divTabList[0]).width(widthDiv);
 
		$(tabBodyLines[0]).width(widthLine);
 
		var headColumnCount = $(tabHeaderLines[0]).children().length; // nombre de colonnes de la première ligne (fonctionne pour thead et tbody)
		for(j=0; j<headColumnCount; j++){
			var columnTmp = $(tabHeaderLines[0]).children()[j];
			var widthColumn = $(columnTmp).width(); //alert(widthColumn);
			$($(tabBodyLines[0]).children()[j]).width(widthColumn);
		}
		// => TODO : on fait quoi si le tableau est vide ?
		// */
	}
}
 
function getScrollerWidth() {
    var scr = null;
    var inn = null;
    var wNoScroll = 0;
    var wScroll = 0;
 
    // Outer scrolling div
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '-1000px';
    scr.style.left = '-1000px';
    scr.style.width = '100px';
    scr.style.height = '50px';
    // Start with no scrollbar
    scr.style.overflow = 'hidden';
 
    // Inner content div
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';
 
    // Put the inner div in the scrolling div
    scr.appendChild(inn);
    // Append the scrolling div to the doc
    document.body.appendChild(scr);
 
    // Width of the inner div sans scrollbar
    wNoScroll = inn.offsetWidth;
    // Add the scrollbar
    scr.style.overflow = 'auto';
    // Width of the inner div width scrollbar
    wScroll = inn.offsetWidth;
 
    // Remove the scrolling div from the doc
    document.body.removeChild(
        document.body.lastChild);
 
    // Pixel width of the scroller
    return (wNoScroll - wScroll);
}
 
 
//]]>
	</script>
 
</head>
 
<body style="min-width: 800px;">
 
	<div id="corps">
		<noscript><h1>Erreur : Javascript est désactivé</h1></noscript>
 
			<div class="blocTab fix_table">
				<h3>Mon Titre</h3>
				<div class="fix_table_header">
					<table>
						<thead>
							<tr>
								<th>ma colonne 1</th>
								<th>ma colonne 2</th>
								<th>ma colonne 3</th>
								<th>ma colonne 4</th>
							</tr>
						</thead>
						<tbody style="display:none"><tr><td></td></tr></tbody> <!-- pour etre valide W3C -->
					</table>
				</div>
 
				<div class="fix_table_body" style="height:10em;overflow-Y:auto;">
					<table>				
						<tbody>
							<tr>
								<td>1</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>2</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>3</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>4</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
 
							<tr>
								<td>5</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>1</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>2</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>3</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>4</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
 
							<tr>
								<td>5</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
						</tbody>
					</table>
				</div>
			</div>
			<div class="spacer"></div>
 
		</div>
 
		<div class="spacer"><hr/></div>
	</div>
 
</body>
</html>

=> penser à mettre le bon chemin pour la lib Jquery
boboss123 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/01/2012, 19h20   #5
Rédacteur/Modérateur
 
Avatar de Macmillenium
 
Homme
Inscription : mars 2008
Messages : 2 290
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 26
Localisation : France, Sarthe (Pays de la Loire)

Informations professionnelles :
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : mars 2008
Messages : 2 290
Points : 3 208
Points : 3 208
J'ai pas le courage de lire ton code...
Essaye ceci:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
 
var cells1 = $('.fix_table_header').find('td, th'),
    cells2 = $('.fix_table_body').find('td, th'),
    cellsWidthArray = [];
 
cells1.each(function(i) {
  cellsWidthArray[i] = $(this).width();
});
 
cells2.each(function(i) {
  $(this).width(cellsWidthArray[i])
});
__________________
Je ne réponds pas aux questions techniques par MP.
Macmillenium est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 06/01/2012, 09h02   #6
Membre confirmé
 
Inscription : septembre 2009
Messages : 749
Détails du profil
Informations forums :
Inscription : septembre 2009
Messages : 749
Points : 223
Points : 223
Non, ça ne fonctionne pas : Lorsque la scrollbar s'active, les largeurs des cellules du tableau contenant les données est automatiquement redimensionné
boboss123 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/01/2012, 09h30   #7
Membre confirmé
 
Inscription : décembre 2009
Messages : 249
Détails du profil
Informations forums :
Inscription : décembre 2009
Messages : 249
Points : 219
Points : 219
j'avais fait une petite fonction pour ca, tu passais la table en argument et elle te fixait les headers (en créant un div avec les headers et un div avec le contenu). Après y'avait pas mal de truc a faire attention :
- les colspans faisait souvent des décalages de pixels
- les cellpadding, cellspacing et border
- le overflow, la barre à droite est contenu dans l'attribut "width" sur firefox et pas sur IE (ou le contraire ^^)
- modification de la taille de la page, ca entraine souvent une modification de la taille de la table
- et y'avait encore d'autre truc ....

C'est pas si simple que ca a faire..je vais essayer de la refaire aujourd'hui je me fais chi.. au boulot, je te filerais le code quand je réussis si tu veux
ticroch est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/01/2012, 10h26   #8
Rédacteur
 
Avatar de jreaux62
 
Homme Jérôme Réaux
Webdesigner
Inscription : août 2008
Messages : 2 994
Détails du profil
Informations personnelles :
Nom : Homme Jérôme Réaux
Âge : 45
Localisation : France, Pas de Calais (Nord Pas de Calais)

Informations professionnelles :
Activité : Webdesigner
Secteur : Arts - Culture

Informations forums :
Inscription : août 2008
Messages : 2 994
Points : 5 787
Points : 5 787
Envoyer un message via Skype™ à jreaux62
Bonjour,
j'ai vu ça en passant :
-> Pure CSS Scrollable Table with Fixed Header
-> (Almost) Pure CSS Scrollable Table with Fixed Header
(non testé)
__________________
"Ce qui se conçoit bien s'énonce clairement - Et les mots pour le dire arrivent aisément."
Nicolas Boileau-Despréaux, Homme de lettres français (1636-1711), principal théoricien de l'esthétique classique.
Site perso Mes tutos DVP : Gestion-Affichage de Nouvelles - Affichage en tableau HTML - Fonctions de redimensionnement d'images
jreaux62 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/01/2012, 10h30   #9
Membre confirmé
 
Inscription : septembre 2009
Messages : 749
Détails du profil
Informations forums :
Inscription : septembre 2009
Messages : 749
Points : 223
Points : 223
Citation:
Envoyé par jreaux62 Voir le message
Bonjour,
j'ai vu ça en passant : Pure CSS Scrollable Table with Fixed Header
(non testé)
Ne fonctionne pas sur IE7 et 8 (9 pas testé)
boboss123 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/01/2012, 10h38   #10
Rédacteur
 
Avatar de jreaux62
 
Homme Jérôme Réaux
Webdesigner
Inscription : août 2008
Messages : 2 994
Détails du profil
Informations personnelles :
Nom : Homme Jérôme Réaux
Âge : 45
Localisation : France, Pas de Calais (Nord Pas de Calais)

Informations professionnelles :
Activité : Webdesigner
Secteur : Arts - Culture

Informations forums :
Inscription : août 2008
Messages : 2 994
Points : 5 787
Points : 5 787
Envoyer un message via Skype™ à jreaux62
Exact.
Il faut peut-être envisager une version alternative pour I.E.

Ou boycotter I.E., qui est le seul à nous faire autant ch.. en nous obligeant à coder 3 fois plus, rien que pour ses versions pourries...
__________________
"Ce qui se conçoit bien s'énonce clairement - Et les mots pour le dire arrivent aisément."
Nicolas Boileau-Despréaux, Homme de lettres français (1636-1711), principal théoricien de l'esthétique classique.
Site perso Mes tutos DVP : Gestion-Affichage de Nouvelles - Affichage en tableau HTML - Fonctions de redimensionnement d'images
jreaux62 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/01/2012, 12h18   #11
Membre confirmé
 
Inscription : septembre 2009
Messages : 749
Détails du profil
Informations forums :
Inscription : septembre 2009
Messages : 749
Points : 223
Points : 223
J'ai trouvé une solution qui marche (sauf sur IE7 => mais l'affichage n'est pas trop degueu) :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
	<title>test</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 
	<style type='text/css'>
body{
min-width: 650px;
margin:0;
padding:0;
border:none;
font-family: Verdana, Arial, sans-serif;
color:black;
background-color:#ebecf6;
height:100%; /* pour layout ie6 */
}
 
display:block;
margin:0;
padding:0;
}
 
a, h1, h2, h3, h4, h5, p{
margin:0.3em 0;
padding:0;
border:none
}
 
.spacer { 
clear: both; 
} 
 
.spacer hr{     
  display: none; 
}
 
 
div.blocTab{
clear: both;
float:left;
 
margin: 5px 0px 10px 5px ;
background-color:red;
color:white;
}
 
div.blocTab h3{
font-weight:bold;
margin:0 4em 0 0;
padding:3px 5px 3px 20px;
background-position : -171px -65px;
}
 
div.blocTab table{
padding:0;
margin:0;
 
border-collapse:collapse;
border:none;
background-color:red;
color:#00039e}
 
div.blocTab th{
font-weight:bold;
background-color:blue;
color:white;
font-weight:bold;
}
 
div.blocTab th a{
display:block;
float:right;
 
height:10px;
width:10px;
padding:0;
margin:2px 0 0 2px;
 
background-position : -86px -65px;
}
 
div.blocTab th, td{
border:2px solid white;
text-align:center;
padding:2px 10px;
margin:0}
 
 
div.blocTab tr{
background-color:green;
margin:0;
padding:0;
}
div.blocTab tr.tr_imp{background-color:#dbe5f6;}
div.blocTab select{
margin:0;
padding: 0 2px 2px 2px;
}
 
 
div.blocTab div.fix_table_body{
height:10em;
overflow-y:scroll;
/*overflow-y:auto;*/
overflow-x:visible;
}
 
div.blocTab div.fix_table_body  table{
/*width:100%;*/
}
 
	</style>
 
	<script type="text/javascript" src="jquery-1.4.2.js"></script>
 
	<script type="text/javascript"> 
//<![CDATA[ 
 
 
// code executé au demarrage
$(document).ready(function(){	// Traitement à effectuer lorsque le DOM est prêt
//$(window).load(function(){		// Traitement à effectuer lorsque tous les elements de la page sont chargés
 
	//initFixedTable();
 
});   
 
// ******************************************************
// creation des tableaux avec scrollbar verticale
var widthColumn = []; // sert a redefinir la largeur des cases du tableau quand on le met a jour
function initFixedTable(){
 
	var divDivTabList = $("div.fix_table");
 
	for (var i=0; i<divDivTabList.length; i++){ // parcours de tous les "div.fix_table"
 
		var divTabList = $(divDivTabList[i]).children("div"); // récupération de de tous les "div.fix_table > div"
		var tabHeader = $(divTabList[0]).children("table")[0]; // récupération de de tous les "div.fix_table > div.fix_table_header > table"
		var tabBody = $(divTabList[1]).children("table")[0]; // récupération de de tous les "div.fix_table > div.fix_table_body > table"
 
		var tabHeaderLines = $(tabHeader).find("tr");
		var tabBodyLines = $(tabBody).find("tr");
 
		//alert(tabHeaderLines.length); // nombre de ligne		
		//alert($(tabHeaderLines[0]).children().length); // nombre de colonnes de la première ligne (fonctionne pour thead et tbody)
 
 
		//var scrollBarWidth = getScrollerWidth();
 
		//var widthLine = $(tabHeaderLines[0]).width();
		//var widthDiv = $(divTabList[0]).width();
		//$(divTabList[1]).width(widthDiv + scrollBarWidth);
 
		//$(divTabList[0]).width(widthDiv);
 
		//$(tabBodyLines[0]).width(widthLine);
 
		//*
 
		//*
		var widthTabBody = $(tabBody).width();
		//alert("get TabBody: " + widthTabBody);
		$(tabHeader).width(widthTabBody); // +2 sur firefox
		var widthTabHead = $(tabHeader).width();
		//alert("get TabHead: " + widthTabHead);
 
		// le get width et le set width ne renvoit pas la meme chose
		// => on corrige l'erreur
		var diffWidth = widthTabBody - widthTabHead;
		widthTabHead = widthTabBody + diffWidth;
 
		$(tabHeader).width(widthTabHead);
 
		//if($(tabHeader).width() != $(tabBody).width()){
		//	alert("get TabHead: " + $(tabHeader).width());
		//	alert("get tabBody: " + $(tabBody).width());		
		//} else {
		//	alert("TabHead/tabBody width: OK");
		//}
 
		$(tabBody).width(widthTabHead); // on fige aussi le body pour éviter tout problème si l'unité n'est pas de type absolue (ex : em)
 
 
		//*		
		// ************************************************
		var headColumnCount = $(tabHeaderLines[0]).children().length; // nombre de colonnes de la première ligne (fonctionne pour thead et tbody)
		var diffWidthColumn = [];
 
		for(j=0; j<headColumnCount; j++){
			var columnTmp = $(tabHeaderLines[0]).children()[j];
			widthColumn[j] = $(columnTmp).width(); //alert(widthColumn);
		}
 
 
		//alert("set colonne width: " + widthColumn[0]);
		$($(tabBodyLines[0]).children()[0]).width(widthColumn[0]);
		//alert("get colonne width: " + $($(tabBodyLines[0]).children()[0]).width());
 
		diffWidthColumn = widthColumn[0] - $($(tabBodyLines[0]).children()[0]).width(); // calcul de la correction d'erreur
		//alert(diffWidthColumn);
 
		for(j=0; j<headColumnCount; j++){
		//for(j=0; j<1; j++){
			widthColumn[j] += diffWidthColumn;
			$($(tabBodyLines[0]).children()[j]).width(widthColumn[j]);
 
			// control d'erreur
			//if ($($(tabHeaderLines[0]).children()[j]).width() != $($(tabBodyLines[0]).children()[j]).width()){
			//	alert("Erreur0 largeur colonne: " + j);
			//} else {
			//	alert("largeur colonne OK: " + j);
			//}
		}
 
 
		// on fige aussi le head pour éviter tout problème si l'unité n'est pas de type absolue (ex : em)
		for(j=0; j<headColumnCount; j++){
			$($(tabHeaderLines[0]).children()[j]).width(widthColumn[j]);
		}
 
 
		// control d'erreur
		//for(j=0; j<headColumnCount; j++){
		//	if ($($(tabHeaderLines[0]).children()[j]).width() != $($(tabBodyLines[0]).children()[j]).width()){
		//		alert("Erreur largeur colonne: " + j);
		//	}
		//}
		// => TODO : on fait quoi si le tableau est vide ?
		// */
 
 
 
	}
}
 
 
 
//]]>
	</script>
 
</head>
 
<body style="min-width: 800px;">
 
	<div id="corps">
		<noscript><h1>Erreur : Javascript est désactivé</h1></noscript>
 
			<div class="blocTab fix_table" id="tableLogBloc" style="float:clear;width:600px;">
				<h3>Mon Titre</h3>
				<div class="fix_table_header">
					<table>
						<thead>
							<tr>
								<th>ma colonne 1</th>
								<th>ma colonne 2</th>
								<th>ma colonne 3</th>
								<th>ma colonne 4</th>
							</tr>
						</thead>
						<tbody style="display:none"><tr><td></td></tr></tbody> <!-- pour etre valide W3C -->
					</table>
				</div>
 
				<div class="fix_table_body" style="height:10em;">
					<table>
						<tbody>
							<tr>
								<td>1</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>2</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>3</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>4</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
 
							<tr>
								<td>5</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>1</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>2</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>3</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
							<tr>
								<td>4</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
 
							<tr>
								<td>5</td>
								<td></td>
								<td></td>
								<td></td>
							</tr>
 
						</tbody>
					</table>
				</div>
			</div>
			<div class="spacer"></div>
 
		</div>
 
		<div class="spacer"><hr/></div>
	</div>
 
</body>
</html>
Testé sous : Opera 10.52, Safari 3.2.2, Chrome 7.0.517.41, IE8, IE9
Testé aussi (avec IETester) sur : IE6, IE7(affichage incorrecte)


Le principe de fonctionnement est simple : je récupère la largeur du tableau qui est scrollable (ce tableau a un width: 100% => donc il prend toute la zone et la barre de scroll est affichée) puis je l'applique au tableau de l’entête. Ensuite je recupère la taille des colonnes de l’entête et je les applique au colonnes du tableau scrollable.

=> le problème avec IE7 c'est si width de la table scrollable = 100%, la table dépasse la zone en largeur (désactiver le js pour voir le problème) : vous avez compris le problème ?

Une solution pour résoudre le problème ?


-------------------------
aussi, il y a ce plug-in qui le gère : http://datatables.net/
=> mais je n'ai pas bien compris comment il fait...
=> je ne l'utilise pas car je suis limité en taille de fichier (système embarqué) : le plug-in est assez gros mais la partie de la gestion du scrollback à l'air assez petite
boboss123 est déconnecté   Envoyer un message privé Réponse avec citation 01
Vieux 06/01/2012, 15h00   #12
Rédacteur/Modérateur
 
Avatar de Macmillenium
 
Homme
Inscription : mars 2008
Messages : 2 290
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 26
Localisation : France, Sarthe (Pays de la Loire)

Informations professionnelles :
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : mars 2008
Messages : 2 290
Points : 3 208
Points : 3 208
Tu te compliques trop la vie.

Citation:
Envoyé par boboss123 Voir le message
Non, ça ne fonctionne pas : Lorsque la scrollbar s'active, les largeurs des cellules du tableau contenant les données est automatiquement redimensionné
Code :
$('.fix_table_header').find('table').width($('.fix_table_body').find('table').outerWidth())
outerWidth() tient compte du box model et inclut donc les border et padding.

Pour IE7 il affiche la scrollbar à l'intérieur du bloc, c'est un bug connu et documenté.
__________________
Je ne réponds pas aux questions techniques par MP.
Macmillenium est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/01/2012, 08h46   #13
Membre confirmé
 
Inscription : septembre 2009
Messages : 749
Détails du profil
Informations forums :
Inscription : septembre 2009
Messages : 749
Points : 223
Points : 223
merci pour l'information, c'est toujours bon à savoir même si ça ne résoud pas vraiment mon problème.

Ma solution actuelle me semble pas mal : vous en pensez quoi ?
=> Pour avoir un affichage parfait, reste juste à avoir le bon code CSS pour que la définition de la largeur de "div.blocTab div.fix_table_body table" rentre dans "div.blocTab div.fix_table_body" sans déclancher d'overflow sous IE7 (désactiver le js de mon code de test pour voir le problème de css)

vous savez pourquoi, j'ai ça sur certains navigateurs ?
Code :
1
2
3
4
5
 
$(elmt).width(setVal);
getVal = $(elmt).width();
// Sur certain navigateurs, getVal  != setVal (de quelques pixels)
// => C'est pourquoi j'ai inclus un correctif d'erreur
boboss123 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/01/2012, 10h23   #14
Membre confirmé
 
Inscription : septembre 2009
Messages : 749
Détails du profil
Informations forums :
Inscription : septembre 2009
Messages : 749
Points : 223
Points : 223
J'ai trouvé une solution pour que ça fonctionne aussi sous IE7 : il faut que je mette le tableau dans un div (ça commence à faire pas mal de div XD ) :

Code html :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
	<title>test</title>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 
	<style type='text/css'>
body{
min-width: 650px;
margin:0;
padding:0;
border:none;
font-family: Verdana, Arial, sans-serif;
color:black;
background-color:#ebecf6;
height:100%; /* pour layout ie6 */
}
 
display:block;
margin:0;
padding:0;
}
 
a, h1, h2, h3, h4, h5, p{
margin:0.3em 0;
padding:0;
border:none
}
 
.spacer { 
clear: both; 
} 
 
.spacer hr{     
  display: none; 
}
 
 
div.blocTab{
clear: both;
float:left;
 
margin: 5px 0px 10px 5px ;
background-color:red;
color:white;
}
 
div.blocTab h3{
font-weight:bold;
margin:0 4em 0 0;
padding:3px 5px 3px 20px;
background-position : -171px -65px;
}
 
div.blocTab table{
padding:0;
margin:0;
 
border-collapse:collapse;
border:none;
background-color:red;
color:#00039e}
 
div.blocTab th{
font-weight:bold;
background-color:blue;
color:white;
font-weight:bold;
}
 
div.blocTab th a{
display:block;
float:right;
 
height:10px;
width:10px;
padding:0;
margin:2px 0 0 2px;
 
background-position : -86px -65px;
}
 
div.blocTab th, td{
border:2px solid white;
text-align:center;
padding:2px 10px;
margin:0}
 
 
div.blocTab tr{
background-color:green;
margin:0;
padding:0;
}
div.blocTab tr.tr_imp{background-color:#dbe5f6;}
div.blocTab select{
margin:0;
padding: 0 2px 2px 2px;
}
 
 
div.blocTab div.fix_table_body{
height:10em;
overflow-y:scroll;
/*overflow-y:auto;*/
overflow-x:visible;
}
 
div.blocTab div.fix_table_body  table{
width:100%;
/*width:auto;*/
}
 
	</style>
 
	<script type="text/javascript" src="jquery-1.4.2.js"></script>
 
	<script type="text/javascript"> 
//<![CDATA[ 
 
 
// code executé au demarrage
$(document).ready(function(){	// Traitement à effectuer lorsque le DOM est prêt
//$(window).load(function(){		// Traitement à effectuer lorsque tous les elements de la page sont chargés
 
	initFixedTable();
 
});   
 
// ******************************************************
// creation des tableaux avec scrollbar verticale
var widthColumn = []; // sert a redefinir la largeur des cases du tableau quand on le met a jour
function initFixedTable(){
 
	var divDivTabList = $("div.fix_table");
 
	for (var i=0; i<divDivTabList.length; i++){ // parcours de tous les "div.fix_table"
 
		var divTabList = $(divDivTabList[i]).children("div"); // récupération de de tous les "div.fix_table > div"
		var tabHeader = $(divTabList[0]).children("table")[0]; // récupération de de tous les "div.fix_table > div.fix_table_header > table"
		var tabBody = $(divTabList[1]).find("div > table")[0]; // récupération de de tous les "div.fix_table > div.fix_table_body > div > table"
 
		var tabHeaderLines = $(tabHeader).find("tr");
		var tabBodyLines = $(tabBody).find("tr");
 
		//alert(tabHeaderLines.length); // nombre de ligne		
		//alert($(tabHeaderLines[0]).children().length); // nombre de colonnes de la première ligne (fonctionne pour thead et tbody)
 
 
		//var scrollBarWidth = getScrollerWidth();
 
		//var widthLine = $(tabHeaderLines[0]).width();
		//var widthDiv = $(divTabList[0]).width();
		//$(divTabList[1]).width(widthDiv + scrollBarWidth);
 
		//$(divTabList[0]).width(widthDiv);
 
		//$(tabBodyLines[0]).width(widthLine);
 
		//*
 
		//*
		var widthTabBody = $(tabBody).width();
		//alert("get TabBody: " + widthTabBody);
		$(tabHeader).width(widthTabBody); // +2 sur firefox
		var widthTabHead = $(tabHeader).width();
		//alert("get TabHead: " + widthTabHead);
 
		// le get width et le set width ne renvoit pas la meme chose
		// => on corrige l'erreur
		var diffWidth = widthTabBody - widthTabHead;
		widthTabHead = widthTabBody + diffWidth;
 
		$(tabHeader).width(widthTabHead);
 
		//if($(tabHeader).width() != $(tabBody).width()){
		//	alert("get TabHead: " + $(tabHeader).width());
		//	alert("get tabBody: " + $(tabBody).width());		
		//} else {
		//	alert("TabHead/tabBody width: OK");
		//}
 
		$(tabBody).width(widthTabHead); // on fige aussi le body pour éviter tout problème si l'unité n'est pas de type absolue (ex : em)
 
 
		//*		
		// ************************************************
		var headColumnCount = $(tabHeaderLines[0]).children().length; // nombre de colonnes de la première ligne (fonctionne pour thead et tbody)
		var diffWidthColumn = [];
 
		for(j=0; j<headColumnCount; j++){
			var columnTmp = $(tabHeaderLines[0]).children()[j];
			widthColumn[j] = $(columnTmp).width(); //alert(widthColumn);
		}
 
 
		//alert("set colonne width: " + widthColumn[0]);
		$($(tabBodyLines[0]).children()[0]).width(widthColumn[0]);
		//alert("get colonne width: " + $($(tabBodyLines[0]).children()[0]).width());
 
		diffWidthColumn = widthColumn[0] - $($(tabBodyLines[0]).children()[0]).width(); // calcul de la correction d'erreur
		//alert(diffWidthColumn);
 
		for(j=0; j<headColumnCount; j++){
		//for(j=0; j<1; j++){
			widthColumn[j] += diffWidthColumn;
			$($(tabBodyLines[0]).children()[j]).width(widthColumn[j]);
 
			// control d'erreur
			//if ($($(tabHeaderLines[0]).children()[j]).width() != $($(tabBodyLines[0]).children()[j]).width()){
			//	alert("Erreur0 largeur colonne: " + j);
			//} else {
			//	alert("largeur colonne OK: " + j);
			//}
		}
 
 
		// on fige aussi le head pour éviter tout problème si l'unité n'est pas de type absolue (ex : em)
		for(j=0; j<headColumnCount; j++){
			$($(tabHeaderLines[0]).children()[j]).width(widthColumn[j]);
		}
 
 
		// control d'erreur
		//for(j=0; j<headColumnCount; j++){
		//	if ($($(tabHeaderLines[0]).children()[j]).width() != $($(tabBodyLines[0]).children()[j]).width()){
		//		alert("Erreur largeur colonne: " + j);
		//	}
		//}
		// => TODO : on fait quoi si le tableau est vide ?
		// */
 
 
 
	}
}
 
 
 
//]]>
	</script>
 
</head>
 
<body style="min-width: 800px;">
 
	<div id="corps">
		<noscript><h1>Erreur : Javascript est désactivé</h1></noscript>
 
			<div class="blocTab fix_table" id="tableLogBloc" style="float:clear;width:600px;">
				<h3>Mon Titre</h3>
				<div class="fix_table_header">
					<table>
						<thead>
							<tr>
								<th>ma colonne 1</th>
								<th>ma colonne 2</th>
								<th>ma colonne 3</th>
								<th>ma colonne 4</th>
							</tr>
						</thead>
						<tbody style="display:none"><tr><td></td></tr></tbody> <!-- pour etre valide W3C -->
					</table>
				</div>
 
				<div class="fix_table_body" style="height:10em;">
					<div style="height:100%;">
						<table>
							<tbody>
								<tr>
									<td>1</td>
									<td></td>
									<td></td>
									<td></td>
								</tr>
 
								<tr>
									<td>2</td>
									<td></td>
									<td></td>
									<td></td>
								</tr>
 
								<tr>
									<td>3</td>
									<td></td>
									<td></td>
									<td></td>
								</tr>
 
								<tr>
									<td>4</td>
									<td></td>
									<td></td>
									<td></td>
								</tr>
 
 
								<tr>
									<td>5</td>
									<td></td>
									<td></td>
									<td></td>
								</tr>
 
								<tr>
									<td>1</td>
									<td></td>
									<td></td>
									<td></td>
								</tr>
 
								<tr>
									<td>2</td>
									<td></td>
									<td></td>
									<td></td>
								</tr>
 
								<tr>
									<td>3</td>
									<td></td>
									<td></td>
									<td></td>
								</tr>
 
								<tr>
									<td>4</td>
									<td></td>
									<td></td>
									<td></td>
								</tr>
 
 
								<tr>
									<td>5</td>
									<td></td>
									<td></td>
									<td></td>
								</tr>
 
							</tbody>
						</table>
					</div>
				</div>
			</div>
			<div class="spacer"></div>
 
		</div>
 
		<div class="spacer"><hr/></div>
	</div>
 
</body>
</html>

=> par contre le plug-in datatables cité précédemment le fait sans ce div... il y a donc surement une meilleure solution que la mienne...
boboss123 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/01/2012, 13h57   #15
Membre confirmé
 
Inscription : décembre 2009
Messages : 249
Détails du profil
Informations forums :
Inscription : décembre 2009
Messages : 249
Points : 219
Points : 219
En cherchant un peu j'ai trouvé ce plug-ins jQuery qui le fait : http://www.farinspace.com/jquery-scr...-table-plugin/

Je ne l'ai pas du tout essayé donc je ne sais pas s'il marche sur tous les browsers....
ticroch est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 04h10.


 
 
 
 
Partenaires

Hébergement Web