【发布时间】:2014-09-18 15:30:07
【问题描述】:
我正在使用 HTML5 jQuery 可排序库。不是 jQuery UI 可排序的,但这里是 http://farhadi.ir/projects/html5sortable/
我过去在很多项目中都使用过它,通常我使用 AJAX 将排序顺序作为 ID 字符串保存到数据库字段中。
在我当前的项目中,我需要做一些完全不同的事情。我这次没有使用 AJAX 来保存订单。
基本上,我在表单编辑屏幕上运行 Sortable 库,该屏幕将包含一个 DIV 列表,这些 div 内将是表单字段。页面底部是一个保存按钮,用于提交表单以保存页面上的所有数据。所以我想将每个 DIV 的排序顺序存储到每个项目的隐藏表单字段中。
我已经在 CodePen.io 上设置了一个演示程序http://codepen.io/jasondavis/pen/ztirw?editors=101
我可以使用一些帮助来更新每个 Div 下的表单,以便在每次 Drop 发生时使用排序顺序更新字段。因此,我不想以正确的排序顺序保存一串 ID,而是希望将 Drop 事件中的每条记录更新到具有当前排序位置的表单中。
有什么帮助吗?
演示 HTML 结构如下所示...
<div id="project_tasks" class="tasks_block sortable">
<div id="task_13" class="task_row">
<span class="handle"></span>
<input name="taskid_13" id="taskid_13" size="15" type="text" value="taskID 1">
<input name="projectid_13" id="projectid_13" size="15" type="text" value="917fdb60-96d7-346f-10b3-54175c9a2f34">
Sort Order: <input name="sort_order_19" id="sort_order_19" size="15" type="text" value="1">
<br style="clear:both;">
</div>
<div id="task_14" class="task_row">
<span class="handle"></span>
<input name="taskid_14" id="taskid_14" size="15" type="text" value="taskID 2">
<input name="projectid_14" id="projectid_14" size="15" type="text" value="917fdb60-96d7-346f-10b3-54175c9a2f34">
Sort Order: <input name="sort_order_19" id="sort_order_19" size="15" type="text" value="2">
<br style="clear:both;">
</div>
<div id="task_15" class="task_row">
<span class="handle"></span>
<input name="taskid_15" id="taskid_15" size="15" type="text" value="taskID 3">
<input name="projectid_15" id="projectid_15" size="15" type="text" value="917fdb60-96d7-346f-10b3-54175c9a2f34">
Sort Order: <input name="sort_order_19" id="sort_order_19" size="15" type="text" value="3">
<br style="clear:both;">
</div>
<div id="task_15" class="task_row taskheading">
<span class="handle"></span>
<h2>List Heading 1</h2>
Sort Order: <input name="sort_order_19" id="sort_order_19" size="15" type="text" value="4">
<br style="clear:both;">
</div>
<div id="task_16" class="task_row">
<span class="handle"></span>
<input name="taskid_16" id="taskid_16" size="15" type="text" value="taskID 4">
<input name="projectid_16" id="projectid_16" size="15" type="text" value="917fdb60-96d7-346f-10b3-54175c9a2f34">
Sort Order: <input name="sort_order_19" id="sort_order_19" size="15" type="text" value="5">
<br style="clear:both;">
</div>
</div>
一个小的 JavaScript 来开始事情......
$( document ).ready(function() {
$('#project_tasks').sortable({
handle: '.handle',
onStartDrag: function() {},
onEndDrag: function() {},
onChangeOrder: function() {}
}).bind('sortupdate', function() {
$('.sortable div').each(function() {
// Update a HIDDEN Field under each DIV with the current sort order
// So when my Form is submitted/saved, it can save the sort order for
// each record into the database.
});
});
});
【问题讨论】:
标签: javascript jquery jquery-ui-sortable