javascript - Comment implémenter la totalisation automatique des tableaux
黄舟
黄舟 2017-07-01 09:11:59
0
3
1021
ID Champ 1 Champ 2 Champ 3 Champ 4
1 11 12 13 14
2 22 26 28 27
Total

    <table id="mytable" border="1" width="37%">
  <thead>
    <tr>
    <td width="63" >ID</td>
    <td width="68" >字段1</td>
    <td width="62" >字段2</td>
    <td width="75" >字段3</td>
    <td>字段4</td>
  </tr>
  </thead>

  <tr>
    <td>1</td>
    <td width="63" >11</td>
    <td width="68" >12</td>
    <td width="62" >13</td>
    <td width="75" >14</td>
  </tr>
  <tr>
    <td>2</td>
    <td width="63" >22</td>
    <td width="68" >26</td>
    <td width="62" >28</td>
    <td width="75" >27</td>
  </tr>
  <tr id="totalRow">
    <td>合计</td>
    <td width="63" ></td>
    <td width="68" ></td>
    <td width="62" ></td>
    <td width="75" ></td>
  
  </tr>
</table>


Je viens d'apprendre le front-end. Est-ce que quelqu'un sait comment utiliser JQ pour additionner automatiquement chaque colonne au total
Il existe de nombreux tableaux de ce type sur une seule page qui doivent être comptés Q_Q

?
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

répondre à tous(3)
为情所困

Faites attention à la structure HTML, vous avez écrit thead却没有写body, modifiez-la comme suit :

<table id="mytable" border="1" width="37%">
<thead>
    <tr>
        <td width="63">ID</td>
        <td width="68">字段1</td>
        <td width="62">字段2</td>
        <td width="75">字段3</td>
        <td>字段4</td>
    </tr>
</thead>
<tbody>
    <tr>
        <td>1</td>
        <td width="63">11</td>
        <td width="68">12</td>
        <td width="62">13</td>
        <td width="75">14</td>
    </tr>
    <tr>
        <td>2</td>
        <td width="63" >22</td>
        <td width="68" >26</td>
        <td width="62" >28</td>
        <td width="75" >27</td>
    </tr>
</tbody>
<tfoot>
    <tr id="totalRow">
        <td>合计</td>
        <td width="63"></td>
        <td width="68"></td>
        <td width="62"></td>
        <td width="75"></td>
    </tr>
</tfoot>
</table>

Parmi eux, theadtbodytfoot都分开,thead为表头不遍历,tbody为遍历部分,tfoot est la partie récapitulative pour faciliter l'utilisation du sélecteur jQuery.

var sum = new Array(+$('#mytable thead tr td:not(:first-child)').length).fill(0);

$('#mytable tbody tr').each(function() {
    $(this).children('td:not(:first-child)').each(function() {
        var index = $(this).index() - 1;

        sum[index] += +$(this).text();
    });
});

$('#mytable #totalRow td:not(:first-child)').each(function() {
    var index = $(this).index() - 1;

    $(this).text(sum[index]);
});

Mise à jour

Voici quelques points de connaissances :

  1. new Array()Construire un nouveau tableau

  2. .fill()remplir le tableau

  3. Utilisation du sélecteur:not():first-child

  4. Ajoutez + devant une chaîne ou un nombre pour forcer sa conversion en nombre

  5. Utilisation d'un opérateur d'affectation+=

某草草

J'espère que vous pourrez comprendre le
code HTML

<table border="" cellspacing="" cellpadding="">
        <tr>
            <th>ID</th>
            <th>字段1</th>
            <th>字段2</th>
            <th>字段3</th>
            <th>字段四</th>
        </tr>
        <tr id="shuju_0">
            <td>数据1</td>
            <td>12</td>
            <td>21</td>
            <td>25</td>
            <td>2</td>
        </tr>
        <tr id="shuju_1">
            <td>数据2</td>
            <td>32</td>
            <td>16</td>
            <td>42</td>
            <td>21</td>
        </tr>
        <tr id="shuju_2">
            <td>合计</td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </table>

code js

var shuju0 =$("#shuju_0").children(),
        shuju1=$("#shuju_1").children(),
        shuju2=$("#shuju_2").children();
        for(var i=1;i<shuju0.length;i++){
            console.log($(shuju0[i]).text());
            $(shuju2[i]).text($(shuju0[i]).text()*1+$(shuju1[i]).text()*1);
        }
世界只因有你

jquery chacun parcourt chaque colonne, les additionne et les place dans la dernière ligne

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal