在jQuery上更新Div隐藏字段可排序拖放更新
Update Div Hidden Field on jQuery Sortable Drag and Drop Update
我使用的是HTML5 jQuery可排序库。不是jQuery UI可排序,而是这里的这个http://farhadi.ir/projects/html5sortable/
我在过去的许多项目中都使用过它,通常我使用AJAX将排序顺序作为一个ID字符串保存到数据库字段中。
在我目前的项目中,我需要做完全不同的事情。这次我没有使用AJAX来保存订单。
基本上,我有一个在Form编辑屏幕上运行的Sortable库,它将有一个DIV的列表,在这些DIV的内部将是表单字段。页面底部有一个保存按钮,用于提交表单以保存页面上的所有数据。因此,我想将每个DIV的排序顺序存储到每个项的隐藏表单字段中。
我在这里设置了一个在CodePen.io上使用的演示http://codepen.io/jasondavis/pen/ztirw?editors=101
我可以使用一些帮助来更新每个Div下的Form,以在每次发生Drop时使用排序顺序更新字段。因此,我不想以正确的排序顺序保存一个ID字符串,而是想将Drop事件中的每个记录更新到具有当前排序位置的Form文件中。
请帮忙吗?
演示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.
});
});
});
好的-开始吧:
http://codepen.io/anon/pen/IEKvA
$('.sortable div').each(function(idx) {
var inputField = $(this).find("[id^='sort_order']");
$(inputField).val(idx);
});
我们的想法是,每次删除项目时,都要遍历div,找到以id sort_order开头的所有输入字段,并相应地设置索引。
相关文章:
- 如何在动态表更新后刷新整个表排序缓存
- 可拖动和可排序项目编号在更改时更新
- 更新标签数组时如何对其进行排序
- 更新“项目”选项以进行排序
- 使用一次更新对有序列表中的元素重新排序
- MongoDB使用排序和切片更新嵌套数组,不会对代码进行排序
- jQuery - 排序后更新可排序列表
- React Native ListView 不会在排序或检索新数据后更新
- 从服务器更新的 meteor 集合,不会在客户端上被动排序
- D3 - 为什么此排序函数不更新其选择中的所有元素
- 使用 AJAX 回调更新挖空可观察数组会生成随机排序的结果
- knockoutjs ObservableArrays 和排序函数:UI 未更新
- 如何在主干中对列表进行排序时更新模型的顺序属性
- jQuery UI's未激发可排序更新事件
- 使用Redis更新排序集
- 根据数字排序顺序更新自动重新排列HTML列表
- 在backbone.js中,如何使用新的模型数据更新视图,同时在没有完整渲染的情况下保持排序顺序
- 可排序数据和更新数据库问题
- mongodb数组重新排序和更新
- 使用Jquery sortable和gridview更新排序顺序