rowReordering - how to return new order?

rowReordering - how to return new order?

phoenix4phoenix4 Posts: 2Questions: 0Answers: 0
edited August 2012 in DataTables 1.8
Hi Everyone,

I have rowReordering working like in the demo here:
http://jquery-datatables-row-reordering.googlecode.com/svn/trunk/default.html

However I want to have a separate function I can call from a html button that returns the new order.
(dont care what format it is in, json, array etc...)

It should be the most simplest of things, tho I cant seem to work out how.

Eg:
so if by default my rows had index : 5, 10, 15

then I dragged them to new positions eg new order: 10, 5, 15

I'd like to click a button and have a function return me the new order.


Any ideas?

Regards,
Adam

Replies

  • Ironwil616Ironwil616 Posts: 50Questions: 0Answers: 0
    edited September 2012
    I modified the source code of the plugin to do this more transparently. I basically just took the concept from the existing 'fnGetState' function, and added this function:

    [code]
    function fnGetReorderedIDs(id){
    var table = $("#" + id).closest("table");
    var ids = table.find(".projectID").map(function(){
    return this.value;
    }).get();

    return ids.join(",")
    }
    [/code]

    The selector ".projectID" returns a group of hidden fields that each contained the ID of the project in its row. The map function just allowed me to grab all the values as a group, and I joined them together because I was sending them server-side for reordering, and it was the easiest way I could think of. Then I modified the ajax call in the plugin to add these values:

    [code]
    // Snag the IDs in their current order in the DOM.
    var reorderedIDs = fnGetReorderedIDs(ui.item.context.id);

    if (properties.sURL != null) {
    properties.fnStartProcessingMode();
    $.ajax({
    url: properties.sURL,
    type: properties.sRequestType,
    data: {
    // Leave the default values alone, even though I'm not using them.
    id: ui.item.context.id,
    fromPosition: oState.iCurrentPosition,
    toPosition: oState.iNewPosition,
    direction: oState.sDirection,
    group: sGroup,
    // Add my reordered IDs to the mix.
    reorderedIDs: reorderedIDs
    },
    success: function () {
    fnMoveRows(sSelector, oState.iCurrentPosition, oState.iNewPosition, oState.sDirection, ui.item.context.id, sGroup);
    properties.fnEndProcessingMode();
    },
    error: function (jqXHR) {
    fnCancelSorting(tbody, properties, 1, jqXHR.statusText);
    }
    });
    }
    [/code]

    There are probably much better ways to do this, but I'm not super-adept at writing plugin code. In any case, this grabbed a set of values from the DOM as it stands after the move. Without it, I had to write some fairly ugly code to reset each project's position in the list, and it's much easier and less error-prone to simply assign them all an index value from their actual position.
This discussion has been closed.