如何生成序列id并向上或向下移动表行并动态调整每一行的序列ID?

时间:2013-06-12 14:27:28

标签: javascript jquery-ui jquery javascript-events

1。我有两个表table1和table2,当你单击Add按钮时,它会将从table1检查的行移动到table2。当内容从table1添加到table2时,我必须从上到下生成从1到n的序列Id。当我将行从table1移动到table2时,如何生成此序列ID?

2。其次,我必须使用UP&amp ;;上下移动table2中的行。分别为DOWN按钮。 问题是我需要根据表行的移动位置更改序列值。例如:如果我将行向下移动1行,则序列值必须从1更改为2.但是,第一行序列值需要从2更改为1,因此始终保持从1到n的顺序顺序,而不管如何移动行。

非常感谢任何想法。

感谢。

这是jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Insert title here</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
 <script type="text/javascript">
 $(document).ready(function(){
        $('#add').on("click", function(){
            $('#one tbody input:checked').parent().parent().clone().appendTo("#two tbody");
            return false;
        });

        $('#remove').on("click", function(){
            $('#two tbody input:checked').parent().parent().remove();
            return false;
        });

        $(".up,.down").click(function(){
            var row = $('#two tbody input:checked').parents("tr:first");
            if ($('#two tbody input:checked').is(".up")) {
                row.insertBefore(row.prev());
            } else {
                row.insertAfter(row.next());
            }

            //return false;
        });
    });
 </script>
</head>
<body>
<table id="one" style="border:1px solid red;">
    <caption>Table 1</caption>
<thead>
<tr>
<th></th>
<th >ID</th>
<th> Name</th>
<th>System</th>
</tr>
</thead>
    <tbody>
      <tr>
        <td><input type="checkbox" /></td>
        <td>12</td>
        <td>Sam</td>
        <td>FSS</td>
     </tr>

     <tr>
        <td><input type="checkbox" /></td>
        <td>87</td>
        <td>Harry</td>
        <td>MSS</td>
     </tr>
     <tr>
        <td><input type="checkbox" /></td>
        <td>23</td>
        <td>Rita</td>
        <td>MVV</td>
     </tr>
     <tr>
        <td><input type="checkbox" /></td>
        <td>65</td>
        <td>Tom</td>
        <td>RDD</td>
     </tr>   
   </tbody>
</table>

<table id="two" style="border:1px solid green;">
    <caption>Table 2</caption>
<thead>
<tr>
<th></th>
<th >ID</th>
<th>Sequence No</th>
<th>Name</th>
<th>System</th>
</tr>
</thead>
    <tbody>

    </tbody>

</table>
<br><hr><br>
<button id="add">Add</button>
<button id="remove">Remove</button>
<button class="up">UP</button>
<button class="down">DOWN</button>
</body>
</html>

还有一件事,在我点击向上或向下按钮时,检查的行只会向下移动,不知道为什么?

2 个答案:

答案 0 :(得分:1)

你可以这样做:

$('#add').on("click", function(){
        var clone=$('#one tbody input:checked').parent().parent().clone();
        clone.each(function(){
            $(this).appendTo("#two tbody").attr("id",$(this).index());
        });
        return false;
    });

    $('#remove').on("click", function(){
        $('#two tbody input:checked').parent().parent().remove();
        $('#two tbody tr').each(function(){
            $(this).attr("id",$(this).index());
        });
        return false;
    });

    $(".up,.down").click(function(){
        var row = $('#two tbody input:checked').parent().parent();
        if ($(this).is(".up")) {
            row.each(function(){
                var previndex=$(this).prev().index();
                if(previndex>=0){
                    var rowindex=$(this).index();
                    $(this).attr("id",previndex);
                    $(this).prev().attr("id",rowindex);
                    $(this).insertBefore($(this).prev());
                }
            });
        } else {
            $(row.get().reverse()).each(function(){
                var nextindex=$(this).next().index();
                if(nextindex>=0){
                    var rowindex=$(this).index();
                    $(this).attr("id",nextindex);
                    $(this).next().attr("id",rowindex);
                    $(this).insertAfter($(this).next());
                }
            });
        }

    });     

http://jsfiddle.net/QM8Pg/

答案 1 :(得分:1)

我尽力解释代码,以便解释正在发生的事情:

jQuery(function($) {

  var tableOne = $('#one'),
      tableTwo = $('#two'),
      sequenceIndex = $('.sequence').index(),
      moved = [];

  function addChecked(e) {
    tableOne.find(':checked').each(function() {
      var row = $(this).closest('tr'),
          clone;

      // check if the row we're trying to 
      // move has already been moved
      if ($.inArray(row[0], moved) < 0) {

        // cache this row so we 
        // know it's been moved
        moved.push(row[0]);

        // clone and append to table2.
        // store original row as data 
        // so that we can remove from 
        // cache when rows are removed
        clone = row.clone()
          .data('orig', row[0])
          .appendTo(tableTwo.find('tbody'));

        // because table1 doesn't have the same
        // number of columns as table2 we have to
        // create an extra column within the row  
        // to hold the sequence num
        $('<td/>').insertAfter(clone.find('td').eq(sequenceIndex-1));
      }
    });

    updateSequence();
    e.preventDefault();
  }

  function removeChecked(e) {
    tableTwo.find(':checked').each(function() {
      var row = $(this).closest('tr'),
          // get the index of this row within the cache
          position = moved.indexOf(row.data('orig'));

      // remove this row from the cache
      moved.splice(position, 1);

      // remove this row from the DOM
      row.remove();
    });

    updateSequence();
    e.preventDefault();
  }

  function moveUpDown(e) {
    var button = $(this),
        isUp = button.hasClass('up'),
        rows = tableTwo.find(':checked').closest('tr');

    if (!isUp) {
      // if we're going down we need to 
      // reverse the row array so that the
      // rows don't just switch around
      Array.prototype.reverse.call(rows);
    }

    rows.each(function() {
      var checked = $(this),
          nextPrev = checked[isUp ? 'prev' : 'next']();

      // move the row
      if (nextPrev.length) {
        checked[isUp ? 'insertBefore' : 'insertAfter'](nextPrev);
      }
    });

    updateSequence();
    e.preventDefault();
  }

  function updateSequence() {
    tableTwo.find('tr').each(function() {
      var row = $(this),
          sequence = row.index()+1;

      row.find('td').eq(sequenceIndex).text(sequence);
    });
  }

  $('#add').on('click', addChecked);
  $('#remove').on('click', removeChecked);
  $('.up, .down').on('click', moveUpDown);
});

我还要稍微修改HTML。我刚刚在Sequence Num表头中添加了一个.sequence类。你可以在这里看到这个:http://jsbin.com/ocesop/1/edit