jQuery 可排序和数组度假村
我有一个元素数组:markers
和一个表,该表更新其行以填充 markers
中存储的数据。 markers
本身包含一个对象 marker
,其中包含纬度和经度等信息。
当我使用 markers
数组中的详细信息填充表时,我会在循环时根据 i
为每一行赋予一个 ID。
在这张桌子上我有 jQuery 可排序。我正在寻找一种在每次对行进行排序时重新排序我的 markers
数组的方法,以便数组中项目的位置实际上是它的排序顺序值。这样,我可以随时循环遍历数组并查看它的最新顺序。
到目前为止,我已经按照应有的方式生成了表,并填充了 markers
数组。我什至能够在打乱行后重新获取行的顺序,目前我将它们存储在数组 orders
中,并且只是提醒,这是有效的(即 0,1,2 > > 0,1,2 > )。拖到度假村>2,0,1)。我希望现在能够使用 orders
中的一组值来重新利用我原来的 markers
数组。
我认为这将涉及创建一个临时数组 markersTemp
,用于存储保存在 markers
中的值,但按照新的顺序,除非我不确定如何循环到拯救每一个。有人做过类似的事情吗?
I have an array of elements: markers
and a table which updates it's rows to populate with the data stored within markers
. markers
itself houses an object marker
which has a latitude and longitude amongst other things.
When I populate the table with the details from the markers
array I give each row an ID based on i
as it loops through.
On this table I have jQuery sortable. I am looking for a way to re-order my markers
array each time the rows are sorted, so that the position of the item in the array is actually it's sort order value. That way, I can simply loop through the array at any time and see it's most up to date order.
So far I have the table generating as should and the markers
array populated. I am even able to grab back the order of the rows after shuffling them around, at the moment I store them in an array orders
and just alert, this works (ie. 0,1,2 > dragged to resort > 2,0,1). I was hoping for a way of now using the set of values in orders
to resort my original markers
array.
I thought it would involve creating a temporary array markersTemp
with which to store the values saved in markers
but in the new order, except I'm not sure how to loop through to save each one. Anyone done anything similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
旧过程:
order
将 ID 存储在列表中的当前行
order
markersTemp
order
获取列表
order
中位置i
的 ID。让
markersTemp[i]
为markers
中的元素(通过id
在旧位置引用)markers
markers
到markersTemp
优化:
order
按
order
中从getId()
返回的索引引用的元素推送元素,order
覆盖markers
。Old procedure:
order
Store the IDs at the current rows in list
order
markersTemp
order
Get the ID at position
i
in listorder
.Let
markersTemp[i]
be the element inmarkers
(referred at the old position throughid
)markers
markers
tomarkersTemp
Optimized:
order
Push the element, as referred by the index as returned from
getId()
inorder
markers
byorder
.啊啊,抱歉耽误大家时间了!又花了5分钟,我就解决了。对于其他遇到问题的人,这是在可排序的删除事件之后调用的整个函数。
但我不会说事情已经完全结束了。我很想知道这是否是最有效/最简洁的方法。我知道 javascript 中内置了一个 array .sort() 函数,可以用它来代替吗?
编辑:
好吧,我在页面上的表格的布局如下:
getId() 只是获取 TR 的 id 并运行一些正则表达式来仅获取数字。当对它们进行排序时,顺序可能类似于:row2、row0、row1。我将此订单存储在
orders
(临时数组)中,并使用markersTemp
的值填充markersTemp
(另一个临时数组),但采用新订单。Ahh, apologies for wasting everyones time! 5 more minutes and I got it solved. For others with the problem, here's my entire function called after a sortable dropped event.
I wouldn't say it was entirely over though. I would be very interested to know if this is the most efficient/tidiest way to do this. I am aware there is an array .sort() function built into javascript, could this be used instead?
Edit:
Ok so my table on the page is laid out like so:
getId() simply grabs the id of the TR and runs a little regex to get only the number. When they are resorted then the order might be something like: row2, row0, row1. I store this order in
orders
(a temporary array) and populatemarkersTemp
(another temporary array) with the values ofmarkersTemp
but in the new order.