2013-05-08 47 views
0

我有一張學生表,按房間排序。在每個房間都是學生,分成小組。如何移動數組中的一組項目?

我想通過點擊「向上」或「向下」來改變房間內的組的位置。我在下面嘗試了一下,但它看起來很骯髒,並且不起作用。這裏的jsFiddlehttp://jsfiddle.net/vHhxV/

我想有一個簡短,乾淨,簡單的解決方案。

學生的表:

# 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 

我嘗試(同:http://jsfiddle.net/vHhxV/

<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> 
+1

當您在每行上都有操作項時,您的用戶界面將會混淆不清,無法在組級別上移動。 – 2013-05-08 14:15:02

+0

你說得對。這只是一個虛擬的,但我稍後會改變可用性。謝謝你的建議! – 2013-05-08 18:08:04

回答

1

這將滿足您定義的要求。它不考慮房間(這是另一個問題),而只是將排在一組爲您指定的上漲或下跌,如果有行高於或低於可以影響的位置。

注:我花了一些自由與您的標記等,以使我的工作更容易,添加類等和代碼 - 那疲憊不堪陣推的東西,只需使用一個數組常量。

該方案突出了行移除/考慮​​一些CSS,而它的工作原理 - 刪除CSS,如果你不希望它:)

測試小提琴:http://jsfiddle.net/YpcBt/

MARKUP:

<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> 

CSS:

.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')); 
    }); 
}); 
+0

就是這樣!非常感謝! :-) – 2013-05-09 08:25:52

2

我認爲這將是最好添加一個排序索引,其中列明瞭陣列元素的順序,而不是重新排列數組元素本身。

+0

感謝您的快速回復!你有一個例子或一些關鍵字,我可以谷歌? – 2013-05-08 12:56:41

+1

我的意思是,如果你添加額外的順序列,只需要添加的元素融入到基於該值的表。 var list = new array(); 列表(學生)[0] .order] =學生[0] 或類似的 – Tralli 2013-05-08 12:58:38

0

一個新的工作小提琴移動組,完全按照自己的要求,是http://jsfiddle.net/acturbo/8nZUx/5/ :)

jQuery

$(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(); 
}); 
+0

感謝您的答覆東西,但遺憾的是它並沒有解決這個問題,因爲我不想只是切換兩個學生。我想改變每個組的順序。例如: 我在我的問題更新學生的桌子上面。請看一下。:-) – 2013-05-08 14:06:25

+1

您可以使用此代碼和邏輯輕鬆管理組。我已經爲您提供了完整解決方案的基礎。您收到的其他答案僅提供了詞語,但我的答案已被拒絕投票,而其他「答案」已投票通過? – carrabino 2013-05-08 19:08:39

+0

不幸的是,我知道如何開發基礎解決方案,您提供了我。我很感激,但我不知道如何管理這些組織。我想上移/下移項目組,而不是單個項目。 我對每個答案都投了贊成票。我想知道爲什麼有人把它投下來。我的問題也被低估了。 – 2013-05-08 19:17:46

相關問題