Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在数组中移动一组项?_Javascript_Jquery_Arrays_Sorting - Fatal编程技术网

Javascript 如何在数组中移动一组项?

Javascript 如何在数组中移动一组项?,javascript,jquery,arrays,sorting,Javascript,Jquery,Arrays,Sorting,我有一桌学生,按房间分类。每个房间都有学生,分成小组 我想通过单击“向上”或“向下”来更改组在房间中的位置。我在下面做了一次尝试,但它看起来很脏,不起作用。以下是: 我想要一个简短、干净、简单的解决方案 学生表: # Before moving "Group 2" down Id | Name | Age | Room | Group | Actions ----------------------------------------------- 5 | Student 5 |

我有一桌学生,按房间分类。每个房间都有学生,分成小组

我想通过单击“向上”或“向下”来更改组在房间中的位置。我在下面做了一次尝试,但它看起来很脏,不起作用。以下是:

我想要一个简短、干净、简单的解决方案

学生表:

# Before moving "Group 2" down

Id | Name      | Age | Room | Group | Actions
-----------------------------------------------
5  | Student 5 | 23  | 3    | 2     | UP | DOWN
6  | Student 6 | 27  | 3    | 2     | UP | DOWN // <- *Click*
1  | Student 1 | 29  | 5    | 1     | UP | DOWN
2  | Student 2 | 22  | 5    | 1     | UP | DOWN
3  | Student 3 | 21  | 5    | 3     | UP | DOWN
4  | Student 4 | 25  | 5    | 3     | UP | DOWN

# After moving "Group 2" down

Id | Name      | Age | Room | Group | Actions
-----------------------------------------------
1  | Student 1 | 29  | 5    | 1     | UP | DOWN
2  | Student 2 | 22  | 5    | 1     | UP | DOWN
5  | Student 5 | 23  | 3    | 2     | UP | DOWN // <- ID 5 and ID 6 moved down,
6  | Student 6 | 27  | 3    | 2     | UP | DOWN //    because both are in Group 2.
3  | Student 3 | 21  | 5    | 3     | UP | DOWN
4  | Student 4 | 25  | 5    | 3     | UP | DOWN
#向下移动“组2”之前
Id |姓名|年龄|房间|小组|行动
-----------------------------------------------
5 |学生5 | 23 | 3 | 2 |上|下

6 | Student 6 | 27 | 3 | 2 | UP | DOWN/我认为最好在数组中添加一个顺序索引,说明元素的顺序,而不是重新排列数组元素本身。

我认为最好在数组中添加一个顺序索引,说明元素的顺序,不重新排列数组元素本身。

正如您所要求的,移动组的新工作小提琴是
http://jsfiddle.net/acturbo/8nZUx/5/
:)

:

