-1
我需要一個拖放效果,只需找到jQuery可排序的最可行和最簡單的解決方案,但我想在重新排序後保存位置。使用php/sqlite我可以做到這一點,但由於我使用框架瓶解決方案將不得不在Python中。我來到這個代碼在這裏搜索jQuery ui可排序 - 使用Python/Flask/SQLite保存到數據庫
HTML:
$(function() {
var $sortables = $("#sortMe").sortable({
stop: function() {
var sortedItems = $sortables.sortable("toArray");
}
});
});
潘岳:
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
class Sortable(db.Model):
__tablename__ = 'sortables'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
data = db.Column(db.String)
def __init__(self, data):
self.data = data
@app.route("/saveorder", methods=['GET', 'POST'])
def save_order():
if request.method == "POST":
編輯。
現在,我得到這個
HTML:
$(function() {
$('#sortMe').sortable({
update: function(event, ui) {
var postData = $(this).sortable('serialize');
console.log(postData);
$.ajax({
url: '/saveorder',
type: 'POST',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({list: postData}),
success: function (ret) {
alert('JSON posted: ' + JSON.stringify(ret));
}
});
}
});
});
潘岳:
@app.route("/saveorder", methods=['GET', 'POST'])
def saveorder():
json = request.json
print(json)
return jsonify(json)