<td> <span data-id="<%= product.id %>" data-sort="<%= product.sort_in_shop %>" class="sort_in_shop"><%= product.sort_in_shop %></span> </td>
相应的js
// 排序
$('.sort_in_shop').each(function () {
const productId = $(this).attr('data-id')
const productSort = $(this).attr('data-sort')
$(this).click(function () {
const textBox = document.createElement('INPUT')
textBox.type = 'text'
textBox.value = productSort
$(this).html(textBox)
$(this).find('input').focus()
textBox.onblur = function () {
const newSort = textBox.value
const ajaxOptions = {
method: 'POST',
data: {
id: productId,
sort: newSort
}
}
$.ajax('/api//', ajaxOptions).done((data) => {
$(this).parent().html(data.sort)
}).fail(() => {
// window.location.href = '/login'
})
}
})
})
def change_shop_sort
find_product
sort = @product.sort_in_shop
unless params[:sort].blank?
@product.sort_in_shop = params[:sort]
@product.save
sort = params[:sort]
end
render json: {
sort: sort
}
end