IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

jQuery Discussion :

Dimensionner largeur de colonne


Sujet :

jQuery

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre chevronné
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 855
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 855
    Par défaut Dimensionner largeur de colonne
    Bonjour,

    voici mon code html :
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    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,

  2. #2
    Inactif  

    Homme Profil pro
    développeur Vala
    Inscrit en
    Février 2011
    Messages
    478
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Cher (Centre)

    Informations professionnelles :
    Activité : développeur Vala
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2011
    Messages : 478
    Par défaut
    si ton nombre de colonnes est limité, je te conseille d'attribuer la même classe à chaque colonne.

  3. #3
    Membre chevronné
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 855
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 855
    Par défaut
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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);
    }

  4. #4
    Membre chevronné
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 855
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 855
    Par défaut
    code complet pour debuger :
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    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

  5. #5
    Rédacteur
    Avatar de Macmillenium
    Homme Profil pro
    Développeur front-end
    Inscrit en
    Mars 2008
    Messages
    2 333
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur front-end
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Mars 2008
    Messages : 2 333
    Par défaut
    J'ai pas le courage de lire ton code...
    Essaye ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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])
    });

  6. #6
    Membre chevronné
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 855
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 855
    Par défaut
    Non, ça ne fonctionne pas : Lorsque la scrollbar s'active, les largeurs des cellules du tableau contenant les données est automatiquement redimensionné

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Comment fixer les largeurs des colonnes d'un TStrinGrid
    Par marsupilami34 dans le forum Composants VCL
    Réponses: 6
    Dernier message: 24/08/2005, 13h24
  2. forcer largeur de colonne qd URL chargee
    Par Batou dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 18/08/2005, 10h38
  3. Fixer la largeur des colonnes d'une CListCtrl
    Par Depteam1 dans le forum MFC
    Réponses: 4
    Dernier message: 28/06/2005, 15h46
  4. Largeur de colonne d'une ListView
    Par deaqu1 dans le forum IHM
    Réponses: 7
    Dernier message: 07/06/2005, 19h45
  5. [C#] Largeur de colonne et de ligne dans un DataGrid
    Par pc152 dans le forum Windows Forms
    Réponses: 3
    Dernier message: 31/08/2004, 14h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo