问题
I'm using the jQuery-UI Sortable Connected Lists. I'm saving the order of the connected lists to a Rails server.
My approach is to grab the list ID, column ID and index position of each list item. I want to then wrap this into an object that can be passed as a parameter back to the Rails Controller to be saved into the database. So ideally i'm looking to format the parameter like this: Parameters: {"Activity"=>[{id:1,column:2,position:1},{id:2,column:2,position:2} ,...]}
How do I properly format my parameters to be passed in this Ajax POST request?
Right now, with the approach below, I'm passing on Parameters: {"undefined"=>""}
This is my current jQuery code (Coffeescript) which doesn't work:
jQuery ->
$('[id*="day"]').sortable(
connectWith: ".day"
placeholder: "ui-state-highlight"
update: (event, ui) ->
neworder = new Array()
$('[id*="day"] > li').each ->
column = $(this).attr("id")
index = ui.item.index() + 1
id = $("#" + column + " li:nth-child(" + index + ") ").attr('id')
passObject={}
passObject.id = id
passObject.column = column
passObject.index = index
neworder.push(passObject)
alert neworder
$.ajax
url: "sort"
type: "POST"
data: neworder
).disableSelection()
My apologies because this seems like a really amateur question but I'm just getting started with programming jQuery and Javascript.
回答1:
Assuming that all your selectors are correct, then you should be able to do this:
$.ajax
url: "sort"
type: "POST"
data: { Activity: JSON.stringify(neworder) }
You can also simplify the rest of your code:
update: (event, ui) ->
neworder = [ ]
$('[id*="day"] > li').each ->
# 'this' should be the DOM element so just read off the 'id' attribute,
# you don't need to waste time building a jQuery object just to get an
# 'id' attribute.
column = @id
index = ui.item.index() + 1
# String interpolation to skip the string addition noise. You already
# have a jQuery object here so there's nothing wrong with using
# attr('id') here.
id = $("##{column} li:nth-child(#{index})").attr('id')
# You don't need 'passObject' here, just use an object literal and push
# it onto neworder all in one go.
neworder.push(
id: id
column: column
index: index
)
$.ajax
url: "sort"
type: "POST"
data: { Activity: JSON.stringify(neworder) }
Or even more compact (if you like that sort of thing):
update: (event, ui) ->
neworder = [ ]
$('[id*="day"] > li').each ->
index = ui.item.index() + 1
neworder.push(
id: $("##{column} li:nth-child(#{index})").attr('id')
column: @id
index: index
)
$.ajax
url: "sort"
type: "POST"
data: { Activity: JSON.stringify(neworder) }
Then you can JSON.parse(params[:Activity])
to unpack it in your controller.
After a bit of discussion in the comments, I think your index
values are wrong. Doing this:
index = ui.item.index() + 1
will give you the same index
for each <li>
. You probably want something more like this:
$lis = $('[id*="day"] > li')
$lis.each ->
index = $lis.index(@)
#...
That should give you the index of the current <li>
(@
) within the collection of <li>
s that you're iterating over. Alternatively, you could use the arguments that each passes to the iterator function:
$('[id*="day"] > li').each (index, el) ->
# Just use `index` as-is in here...
回答2:
This is my jQuery code (in Coffeescript):
jQuery ->
$('[id*="day"]').sortable(
connectWith: ".day"
placeholder: "ui-state-highlight"
update: (event, ui) ->
neworder = new Array()
$(this).children().each ->
column = $(this).parent().attr("id").match(/\d+/)[0]
id = $(this).attr("id").match(/\d+/)[0]
neworder.push(
id: id
column: column
)
alert neworder
$.ajax
url: "sort"
type: "POST"
data: { Activity: JSON.stringify(neworder) }
).disableSelection()
How I parse the AJAX call back to the Rails Controller:
def sort
JSON.parse(params[:Activity]).each_with_index do |x, index|
idx = x["id"]
positionx = index+1
column = x["column"]
activity = Activity.find(idx)
activity.update_attributes(:position => positionx, :day_id => column)
end
render nothing: true
end
Back in my Model, I order "Activities" association by it's Position.
has_many :activities, :order => 'position'
Thanks so much for your help @muistooshort
来源:https://stackoverflow.com/questions/13214454/formatting-parameters-for-ajax-post-request-to-rails-controller-for-jquery-ui