$(文档).ready(函数(){
/*构建HTML*/
函数构建(){
var students=新数组();
推送({顺序:1,身份证:1,姓名:“学生1”,房间号:5,年龄:29,组别:1});
推送({顺序:2,id:2,姓名:“学生2”,房间号:5,年龄:22,组别:1});
推送({顺序:3,身份证:3,姓名:“学生3”,房间号:5,年龄:21,组别:3});
推送({顺序:4,身份证:4,姓名:“学生4”,房间号:5,年龄:25,组别:3});
推送({顺序:5,身份证:5,姓名:“学生5”,房间号:3,年龄:23,组别:2});
推送({顺序:6,id:6,姓名:“学生6”,房间号:3,年龄:27,组别:2});
推送({顺序:7,id:7,姓名:“学生7”,房间号:5,年龄:44,组别:3});
/*按房间编号对学生进行分类*/
学生。排序(函数(a,b){
返回((a.room_idb.room_id)?1:0);
});
var html='';
html+='IdNameAgeRoomGroupActions';
for(var i=0;i
一种新的工作提琴,完全按照您的要求移动组,它是
http://jsfiddle.net/acturbo/8nZUx/5/
:)

:

$(文档).ready(函数(){
/*构建HTML*/
函数构建(){
var students=新数组();
推送({顺序:1,身份证:1,姓名:“学生1”,房间号:5,年龄:29,组别:1});
推送({顺序:2,id:2,姓名:“学生2”,房间号:5,年龄:22,组别:1});
推送({顺序:3,身份证:3,姓名:“学生3”,房间号:5,年龄:21,组别:3});
推送({顺序:4,身份证:4,姓名:“学生4”,房间号:5,年龄:25,组别:3});
推送({顺序:5,身份证:5,姓名:“学生5”,房间号:3,年龄:23,组别:2});
推送({顺序:6,id:6,姓名:“学生6”,房间号:3,年龄:27,组别:2});
推送({顺序:7,id:7,姓名:“学生7”,房间号:5,年龄:44,组别:3});
/*按房间编号对学生进行分类*/
学生。排序(函数(a,b){
返回((a.room_idb.room_id)?1:0);
});
var html='';
html+='IdNameAgeRoomGroupActions';
for(var i=0;i<script language="text/javascript">
    $(document).ready(function() {
        /* Build the HTML */
        function build() {
            var students = new Array();
            students.push({ id: 1, name: "Student 1", room_id: 5, age: 29, group: 1 });
            students.push({ id: 2, name: "Student 2", room_id: 5, age: 22, group: 1 });
            students.push({ id: 3, name: "Student 3", room_id: 5, age: 21, group: 3 });
            students.push({ id: 4, name: "Student 4", room_id: 5, age: 25, group: 3 });
            students.push({ id: 5, name: "Student 5", room_id: 3, age: 23, group: 2 });
            students.push({ id: 6, name: "Student 6", room_id: 3, age: 27, group: 2 });

            /* Sort students by room_id */
            students.sort(function(a, b) {
                return ((a.room_id < b.room_id) ? -1 : ((a.room_id > b.room_id) ? 1     : 0));
            });

            var html = '<table>';
            html += '<tr><th>Id</th><th>Name</th><th>Age</th><th>Room</th><th>Group</th><th>Actions</th></tr>';

            for (var i = 0; i < students.length; i++) {
                html += '<tr>';
                html +=     '<td>'+ students[i].id +'</td>';
                html +=     '<td>'+ students[i].name +'</td>';
                html +=     '<td>'+ students[i].age +'</td>';
                html +=     '<td>'+ students[i].room_id +'</td>';
                html +=     '<td>'+ students[i].group +'</td>';
                html +=     '<td><a href="#" class="move_up" rel="' +
                             students[i].group +
                             '">UP</a> | <a href="#" class="move_down" rel="' +
                             students[i].group +
                             '">DOWN</a></td>';
                html += '</tr>';
            }
            html += '</table>';

            $("#students").html(html);
        }

        /* Move group up */
        $(".move_up").live("click", function() {
            var group = $(this).attr("rel");
            alert("move up: " + group);

            /**
                What to do here?

                    Idea:
                        1. Build an array of the group items (to move up)
                        2. Build an array of the parent group items (to move down)
                        3. Re-build the students array, like:

                        var group = new Array();
                        // Collect all students of the group that has to move up

                        var parent_group = new Array();
                        // Collect all students of the parent group that has to
                        // move down or be switched with the selected group.

                        var students_tmp = new Array(); // New students array
                        var ids          = new Array(); // Existing ids
                        for (var i = 0; i < students.length; i++) {
                            if (students[i].group == group) {
                                // Do nothing, because students of this group
                                // will be added below.
                            } else if (students[i].group == parent_group) {
                                // Add group before parent group
                                for (var k = 0; k < group.length;     k++) {
                                    // Add, if not exists
                                    if (jQuery.inArray(group[k].id, ids) == -1) {
                                        students_tmp.push(group[k]); // Add student
                                        ids.push(group[k].id); // Add students id
                                    }
                                }

                                // Add parent group after group
                                for (var k = 0; k < parent_group.length; k++) {
                                    // Add, if not exists
                                    if (jQuery.inArray(parent_group[k].id, ids) == -1) {
                                        students_tmp.push(parent_group[k]); // Add student
                                        ids.push(parent_group[k].id); // Add students id
                                    }
                                }
                            } else {
                                // Add student if not in group or parent group
                                if (jQuery.inArray(students[i].id, ids) == -1) {
                                        students_tmp.push(students[i]);
                                    ids.push(students[i].id);
                                }
                            }
                        }
                        students = students_tmp;
                        build();
            */
        });

        /* Move group down */
        $(".move_down").live("click", function() {
            var group = $(this).attr("rel");
            alert("move down: " + group);

            /**
                What to do here?

                Idea:
                - Same as above (move_up), just reversed
            */
        });

        build();
    });
</script>

<div id="students">...</div>
$(document).ready(function() {
    /* Build the HTML */
    function build() {
        var students = new Array();
        students.push({ order: 1, id: 1, name: "Student 1", room_id: 5, age: 29, group: 1 });
        students.push({ order: 2, id: 2, name: "Student 2", room_id: 5, age: 22, group: 1 });
        students.push({ order: 3, id: 3, name: "Student 3", room_id: 5, age: 21, group: 3 });
        students.push({ order: 4, id: 4, name: "Student 4", room_id: 5, age: 25, group: 3 });
        students.push({ order: 5, id: 5, name: "Student 5", room_id: 3, age: 23, group: 2 });
        students.push({ order: 6, id: 6, name: "Student 6", room_id: 3, age: 27, group: 2 });
        students.push({ order: 7, id: 7, name: "Student 7", room_id: 5, age: 44, group: 3 });

        /* Sort students by room_id */
        students.sort(function(a, b) {
            return ((a.room_id < b.room_id) ? -1 : ((a.room_id > b.room_id) ? 1 : 0));
        });

        var html = '<table>';
        html += '<tr><th>Id</th><th>Name</th><th>Age</th><th>Room</th><th>Group</th><th>Actions</th></tr>';

        for (var i = 0; i < students.length; i++) {
            html += '<tr class="'+ students[i].group  +'">';  // Assign the groupid to the row as a class for easier managment.
            html +=     '<td>'+ students[i].id +'</td>';
            html +=     '<td>'+ students[i].name +'</td>';
            html +=     '<td>'+ students[i].age +'</td>';
            html +=     '<td>'+ students[i].room_id +'</td>';
            html +=     '<td>'+ students[i].group +'</td>';
            html +=     '<td class="order-col" data-order="' +
                        students[i].order +
                        '"><a href="#" class="move_up">UP</a> | <a href="#" class="move_down">DOWN</a></td>';

            html += '</tr>';
        }
        html += '</table>';

        $("#students").html(html);
    }

    /* Move group up */
    $(".move_up").live("click", function() {

        var currentRow =  $(this).parent().parent();
        var currentGroupClass  =  $(currentRow).attr("class");

        var prevRow = $(currentRow).prev();
        var prevGroupClass = $(prevRow).attr("class");

        if (prevGroupClass == undefined){
            return;
        }

        // Find previous group
        while (prevGroupClass == currentGroupClass){
            prevRow = $(prevRow).prev();
            prevGroupClass =  $(prevRow).attr("class");
        }
        // Move the group
        $("."+currentGroupClass).insertBefore(  $("."+prevGroupClass).first() );
    });

    /* Move group down */
    $(".move_down").live("click", function() {
        var currentRow =  $(this).parent().parent();
        var currentGroupClass  =  $(currentRow).attr("class");

        var nextRow = $(currentRow).next();
        var nextGroupClass = $(nextRow).attr("class");

        if (nextGroupClass == undefined){
            return;
        }

        // Find next group
        while (nextGroupClass == currentGroupClass){
            nextRow = $(nextRow).next();
            nextGroupClass =  $(nextRow).attr("class");
        }
        // Move the group
        $("."+currentGroupClass).insertAfter(  $("."+nextGroupClass).last() );
    });
    console.clear();
    build();
});
<div id="students">Students Table
    <table>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Age</th>
            <th>Room</th>
            <th>Group</th>
            <th>Actions</th>
        </tr>
    </table>
</div>
.rowme {
    background-color:pink;
}
.movers {
    background-color:lime;
}
var students = [{
    id: 1,
    name: "Student 1",
    room_id: 5,
    age: 29,
    classgroup: 1
}, {
    id: 2,
    name: "Student 2",
    room_id: 5,
    age: 22,
    classgroup: 1
}, {

    id: 3,
    name: "Student 3",
    room_id: 5,
    age: 21,
    classgroup: 3
}, {
    id: 4,
    name: "Student 4",
    room_id: 5,
    age: 25,
    classgroup: 3
}, {
    id: 5,
    name: "Student 5",
    room_id: 3,
    age: 23,
    classgroup: 2
}, {
    id: 6,
    name: "Student 6",
    room_id: 3,
    age: 27,
    classgroup: 2
}];

function addRows() {
    $("#students").find('table tr.student').each(function (i) {
        var cgroup = students[i] ? students[i].classgroup : i + ":notfound";
        $(this).data('cgroup', cgroup);
    });
}
/* build the HTML */
function build() {
    /* sort students by room_id */
    students.sort(function (a, b) {
        return ((a.room_id < b.room_id) ? -1 : ((a.room_id > b.room_id) ? 1 : 0));
    });

    var ahtml = '';
    var i = 0;
    for (i = 0; i < students.length; i++) {
        ahtml += '<tr class="student">';
        ahtml += '<td>' + students[i].id + '</td>';
        ahtml += '<td>' + students[i].name + ":" + i + '</td>';
        ahtml += '<td>' + students[i].age + '</td>';
        ahtml += '<td>' + students[i].room_id + '</td>';
        ahtml += '<td>' + students[i].classgroup + '</td>';
        ahtml += '<td><a href="#" class="move_up" rel="' + students[i].classgroup + '">UP</a> | <a href="#" class="move_down" rel="' + students[i].classgroup + '">DOWN</a></td>';
        ahtml += '</tr>';
    }
    //html += '</table>';
    $("#students").find('table').append(ahtml);
    addRows();
};
$(document).ready(function () {
    build();
    /* move group up */
    $('#students').on('click', '.move_up', function () {
        $('.rowme, .movers').removeClass('rowme movers');
        var row = $(this).parents('tr.student');
        var mygroup = row.data("cgroup");
        $('#students').find('table tr.student').filter(function () {
            return $(this).data('cgroup') == mygroup;
        }).addClass('movers').first().prev('.student').addClass('rowme');
        var moveGroup = $('.rowme').data('cgroup');
        $('#students').find('table tr.student').filter(function () {
            return $(this).data('cgroup') == moveGroup;
        }).addClass('rowme');
        $('.movers').insertBefore($('.rowme:first'));
    });
    /* move group down */
    $('#students').on('click', ".move_down", function () {
         $('.rowme, .movers').removeClass('rowme movers');
        var row = $(this).parents('tr.student');
        var mygroup = row.data("cgroup");
        $('#students').find('table tr.student').filter(function () {
            return $(this).data('cgroup') == mygroup;
        }).addClass('movers').last().next('.student').addClass('rowme');
        var moveGroup = $('.rowme').data('cgroup');
        $('#students').find('table tr.student').filter(function () {
            return $(this).data('cgroup') == moveGroup;
        }).addClass('rowme');
        $('.movers').insertAfter($('.rowme:last'));
    });
});