Rails 4 アプリケーションで、Railscast 147 チュートリアルを少し変更して、並べ替え可能なネストされたフォームをセットアップしましたが、すべて正常に動作します。私のプロジェクトでは、チェックボックスを含む、ネストされたリソースのフィールドでそれぞれをli
ラップします。fields_for
私の問題は、チェックボックスを含むの位置に応じてチェックボックス情報を処理したいということですli
。さらに、入力の 1 つがチェックされた場合、これは他のフィールドにも影響するはずです。これを行うための jquery 関数を作成し (主に and を介しaddClass
てremoveClass
)、この関数の呼び出しをソート可能およびイベント ハンドラーに配置しました。
process_components = (o) ->
$components = $(o).find('li')
// other stuff
$components.removeClass("parent_component child_component")
$components.find('input.is_child').each ->
firstcheck = $(this).prop('checked')
secondcheck = $(this).closest('li').next().find('input.is_child').prop('checked')
if firstcheck is true
$(this).closest('li').addClass('child_component')
if firstcheck is false and secondcheck is true
$(this).closest('li').addClass('parent_component')
$('#components-list').sortable
axis: 'y'
items: "> li.component"
update: ->
$.post($(this).data('update-url'), $(this).sortable('serialize'))
process_components $(this)
$('#components-list').on 'click', ':checkbox', (event) ->
process_components '#components-list'
私の問題は、リストの要素を手動で数回並べ替えた後にのみ、これが適切に機能することです (要素は、リスト内の位置が変更された後にのみチェックボックスの状態に応答します)。私はこことGoogleを見回しましたが、なぜこれが起こるのかについての手がかりを見つけることができませんでした. Jquery と Jquery UI の処理の違いと何か関係があるのaddClass
でしょうか? これを修正する方法についてのアイデアはありますか? ありがとう