From c51dd81d2bd4e54bcf54b857080bf17f25765f75 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Thu, 7 Nov 2013 19:14:53 +0800 Subject: [PATCH] fixed page setting select all bug --- app/assets/javascripts/lib/items/items.js.erb | 38 +++++++++++++------ app/controllers/admin/pages_controller.rb | 6 +++ app/views/admin/items/_form_page.html.erb | 6 ++- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/app/assets/javascripts/lib/items/items.js.erb b/app/assets/javascripts/lib/items/items.js.erb index c88c95dd..4065ece6 100644 --- a/app/assets/javascripts/lib/items/items.js.erb +++ b/app/assets/javascripts/lib/items/items.js.erb @@ -189,8 +189,11 @@ function pageSetting(id, edit, ps) { if(_arr !== -1) { $pageCategory.find('input[type="checkbox"]').eq(index).prop('checked', true); } - var $all = $pageCategory.siblings('.checkbox').children('.select_all') - _selectData.module.category.length > 1 && _selectData.module.category.length == index + 1 ? $all.prop('checked', true) : $all.prop('checked', false); + var $all = $pageCategory.siblings('.checkbox').children('.select_all'), + $hiddenInput = $pageCategory.siblings('input[type="hidden"]'); + _selectData.module.category.length ? $hiddenInput.removeAttr('name') : $hiddenInput.attr('name', 'page[category][]'); + _selectData.module.category.length == index + 1 ? $all.prop('checked', true) : $all.prop('checked', false); + console.log($hiddenInput, _selectData.module.category) }); $pageCategoryGroup.show(); } @@ -201,8 +204,10 @@ function pageSetting(id, edit, ps) { if(_arr !== -1) { $pageTags.find('input[type="checkbox"]').eq(index).prop('checked', true); } - var $all = $pageTags.siblings('.checkbox').children('.select_all') - _selectData.module.tags.length > 1 && _selectData.module.tags.length == index + 1 ? $all.prop('checked', true) : $all.prop('checked', false); + var $all = $pageTags.siblings('.checkbox').children('.select_all'), + $hiddenInput = $pageTags.siblings('input[type="hidden"]'); + _selectData.module.tags.length ? $hiddenInput.removeAttr('name') : $hiddenInput.attr('name', 'page[tag][]'); + _selectData.module.tags.length == index + 1 ? $all.prop('checked', true) : $all.prop('checked', false); }); $pageTagsGroup.show(); } @@ -397,6 +402,7 @@ $(function() { if(event.target.id == 'page_module_app_id') { $pageCategory.empty(); + var $hiddenInputA = $pageCategory.siblings('input[type="hidden"]'); if(val.category.length > 0) { $.each(val.category, function(index, val) { if(val !== 0) { @@ -406,14 +412,17 @@ $(function() { $pageCategory.find('input[type="checkbox"]').eq(index).prop('checked', true); } var $all = $pageCategory.siblings('.checkbox').children('.select_all'); - _boolean && _val == _select.main && _select.category.length > 1 && _select.category.length == index + 1 ? $all.prop('checked', true) : $all.prop('checked', false); + _val == _select.main && _select.category.length ? $hiddenInputA.removeAttr('name') : $hiddenInputA.attr('name', 'page[category][]'); + _boolean && _val == _select.main && _select.category.length == index + 1 ? $all.prop('checked', true) : $all.prop('checked', false); } }); - $pageCategoryGroup.show(); + $pageCategoryGroup.slideDown(300); } else { - $pageCategoryGroup.hide(); + $pageCategoryGroup.slideUp(300); + $hiddenInputA.removeAttr('name'); } $pageTags.empty(); + var $hiddenInputB = $pageTags.siblings('input[type="hidden"]'); if(val.tags.length > 0) { $.each(val.tags, function(index, val) { if(val !== 0) { @@ -423,12 +432,14 @@ $(function() { $pageTags.find('input[type="checkbox"]').eq(index).prop('checked', true); } var $all = $pageTags.siblings('.checkbox').children('.select_all'); - _boolean && _val == _select.main && _select.tags.length > 1 && _select.tags.length == index + 1 ? $all.prop('checked', true) : $all.prop('checked', false); + _val == _select.main && _select.tags.length ? $hiddenInputB.removeAttr('name') : $hiddenInputB.attr('name', 'page[tag][]'); + _boolean && _val == _select.main && _select.tags.length == index + 1 ? $all.prop('checked', true) : $all.prop('checked', false); } }); - $pageTagsGroup.show(); + $pageTagsGroup.slideDown(300); } else { - $pageTagsGroup.hide(); + $pageTagsGroup.slideUp(300); + $hiddenInputB.removeAttr('name'); } } return false; @@ -461,8 +472,10 @@ $(function() { if($checked.hasClass('select_all')) { if($checked.prop('checked')) { $checked.closest('.checkbox').siblings('.groups').find('input[type="checkbox"]').prop('checked', true); + $checked.closest('.checkbox').siblings('input[type="hidden"]').removeAttr('name'); } else { $checked.closest('.checkbox').siblings('.groups').find('input[type="checkbox"]').prop('checked', false); + $checked.closest('.checkbox').siblings('input[type="hidden"]').attr('name', 'page['+$checked.closest('.checkbox-groups').data('for')+'][]'); } } else if($checked.hasClass('lang-enable')) { var checkboxes = $checked.closest('.groups').find('input[type="checkbox"]'); @@ -483,10 +496,11 @@ $(function() { var _l = $checked.closest('.groups').find('input[type="checkbox"]').length, _c = $checked.closest('.groups').find('input[type="checkbox"]:checked').length; if(_l == _c) { - $checked.closest('.groups').siblings('.checkbox').find('.select_all').prop('checked', true) + $checked.closest('.groups').siblings('.checkbox').find('.select_all').prop('checked', true); } else { - $checked.closest('.groups').siblings('.checkbox').find('.select_all').prop('checked', false) + $checked.closest('.groups').siblings('.checkbox').find('.select_all').prop('checked', false); }; + _c == 0 ? $checked.closest('.groups').siblings('input[type="hidden"]').attr('name', 'page['+$checked.closest('.checkbox-groups').data('for')+'][]') : $checked.closest('.groups').siblings('input[type="hidden"]').removeAttr('name'); } return false; } else if($checked.attr('type') == 'radio' && !$(this).closest('div').hasClass('active-mune')) { diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index c515655e..70f8239e 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -21,6 +21,12 @@ class Admin::PagesController < Admin::ItemsController end def update + if params[:page][:category][0] == "" + params[:page][:category] = [] + end + if params[:page][:tag][0] == "" + params[:page][:tag] = [] + end @item = Page.find(params[:id]) if @item.module_app && @item.module_app.key == 'page_content' && @item.page_contexts.blank? @item.page_contexts.build(:create_user_id => current_user.id, :update_user_id => current_user.id ) diff --git a/app/views/admin/items/_form_page.html.erb b/app/views/admin/items/_form_page.html.erb index 77be349c..8789ba88 100644 --- a/app/views/admin/items/_form_page.html.erb +++ b/app/views/admin/items/_form_page.html.erb @@ -28,20 +28,22 @@ <%= select_tag "page[app_frontend_url]", nil, class: "input-xlarge change" %> -
+
<%= f.label :categories, t(:categories) %> <%= content_tag :label, class: "checkbox" do %> <%= check_box_tag nil, nil, true, class: "select_all" %> <%= t(:all) %> <% end %>
+
-
+
<%= f.label :tags, t(:tags) %> <%= content_tag :label, class: "checkbox" do %> <%= check_box_tag nil, nil, true, class: "select_all" %> <%= t(:all) %> <% end %>
+