2011-01-09 11 views
2

私はカレンダーがあり、ユーザーは日付を選択できる必要があります(基本的に表のセルです)。問題は選択のためのjQuery UIのサポートは少しスケッチです。特に、ユーザーがCTRLキーを使用して日付を選択または選択解除すると、選択された要素の合計リストを選択「停止」イベントで非常に簡単に取得できないようです。ここで私は現在やっているものですが、それは非常に非効率です:jQuery UIの選択 - 非効率のヘルプ

global.selected = []; 


$('#calendar').selectable({filter: 'td', 

selected: function(event, ui) 
    { 
    for (var a = 0, id = +ui.selected.children[0].id, found = false; a < global.selected.length; a++) 
     { 
     if (global.selected[a] == id) 
      { 
      found = true; 
      break; 
      } 
     } 
    !found && global.selected.push(id); 
    }, 

unselected: function(event, ui) 
    { 
    for (var a = 0, id = +ui.unselected.children[0].id; a < global.selected.length; a++) 
     { 
     if (global.selected[a] == id) 
      { 
      global.selected.splice(a, 1); 
      break; 
      } 
     } 
    }, 

stop: function(event, ui) 
    { 
    console.log(global.selected); 
    } 
} 

答えて

1

私はjQueryのに新しいブランドだけど、私はそれをこのようにアプローチし、一緒にすべてのグローバルを避けるだろう。

$('#calendar').selectable({ 
    filter: 'td', 
    stop: function(event, ui) { 
     var selected = $.map($('#calendar > tbody > tr > td.ui-selected'),function(el) { 
      return el.id; 
     }); 
     console.log(selected); 
    } 
}); 
+0

スマートなアイデア。どうもありがとう! – Nick

関連する問題