Generate values dynamically in a table

Asked

Viewed 49 times

0

I am listing the number of equipment saved in my system in a table, only that they will not always have saved equipment, I can have an empty table, or incomplete. What I need is to list the lists of this table. My table can have 5 or 10 values, that is, always 5 or 10 lines to be filled in. So I would like to dynamically generate the amount of rows in this table.
As shown below: inserir a descrição da imagem aqui

Instead of listing 1,1,1,1,1,1... it would be 1,2,3,4,5,6...

My html table:

#{extends 'main.html' /}

<script>
organizarIndices();

function organizarIndices(){
  var table = document.getElementById('tabela');
  var total = table.rows.length;
  for(var i=0; i<total; i++){
  if(i > 0){
      table.rows[i].cells[0].innerHTML = i;
  }
  }
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<div class="row">
<div class="col-lg-12">
    <ol class="breadcrumb">
        <li><i class="fa fa-dashboard"></i> <a
            href="@{Application.index}">Dashboard</a></li>
        <li><i class="fa fa-cubes"></i> <a
            href="@{Patchpanels.listagemPatchpanel}">Patch panels</a></li>
        <li class="active"><i class="fa fa-edit"></i> Detalhes do patch
            panel</li>
    </ol>
</div>
 </div>
  <!-- /.row -->
   <div class="panel-body">
    <ul class="list-group">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">
                        <i class="fa fa-cubes"></i> Detalhes Patch panels
                    </h3>
                </div>
                <div class="panel-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover table-striped" id="tabela">
                            <thead>
                                <tr>
                                    <th>Porta</th>
                                    <th>Equipamento</th>
                                </tr>
                            </thead>
                            <tbody>
                                #{list items:p.portas, as:'porta'}
                                <tr>
                                    <td><b></b></td>
                                    <td>${porta.descricao}
                                            <div class="pull-right action-buttons">
                                                <span class="btn btn-success btn-xs" id="alert-target"> Ativar</span> <span
                                                    class="btn btn-warning btn-xs"> Reiniciar</span> <span
                                                    class="btn btn-danger btn-xs"> Desativar</span>
                                            </div>
                                        </td>
                                </tr>
                                #{/list}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </ul>
</div>

  • Dude, you got the number right there in the td (<td><b>1</b></td>), if it has to be dynamic makes dynamic...

  • I know, I left it there to show in the image, but I know it’s a static. The pro is that I don’t know how to do it dynamically, Oce can help?

  • Ah, good. But that shouldn’t come from backend? Or you just need it to be sequential?

  • From what I understand you want to show the position(index) of each item in the list?

  • I need it to be sequential. Until it is already listed in the correct order. In other words, I registered equipment 1 at gate 7, it came at position 7 of the table

1 answer

2


Perform the function organizarIndices as soon as the page has been loaded and ready

organizarIndices();

function organizarIndices(){
  var table = document.getElementById('tabela');
  var total = table.rows.length;
  for(var i=0; i<total; i++){
      if(i > 0){
          table.rows[i].cells[0].innerHTML = i;
      }
  }
}
<table id="tabela">
<thead>
    <tr>
        <th>Porta</th>
        <th>Equipamento</th>
    </tr>
</thead>
<tbody>    
    <tr>
        <td>
        </td>
        <td>
          Equipamento 
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
          Equipamento
        </td>
    </tr>
</tbody>
</table>

  • organizarIndices(); get inside the script? I put the script at the top of the html and put the id in the table and did not run right

  • It has to be after the page has been loaded, young man. At the beginning of HTML you can’t, right.

  • It worked moral, I’m new in JQ and so I don’t master the syntax.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.