Jquery排序表,但我需要维护优先级列号序列

时间:2017-09-02 14:57:26

标签: jquery jquery-ui

这是一个拖放表,但我有一个问题,如何维护表中优先级列的数字序列。

<!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> Drag and drop sorting of table rows</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {

    var fixHelperModified = function(e, tr) {
        var $originals = tr.children();
        var $helper = tr.clone();
        $helper.children().each(function(index)
        {
          $(this).width($originals.eq(index).width())
        });
        return $helper;
    };
$("button").click(function(){
        $("#st").clone().appendTo("#dvr");
    });


    $("#diagnosis_list tbody").sortable({
        helper: fixHelperModified,
        stop: function(event,ui) {renumber_table('#diagnosis_list')}
    }).disableSelection();



    $('table').on('click','.btn-delete',function() {
        tableID = '#' + $(this).closest('table').attr('id');
        r = confirm('Delete this item?');
        if(r) {
            $(this).closest('tr').remove();
            renumber_table(tableID);
            }
    });

});


function renumber_table(tableID) {
    $(tableID + " tr").each(function() {
        count = $(this).parent().children().index($(this)) + 1;
        $(this).find('.priority').html(count);
    });
}


</script>

<style type="text/css">
.ui-sortable tr {
    cursor:pointer;
}

.ui-sortable tr:hover {
    background:rgba(244,251,17,0.45);
}

</style>

</head>

<body>

<div id="content">

<h1>Sortable table</h1>


<button>Clone</button>

<div id="st">Here is the <div>
<div id="dvr"><div>
            <table class="table" id="diagnosis_list">
                <thead>
                    <tr><th>Priority</th><th>Name</th><th>Favorite fruit</th><th>Vegetarian?</th><th>&nbsp;</th></tr>
                </thead>
                <tbody>
             /***** here i need to maintain the sequence of the numbers **********************/ 
                    <tr><td class='priority'>1</td><td>George Washington</td><td>Apple</td><td>N</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
                    <tr><td class='priority'>2</td><td>John Adams</td><td>Pear</td><td>Y</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
                    <tr><td class='priority'>3</td><td>Thomas Jefferson</td><td>Banana</td><td>Y</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
                    <tr><td class='priority'>4</td><td>Ben Franklin</td><td>Kumquat</td><td>N</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
                    <tr><td class='priority'>5</td><td>Alexander Hamilton</td><td>Red grapes</td><td>N</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
                </tbody>
            </table>


</div>

</body>
</html>

我需要列优先级像它一样移动,需要列来维护数字序列,如1,2,3,4,5等,而不是像实际的那样移动所有表代码。

0 个答案:

没有答案