1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
(function($){
var originalPos = null;
var fixHelperDimensions = function(e, tr) {
originalPos = tr.prevAll().length;
var $originals = tr.children();
var $helper = tr.clone();
$helper.children().each(function(index)
{
$(this).width($originals.eq(index).width()+1).height($originals.eq(index).height())
.css({
"border-bottom":"1px solid #ddd"
});
});
return $helper.css("border-right","1px solid #ddd");
};
/**
* Returns the key values of the currently checked rows.
* @param id string the ID of the grid view container
* @param action string the action URL for save sortable rows
* @param column_id string the ID of the column
* @param data string the custom POST data
* @return array the key values of the currently checked rows.
*/
$.fn.yiiGridView.sortable = function (id, action, callback, data)
{
if (data == null)
data = {};
var grid = $('#'+id) ;
$("tbody", grid).sortable({
helper: fixHelperDimensions,
update: function(e,ui){
// update keys
var pos = $(ui.item).prevAll().length;
if(originalPos !== null && originalPos != pos)
{
var keys = grid.children(".keys").children("span");
var key = keys.eq(originalPos);
var sort = [];//sort number values from to
keys.each(function(i) {
sort[i] = $(this).attr('data-order');
});
if(originalPos < pos)
{
keys.eq(pos).after(key);
}
if(originalPos > pos)
{
keys.eq(pos).before(key);
}
originalPos = null;
}
var sortOrder = {};
keys = grid.children(".keys").children("span");
keys.each(function(i) {
$(this).attr('data-order', sort[i]);
sortOrder[$(this).text()] = sort[i];
});
data["sortOrder"] = sortOrder;
if(action.length)
{
$.fn.yiiGridView.update(id,
{
type:'POST',
url:action,
data:data,
success:function(){
grid.removeClass('grid-view-loading');
},
complete:function(){
if($.isFunction(callback))
{
callback(sortOrder);
}
}
});
}
else if($.isFunction(callback))
{
callback(sortOrder);
}
}
}).disableSelection();
};
})(jQuery);