基本上,您需要将<option>
s 从 target中拉出<select>
,将选项添加到该数组,手动对其进行排序,然后将 target <select>
s <option>
s 替换为排序列表。jQuery 对这类事情没有多大帮助,但低级 DOM 版本并不是非常困难,如下所示:
exc = $('#geo_country_exclude')[0]
inc = $('#geo_country_include')[0]
by_option_value = (a, b) ->
return +1 if(a.value > b.value)
return -1 if(a.value < b.value)
return 0
mv = (from, to) ->
# to.options looks like an array and smells like an array but
# it isn't an array so we employ the standard arrayification trick.
opts = [].slice.call(to.options)
# Move the <option>
opts.push(from.options[from.selectedIndex])
from.remove(from.selectedIndex)
# Sort and replace.
to.remove(0) for i in [ 0 ... to.options.length ]
to.add(opt) for opt in opts.sort(by_option_value)
$('#add' ).click (event) -> mv(exc, inc)
$('#remove').click (event) -> mv(inc, exc)
您可能需要调整by_option_value
比较功能,并且您可能希望在<select>
移动后调整 s 中的选定项目,但您可以轻松地对其进行排序。
演示:http: //jsfiddle.net/ambiguous/qRw3K/
如果您需要处理多选,那么只需稍作修改即可mv
:
mv = (from, to) ->
opts = [].slice.call(to.options)
while from.selectedIndex >= 0
opts.push(from.options[from.selectedIndex])
from.remove(from.selectedIndex)
to.remove(0) for i in [ 0 ... to.options.length ]
to.add(opt) for opt in opts.sort(by_option_value)
您只需要将opts.push
and包裹from.remove
在一个while
小循环中。
演示:http: //jsfiddle.net/ambiguous/qRw3K/1/