表格自动TR计数

时间:2017-10-02 16:28:37

标签: javascript jquery html css css3

我有这个表+我使用的JS脚本。 我想在左侧的TD中添加一个空的TR计数器。 因此,当我添加一列时,它会自动计数,例如,我有100列,当我删除35列时,数字应该会下降

var myVar;

function myFunction() {
  myVar = setTimeout(showPage, 3000);
}

function showPage() {
  document.getElementById("loader").style.display = "none";
  document.getElementById("myDiv").style.display = "block";
}

$(document).ready(function() {

  $('.buttons').on('click', 'button.hide', function() {
    console.log('hide');
    $('form').hide();
  });

  $('.buttons').on('click', 'button.add', function() {
    console.log('add');
    var edit = $('#edit');
    editRow = $('#editRow');

    edit.show();
    if (!($('#addNew').length)) {
      edit.append('<input type="button" id="addNew" onclick="addNewTr()" value="Add" name="submit" />');
    }

    if (editRow) {
      editRow.remove();
    }

    for (var x = 1; x < $('input').length; x++) {
      $('#btd' + x).val('');
    }
  });

  $('#show').click(function() {
    //$('form').show();
    //$('#btd1').val('Vlad');
    //$('#btd2').val('Andrei');
    //$('#btd3').val('vTask');
    //  $('#btd4').val('Ceva');
    //$('#btd5').val('Alceva');
  });
});

function edit(a) {
  var edit = $('#edit');
  addNew = $('#addNew');
  editRow = $('#editRow');

  edit.show();
  if (addNew) {
    addNew.remove();
  }

  if (editRow.length) {
    editRow.replaceWith('<input type="button" id="editRow" onclick="save(' + a + ')" value="Submit" name="submit" />');
  } else {
    edit.append('<input type="button" id="editRow" onclick="save(' + a + ')" value="Submit" name="submit" />');
  }

  $.each($('.tr-' + a).find('td'), function(key, val) {
    $('form#edit input[type=text]').eq(key).val($(val).text());
  });
}

function save(a) {
  var tr = $('tr');
  valid = true;
  message = '';

  $('form#edit input').each(function() {
    var $this = $(this);

    if (!$this.val()) {
      var inputName = $this.attr('name');
      valid = false;
      message += 'Please complete all the colums' + inputName + '\n';
    }
  });

  if (!valid) {
    alert(message);
  } else {
    for (var q = 1; q < $('.tr-' + a + ' td').length; q++) {
      $('.tr-' + a + ' td:nth-child(' + q + ')').html($('#btd' + q).val());
    }
    for (var x = 1; x < $('input').length; x++) {
      $('#btd' + x).val('');
    }
    $('#editRow').remove();
  }
}

function addNewTr() {
  var tr = $('tr');
  valid = true;
  message = '';

  $('form#edit input').each(function() {
    var $this = $(this);

    if (!$this.val()) {
      var inputName = $this.attr('name');
      valid = false;
      message += 'Please enter your ' + inputName + '\n';
    }
  });

  if (!valid) {
    alert(message);
  } else {
    $('table tbody').append('' +
      '<tr class="tr-' + tr.length + '">' +
      '<td>' + $('#btd1').val() + '</td>' +
      '<td>' + $('#btd2').val() + '</td>' +
      '<td>' + $('#btd3').val() + '</td>' +
      '<td>' + $('#btd4').val() + '</td>' +
      '<td>' + $('#btd5').val() + '</td>' +
      '<td class="buttons">' +
      '<button class="removeThis" onclick="removeThis(' + tr.length + ')">Delete</button >' +
      '<button class="edit" onclick="edit(' + tr.length + ')">Edit</button >' +
      '</td >' +
      '</tr>' +
      '');
    for (var x = 1; x < $('input').length; x++) {
      $('#btd' + x).val('');
    }
  }
}

function removeThis(a) {
  $('.tr-' + a).remove();
}

$('.addRow').on('click', function() {
  addRow();
});

var i = 1;

function addRow() {
  var tr = '<tr>' +
    '<td>' + i + '</td>' +
    '</tr>';
  $('tbody').append(tr);
  i++;
};
/* Center the loader */

