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 :

Remplacer un mot par une image


Sujet :

jQuery

  1. #21
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2013
    Messages
    32
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2013
    Messages : 32
    Points : 9
    Points
    9
    Par défaut
    Bonjour devyan,

    Je viens de tester votre code, il n'affiche pas les images.

    Merci

  2. #22
    Membre expérimenté
    Profil pro
    Inscrit en
    Mars 2002
    Messages
    1 132
    Détails du profil
    Informations personnelles :
    Âge : 52
    Localisation : France

    Informations forums :
    Inscription : Mars 2002
    Messages : 1 132
    Points : 1 418
    Points
    1 418
    Par défaut
    Citation Envoyé par Steufa Voir le message
    Bonjour devyan,

    Je viens de tester votre code, il n'affiche pas les images.

    Merci
    Bonjour Steufa,

    J'ai effectivement répondu un peu vite ...
    Je me suis basé sur le code de la première réponse de Daniel et ai copié la ligne de code de la dernière .

    J'ai fait un exemple jsfiddle.net et ça fonctionne pour la partie navigateur (reste à tester la partie export).

    Le HTML =
    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
     
    <table>
        <tr>
            <th>jQuery</th>
            <th>HTML</th>
        </tr>
        <tr>
            <td class="showImg">report-40b</td>
            <td>
                <img src="http://www.developpez.net/forums/images/buttons/report-40b.png" alt="report-40b" />
            </td>
        </tr>
        <tr>
            <td class="showImg">image-inconnue</td>
            <td>
                <img src="http://www.developpez.net/forums/images/buttons/image-inconnue.png" alt="image-inconnue" />
            </td>
        </tr>
        <tr>
            <td class="showImg">multiquote-back_40b</td>
            <td>
                <img src="http://www.developpez.net/forums/images/buttons/multiquote-back_40b.png" alt="multiquote-back_40b" />
            </td>
        </tr>
    </table>
    Le JS =
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    $(function(){
        $(".showImg").each(function (i, item) {
            $(item).wrapInner('<div class="display-none" />')
                      .append('<img src="http://www.developpez.net/forums/images/buttons/' + $(item).text() + '.png" />');
        });
    });
    Le CSS =
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    .display-none {
        display:none
    }
    table {
        border-spacing: 0;
        border-collapse: collapse;
    }
    td, th {
        border:1px solid black
    }
    devYan.


    devYan.

  3. #23
    Rédacteur

    Avatar de danielhagnoul
    Homme Profil pro
    Étudiant perpétuel
    Inscrit en
    Février 2009
    Messages
    6 389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 73
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant perpétuel
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2009
    Messages : 6 389
    Points : 22 933
    Points
    22 933
    Billets dans le blog
    125
    Par défaut
    Citation Envoyé par Steufa Voir le message
    Bonjour Daniel,

    Je reviens vers toi, car j'ai un petit soucis, en effet, j'utilise ExcellentExport.js pour exporter mon tableau au format CSV. Lors de l'export, le texte dans <td class="showImg" data-img-src="imageTest.png">Info image test</td> ne s'exporte plus.

    ExcellentExport.js : https://github.com/jmaister/excellentexport
    Bonsoir

    C'est logique puisque la méthode html() supprime le contenu du TD, le texte n'existe plus, il n'y a plus que l'image !

    Avec append() le texte est toujours présent, exemple :

    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
    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
    <!DOCTYPE html>
    <html>
    <head>
    	<meta charset="utf-8">
    	<title>imageTest</title>
    	<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    	<script>
            /**
             * ExcellentExport.
             * A client side Javascript export to Excel.
             *
             * @author: Jordi Burgos (jordiburgos@gmail.com)
             *
             * Based on:
             * https://gist.github.com/insin/1031969
             * http://jsfiddle.net/insin/cmewv/
             *
             * CSV: http://en.wikipedia.org/wiki/Comma-separated_values
             */
     
            /*
             * Base64 encoder/decoder from: http://jsperf.com/base64-optimized
             */
     
            /*jslint browser: true, bitwise: true, plusplus: true, vars: true, white: true */
     
            var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
            var fromCharCode = String.fromCharCode;
            var INVALID_CHARACTER_ERR = (function () {
                    "use strict";
                    // fabricate a suitable error object
                    try {
                        document.createElement('$');
                    } catch (error) {
                        return error;
                    }
                }());
     
            // encoder
            if (!window.btoa) {
                window.btoa = function (string) {
                    "use strict";
                    var a, b, b1, b2, b3, b4, c, i = 0, len = string.length, max = Math.max, result = '';
     
                    while (i < len) {
                        a = string.charCodeAt(i++) || 0;
                        b = string.charCodeAt(i++) || 0;
                        c = string.charCodeAt(i++) || 0;
     
                        if (max(a, b, c) > 0xFF) {
                            throw INVALID_CHARACTER_ERR;
                        }
     
                        b1 = (a >> 2) & 0x3F;
                        b2 = ((a & 0x3) << 4) | ((b >> 4) & 0xF);
                        b3 = ((b & 0xF) << 2) | ((c >> 6) & 0x3);
                        b4 = c & 0x3F;
     
                        if (!b) {
                            b3 = b4 = 64;
                        } else if (!c) {
                            b4 = 64;
                        }
                        result += characters.charAt(b1) + characters.charAt(b2) + characters.charAt(b3) + characters.charAt(b4);
                    }
                    return result;
                };
            }
     
            // decoder
            if (!window.atob) {
                window.atob = function(string) {
                    "use strict";
                    string = string.replace(new RegExp("=+$"), '');
                    var a, b, b1, b2, b3, b4, c, i = 0, len = string.length, chars = [];
     
                    if (len % 4 === 1) {
                        throw INVALID_CHARACTER_ERR;
                    }
     
                    while (i < len) {
                        b1 = characters.indexOf(string.charAt(i++));
                        b2 = characters.indexOf(string.charAt(i++));
                        b3 = characters.indexOf(string.charAt(i++));
                        b4 = characters.indexOf(string.charAt(i++));
     
                        a = ((b1 & 0x3F) << 2) | ((b2 >> 4) & 0x3);
                        b = ((b2 & 0xF) << 4) | ((b3 >> 2) & 0xF);
                        c = ((b3 & 0x3) << 6) | (b4 & 0x3F);
     
                        chars.push(fromCharCode(a));
                        b && chars.push(fromCharCode(b));
                        c && chars.push(fromCharCode(c));
                    }
                    return chars.join('');
                };
            }
     
     
            ExcellentExport = (function() {
                "use strict";
                var version = "1.3";
                var csvSeparator = ',';
                var uri = {excel: 'data:application/vnd.ms-excel;base64,', csv: 'data:application/csv;base64,'};
                var template = {excel: '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'};
                var csvDelimiter = ",";
                var csvNewLine = "\r\n";
                var base64 = function(s) {
                    return window.btoa(window.unescape(encodeURIComponent(s)));
                };
                var format = function(s, c) {
                    return s.replace(new RegExp("{(\\w+)}", "g"), function(m, p) {
                        return c[p];
                    });
                };
     
                var get = function(element) {
                    if (!element.nodeType) {
                        return document.getElementById(element);
                    }
                    return element;
                };
     
                var fixCSVField = function(value) {
                    var fixedValue = value;
                    var addQuotes = (value.indexOf(csvDelimiter) !== -1) || (value.indexOf('\r') !== -1) || (value.indexOf('\n') !== -1);
                    var replaceDoubleQuotes = (value.indexOf('"') !== -1);
     
                    if (replaceDoubleQuotes) {
                        fixedValue = fixedValue.replace(/"/g, '""');
                    }
                    if (addQuotes || replaceDoubleQuotes) {
                        fixedValue = '"' + fixedValue + '"';
                    }
                    return fixedValue;
                };
     
                var tableToCSV = function(table) {
                    var data = "";
                    var i, j, row, col;
                    for (i = 0; i < table.rows.length; i++) {
                        row = table.rows[i];
                        for (j = 0; j < row.cells.length; j++) {
                            col = row.cells[j];
                            data = data + (j ? csvDelimiter : '') + fixCSVField(col.textContent.trim());
                        }
                        data = data + csvNewLine;
                    }
                    return data;
                };
     
                var ee = {
                    /** @expose */
                    excel: function(anchor, table, name) {
                        table = get(table);
                        var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
                        var hrefvalue = uri.excel + base64(format(template.excel, ctx));
                        anchor.href = hrefvalue;
                        // Return true to allow the link to work
                        return true;
                    },
                    /** @expose */
                    csv: function(anchor, table, delimiter, newLine) {
                        if (delimiter !== undefined && delimiter) {
                            csvDelimiter = delimiter;
                        }
                        if (newLine !== undefined && newLine) {
                            csvNewLine = newLine;
                        }
                        table = get(table);
                        var csvData = tableToCSV(table);
                        var hrefvalue = uri.csv + base64(csvData);
                        anchor.href = hrefvalue;
                        return true;
                    }
                };
     
                return ee;
            }());
     
    		$(function(){
     
    			$( ".showImg" ).each( function( i, item ){
     
    				$( item ).append( '<img src="http://danielhagnoul.developpez.com/images/' + $( item ).data( "imgSrc" ) + '">' );
    			});
    		});
    	</script>
    </head>
    <body>
        <a download="somedata.csv" href="#" onclick="return ExcellentExport.csv(this, 'datatable');">Export to CSV - UTF8</a>
    	<table id="datatable" width="200" border="1">
    		<tr>
    			<td class="showImg" data-img-src="imageTest.png"><p>Image 1</p></td>
    		</tr>
    		<tr>
    			<td class="showImg" data-img-src="avatarDVJH.jpg"><p>Image 2</p></td>
    		</tr>
    		<tr>
    			<td class="showImg" data-img-src="julia2.png"><p>Image 3</p></td>
    		</tr>
    	</table>
    </body>
    </html>

    Blog

    Sans l'analyse et la conception, la programmation est l'art d'ajouter des bogues à un fichier texte vide.
    (Louis Srygley : Without requirements or design, programming is the art of adding bugs to an empty text file.)

Discussions similaires

  1. Remplacer un bouton par une image
    Par The_Haunted dans le forum Balisage (X)HTML et validation W3C
    Réponses: 9
    Dernier message: 08/10/2008, 14h19
  2. Remplacer le curseur par une image
    Par CyberTwister dans le forum Windows
    Réponses: 2
    Dernier message: 10/05/2007, 20h31
  3. Remplacer du texte par une image en css
    Par Seth77 dans le forum Mise en page CSS
    Réponses: 12
    Dernier message: 03/04/2006, 11h57
  4. Remplacer un caractère par une image
    Par mr.t dans le forum Balisage (X)HTML et validation W3C
    Réponses: 7
    Dernier message: 27/10/2005, 09h18
  5. Réponses: 2
    Dernier message: 26/07/2005, 21h44

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