jQuery 可排序和数组度假村

发布于 2024-12-09 03:27:52 字数 715 浏览 0 评论 0原文

我有一个元素数组: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半山落雨半山空 2024-12-16 03:27:52
function resort() {
    var order = []; // Get the order of the new rows
    $('#createMapDirections tbody tr').each(function(i, elem){
        order.push(markers[getId($("td", elem))]);
    });
    markers = order;
    updateAll();
}

旧过程:

  1. 创建一个新列表 order
  2. 循环遍历表中的当前行。
    将 ID 存储在列表中的当前行 order
  3. 创建一个新列表 markersTemp
  4. 循环列表 order
    获取列表 order 中位置 i 的 ID。
    markersTemp[i]markers 中的元素(通过 id 在旧位置引用)
  5. 重置 markers
  6. 设置markersmarkersTemp

优化:

  1. 创建一个新列表 order
  2. 循环遍历表中的每一行。
    order 中从 getId() 返回的索引引用的元素推送元素,
  3. 并按 order 覆盖 markers
function resort() {
    var order = []; // Get the order of the new rows
    $('#createMapDirections tbody tr').each(function(i, elem){
        order.push(markers[getId($("td", elem))]);
    });
    markers = order;
    updateAll();
}

Old procedure:

  1. Create a new list order
  2. Loop through the current rows in the table.
    Store the IDs at the current rows in list order
  3. Create a new list markersTemp
  4. Loop through list order
    Get the ID at position i in list order.
    Let markersTemp[i] be the element in markers (referred at the old position through id)
  5. Reset markers
  6. Set markers to markersTemp

Optimized:

  1. Create a new list order
  2. Loop through each row in the table.
    Push the element, as referred by the index as returned from getId() in order
  3. Overwrite markers by order.
╰ゝ天使的微笑 2024-12-16 03:27:52

啊啊,抱歉耽误大家时间了!又花了5分钟,我就解决了。对于其他遇到问题的人,这是在可排序的删除事件之后调用的整个函数。

但我不会说事情已经完全结束了。我很想知道这是否是最有效/最简洁的方法。我知道 javascript 中内置了一个 array .sort() 函数,可以用它来代替吗?

function resort() {

    var order = new Array(); // Get the order of the new rows
    for (var i = 0; i < markers.length; i++) {
        order.push(getId($('#createMapDirections tbody tr').eq(i).find('td')));
    }

    var markersTemp = new Array();
    for (var i = 0; i < markers.length; i++) {
        var id = order[i];
        markersTemp[i] = markers[id];
    }

    markers.length = 0;
    markers = markersTemp.slice();

    updateAll();

}

编辑:

好吧,我在页面上的表格的布局如下:

<table><tbody>
    <tr id="row0"><td></td></tr>
    <tr id="row1"><td></td></tr>
    <tr id="row2"><td></td></tr>
</tbody></table>

getId() 只是获取 TR 的 id 并运行一些正则表达式来仅获取数字。当对它们进行排序时,顺序可能类似于:row2、row0、row1。我将此订单存储在 orders (临时数组)中,并使用 markersTemp 的值填充 markersTemp (另一个临时数组),但采用新订单。

function getId(button) {
    return $(button).parents('tr').attr('id').replace(new RegExp('\[A-Za-z]+'), '');
}

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?

function resort() {

    var order = new Array(); // Get the order of the new rows
    for (var i = 0; i < markers.length; i++) {
        order.push(getId($('#createMapDirections tbody tr').eq(i).find('td')));
    }

    var markersTemp = new Array();
    for (var i = 0; i < markers.length; i++) {
        var id = order[i];
        markersTemp[i] = markers[id];
    }

    markers.length = 0;
    markers = markersTemp.slice();

    updateAll();

}

Edit:

Ok so my table on the page is laid out like so:

<table><tbody>
    <tr id="row0"><td></td></tr>
    <tr id="row1"><td></td></tr>
    <tr id="row2"><td></td></tr>
</tbody></table>

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 populate markersTemp (another temporary array) with the values of markersTemp but in the new order.

function getId(button) {
    return $(button).parents('tr').attr('id').replace(new RegExp('\[A-Za-z]+'), '');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文