#loader {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 1;
  width: 150px;
  height: 150px;
  margin: -75px 0 0 -75px;
  border: 16px solid #e80041;
  border-radius: 50%;
  border-top: 16px solid #188f84;
  width: 120px;
  height: 120px;
  -webkit-animation: spin 2s linear infinite;
  animation: spin 2s linear infinite;
}

@-webkit-keyframes spin {
  100% {
    -webkit-transform: rotate(360deg);
  }
  0% {
    -webkit-transform: rotate(0deg);
  }
}

@keyframes spin {
  100% {
    transform: rotate(360deg);
  }
  0% {
    transform: rotate(0deg);
  }
}


/* Add animation to "page content" */

.animate-bottom {
  position: relative;
  -webkit-animation-name: animatebottom;
  -webkit-animation-duration: 1s;
  animation-name: animatebottom;
  animation-duration: 1s
}

@-webkit-keyframes animatebottom {
  from {
    bottom: -100px;
    opacity: 0
  }
  to {
    bottom: 0px;
    opacity: 1
  }
}

@keyframes animatebottom {
  from {
    bottom: -100px;
    opacity: 0
  }
  to {
    bottom: 0;
    opacity: 1
  }
}

#myDiv {
  display: none;
  text-align: center;
}


/* Top Header */

#hh1 {
  color: #1b998b;
  text-align: center;
  font-size: 40px;
  border-bottom: 2px solid #FFFFFF;
  font-family: Sans-serif;
}

#vt1 {
  color: White;
  font-size: 40px;
  text-align: center;
}


/* BODY */

body {
  background-color: #999da5;
}

table.greenTable {
  font-family: Tahoma, Geneva, sans-serif;
  border: 6px solid #152842;
  background-color: #EEEEEE;
  width: 70%;
  text-align: center;
}

table.greenTable td,
table.greenTable th {
  border: 4px solid #1F6698;
  padding: 3px 2px;
}

table.greenTable tbody td {
  font-size: 13px;
  font-weight: bold;
}

table.greenTable tr:nth-child(even) {
  background: #999DA5;
}

