Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
How to make the row number to adjust automatically when drag and drop a row in HTML

There is a HTML table which is set tbody to draggable via jQuery.

$('tbody').sortable();
                            
   var checklistContentTableOnSort = document.getElementById('table');

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table class="table table-bordered table-hover" id="table">
   <thead>
      <tr>
         <th class="text-center"> # </th>
         <th class="text-center"> Text </th>
         <th class="text-center"> Comment </th>
      </tr>
   </thead>
   <tbody class="ui-sortable">
      <tr id="addr0" class="ui-sortable-handle">
         <td> 1 </td>
         <td>         <input type="text" name="text0" placeholder="Text" class="form-control" onchange="addValueOnChange(this)" value="Adg">       </td>
         <td>         <input type="text" name="comment0" placeholder="Comment" class="form-control" onchange="addValueOnChange(this)" value="A">       </td>
      </tr>
      <tr class="ui-sortable-handle">
         <td>2</td>
         <td><input name="text2" type="text" placeholder="Text" class="form-control input-md" onchange="addValueOnChange(this)" value="B"> </td>
         <td><input name="comment2" type="text" placeholder="Comment" class="form-control input-md" onchange="addValueOnChange(this)" value="B"></td>
      </tr>
      <tr class="ui-sortable-handle">
         <td>3</td>
         <td><input name="text3" type="text" placeholder="Text" class="form-control input-md" onchange="addValueOnChange(this)" value="C"> </td>
         <td><input name="comment3" type="text" placeholder="Comment" class="form-control input-md" onchange="addValueOnChange(this)" value="C"></td>
      </tr>
   </tbody>
</table>

In here the first column has the row number. When dragging a row, I want to re arrange all the number to the correct order. can you suggest me a way to do this via JavaScript or jQuery.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Personally I would be tempted to use a CSS counter variable and assign that as the content of the table cell so then it should not matter how these are dragged it will be automagically be numbered. Id also be tempted to remove the inline event handlers in favour of an externally registered version.

const addValueOnChange=(n)=>{alert(n.value)}// example only

document.querySelectorAll('tbody td input[type="text"]').forEach(n=>n.addEventListener('change',function(e){
  addValueOnChange(this)
}))
:root{
  counter-reset:rows;
}
tbody tr{
  counter-increment:rows;
}
tbody tr td:first-of-type:before{
  content:counter(rows);
}
<table class="table table-bordered table-hover" id="table">
   <thead>
      <tr>
         <th class="text-center"> # </th>
         <th class="text-center"> Text </th>
         <th class="text-center"> Comment </th>
      </tr>
   </thead>
   <tbody class="ui-sortable">
      <tr id="addr0" class="ui-sortable-handle">
         <td></td>
         <td><input type="text" name="text0" placeholder="Text" class="form-control" value="Adg" /></td>
         <td><input type="text" name="comment0" placeholder="Comment" class="form-control" value="A" /></td>
      </tr>
      <tr class="ui-sortable-handle">
         <td></td>
         <td><input name="text2" type="text" placeholder="Text" class="form-control input-md" value="B" /></td>
         <td><input name="comment2" type="text" placeholder="Comment" class="form-control input-md" value="B" /></td>
      </tr>
      <tr class="ui-sortable-handle">
         <td></td>
         <td><input name="text3" type="text" placeholder="Text" class="form-control input-md" value="C" /> </td>
         <td><input name="comment3" type="text" placeholder="Comment" class="form-control input-md" value="C" /></td>
      </tr>
   </tbody>
</table>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda