Skip to content

Instantly share code, notes, and snippets.

@YoshitsuguFujii
Created June 1, 2015 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YoshitsuguFujii/4004c2d8d3a9928ecd57 to your computer and use it in GitHub Desktop.
Save YoshitsuguFujii/4004c2d8d3a9928ecd57 to your computer and use it in GitHub Desktop.
複数選択失敗作
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