table.greenTable thead {
  background: #93B5C1;
  background: -moz-linear-gradient(top, #aec7d0 0%, #9dbcc7 66%, #93B5C1 100%);
  background: -webkit-linear-gradient(top, #aec7d0 0%, #9dbcc7 66%, #93B5C1 100%);
  background: linear-gradient(to bottom, #aec7d0 0%, #9dbcc7 66%, #93B5C1 100%);
  border-bottom: 0px solid #444444;
}

table.greenTable thead th {
  font-size: 19px;
  font-weight: bold;
  color: #F0F0F0;
  text-align: center;
  border-left: 2px solid #1B998B;
}

table.greenTable thead th:first-child {
  border-left: none;
}

table.greenTable tfoot td {
  font-size: 13px;
}

table.greenTable tfoot .links {
  text-align: right;
}

table.greenTable tfoot .links a {
  display: inline-block;
  background: #FFFFFF;
  color: #000000;
  padding: 2px 8px;
  border-radius: 5px;
}


/* BUTTONS */


/* BODY */

button {
  font-family: arial;
  font-weight: bold;
  color: #000000 !important;
  font-size: 14px;
  padding: 2px 10px;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  border: 2px solid #152842;
  background: #63B8EE;
  background: linear-gradient(top, #1F6698, #1B998B);
  background: -ms-linear-gradient(top, #1F6698, #1B998B);
  background: -webkit-gradient(linear, left top, left bottom, from(#1F6698), to(#1B998B));
  background: -moz-linear-gradient(top, #1F6698, #1B998B);
}

button:hover {
  color: #14396A !important;
  background: #468CCF;
  background: linear-gradient(top, #93B5C1, #D1D1D1);
  background: -ms-linear-gradient(top, #93B5C1, #D1D1D1);
  background: -webkit-gradient(linear, left top, left bottom, from(#93B5C1), to(#D1D1D1));
  background: -moz-linear-gradient(top, #93B5C1, #D1D1D1);
}

#addNew {
  font-family: arial;
  font-weight: bold;
  color: #000000 !important;
  font-size: 14px;
  padding: 1px 10px;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  border: 2px solid #152842;
  background: #63B8EE;
  background: linear-gradient(top, #1F6698, #1B998B);
  background: -ms-linear-gradient(top, #1F6698, #1B998B);
  background: -webkit-gradient(linear, left top, left bottom, from(#1F6698), to(#1B998B));
  background: -moz-linear-gradient(top, #1F6698, #1B998B);
}

#addNew:hover {
  color: #14396A !important;
  background: #468CCF;
  background: linear-gradient(top, #93B5C1, #D1D1D1);
  background: -ms-linear-gradient(top, #93B5C1, #D1D1D1);
  background: -webkit-gradient(linear, left top, left bottom, from(#93B5C1), to(#D1D1D1));
  background: -moz-linear-gradient(top, #93B5C1, #D1D1D1);
}

#editRow {
  font-family: arial;
  font-weight: bold;
  color: #000000 !important;
  font-size: 14px;
  padding: 1px 10px;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  border: 2px solid #152842;
  background: #63B8EE;
  background: linear-gradient(top, #1F6698, #1B998B);
  background: -ms-linear-gradient(top, #1F6698, #1B998B);
  background: -webkit-gradient(linear, left top, left bottom, from(#1F6698), to(#1B998B));
  background: -moz-linear-gradient(top, #1F6698, #1B998B);
}

#editRow:hover {
  color: #14396A !important;
  background: #468CCF;
  background: linear-gradient(top, #93B5C1, #D1D1D1);
  background: -ms-linear-gradient(top, #93B5C1, #D1D1D1);
  background: -webkit-gradient(linear, left top, left bottom, from(#93B5C1), to(#D1D1D1));
  background: -moz-linear-gradient(top, #93B5C1, #D1D1D1);
}

a {
  color: #fff;
  text-decoration: none;
}

.read-more {
  background: #222;
  padding: 10px 50px;
  -webkit-box-shadow: 0 5px 6px #eee;
  -moz-box-shadow: 0 5px 6px #eee;
  box-shadow: 0 5px 6px #eee;
}

.read-more:before {
  content: "<<";
  color: #fff;
}

#content {
  color: #222;
  text-align: center;
  max-width: 30%;
  margin: 200px auto 0;
  padding: 50px;
  border: 4px solid #222;
}

#preloader {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #222222;
  z-index: 99999;
  height: 100%;
}

#status {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 64px;
  height: 64px;
  margin: -32px 0 0 -32px;
  padding: 0;
}
<!DOCTYPE html>
<html>

<head>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <title></title>
</head>

<body onload="myFunction()" style="margin:0;">
  <div id="loader"></div>
  <div style="display:none;" id="myDiv" class="animate-bottom">
    <h1 id="hh1">[<a id="vt1">vTask</a>]</h1>

    <table class="greenTable" id="myTable">
      <tbody>
        <tr>
          <td colspan="6">
            <form id="edit" action="" method="post" hidden>
              <label for="btd1"></label >
      <input type="text" name="Name" id="btd1" value="" placeholder="Name">
  <label for="btd2" ></label >
      <input type="text" name="Secondary Name" id="btd2" value="" placeholder="Secondary Name">
  <label for="btd3" ></label >
      <input type="text" name="Email" id="btd3" value="" placeholder="Email">
  <label for="btd4" ></label >
      <input type="text" name="Telephone" id="btd4" value="" placeholder="Telephone">
  <label for="btd5" ></label >
      <input type="text" name="Password" id="btd5" value="" placeholder="Password">
  </form ></td></tr>
  <tr>
    <td></td>
  <td width="10%">Password</td>
  <td width="10%">Name</td>
  <td width="10%">Secondary Name</td>
  <td width="10%">Email</td>
  <td width="10%">Telephone</td>
  <td class="buttons" width="20%"><button class="add" >Add</button >
  <button class="hide" >Hide</button >
  </td>
  </tr>
  </tbody>
  </table >
</div>
</body>
</html>

请帮帮我。在脚本的底部,我有一个基于TBody的计数器,但它不起作用

1 个答案:

答案 0 :(得分:0)

这是Demo。我解雇了文件就绪功能。您可以根据需要更改触发状态。它可能是onkeyup,onclik等。只需触发该功能然后计算,然后告诉你的号码。

Here You GO