Created
June 1, 2015 11:25
-
-
Save YoshitsuguFujii/4004c2d8d3a9928ecd57 to your computer and use it in GitHub Desktop.
複数選択失敗作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MultiSelectWithoutCtrl | |
constructor: (_this, options)-> | |
@id = null | |
@name = options["name"] | |
@select = _this | |
@checkboxes = $("<div class='hidden_multi_select'></div>") | |
@select.after(@checkboxes) | |
@selected() | |
selected: -> | |
@select.on "click" , (event) => | |
event.preventDefault() | |
@id = $(event.currentTarget).val() | |
if @is_exist_id() | |
@remove_checkbox() | |
else | |
@add_checkbox() | |
@redraw() | |
add_checkbox: -> | |
@checkboxes.append(@make_checkbox()) | |
remove_checkbox: -> | |
@target_element().remove() | |
make_checkbox: -> | |
"<input name='#{@name}' type='checkbox' value='#{@id}' checked='checked' class='#{@make_class_name()}'>" | |
make_class_name: -> | |
"#{@name.replace("[", "_").replace("]", "_")}_#{@id}" | |
target_element: -> | |
$(".#{@make_class_name()}") | |
is_exist_id: -> | |
@target_element().exists() | |
redraw: -> | |
_.each @select.find("option"), (option) -> | |
@id = option.value | |
if @is_exist_id() | |
option.selected = true | |
else | |
option.selected = false | |
, @ | |
$.fn.multiSelectWithoutCtrl = (options) -> | |
new MultiSelectWithoutCtrl(@, options) | |
$(".user_id").multiSelectWithoutCtrl({name: 'purchase[user_id]'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment