RowReorder Sequence with AJAX Sourced data

RowReorder Sequence with AJAX Sourced data

jLinuxjLinux Posts: 981Questions: 73Answers: 75
edited November 2015 in RowReorder

I was trying to find a way to use the Row Reorder extension with an ajax sourced table. The problem I'm having is the sequence values aren't pulled from AJAX. I just need them to display in whatever order they're pulled as, then be able to be reordered from there. So the sequence values need to be created when the table is initialized.

I tried to set columns.data and columns.defaultContent to null for the first column (sequence col), then use columnDefs.render to return a sequenced integer, which it looks like it works fine at first. But since the columnDefs.render gets called every time the table draws, it doesn't work very well. Also, RowReorder needs to be able to write that column, so having anything else render it would interfere with the RowReorder plugin.

Heres the JS code:

$(document).ready( function () {
    var rowId = 0;

    $('#example').DataTable({
        ajax: {
            url: 'http://www.linuxdigest.org/misc/script_examples/DataTables-Live-Ajax/examples/dataSrc.php'
        },
        rowId: 'extn',
        columns: [
            {
                name: 'seq',
                defaultContent: '0',
                data: null
            },
            {
                name: 'ID',
                data: 'emp_id'
            },
            {
                name: 'Name',
                data: 'name'
            },
            {
                name: 'Position',
                data: 'position'
            },
            {
                name: 'Age',
                data: 'age'
            }
        ],
        columnDefs: [
            {
                targets: 0,
                data: null,
                render: function ( data, type, row, meta ) {
                    return rowId++;
                }
            }
        ],
        rowReorder: true
    });
});

Which is in the JSBin instance I've been messing with.

Any help would be appreceated!

Thanks

This question has accepted answers - jump to:

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Bump? Anyone? @allan? :-D

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    The issue here is that the columns.render number's don't really "exist". They are calculated and then displayed, but aren't retained (other than in the DOM) so they aren't too useful for row reordering which changes the data value (null in this case).

    What you need to do is inject the sequence into the data. This can be done using ajax.dataSrc: http://live.datatables.net/xozoxoce/2/edit (i.e. modify the data before DataTables "sees" it).

    Allan

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited November 2015

    Oh wow. I was thinking about adding the rowReorder to my keepconditions plugin, but the rows would need to have a rowId set so i would know what rows were moved, and thats usually only for ajax or json sources.. so they would NEED to do that fix you just showed... which kinda sucks.. lol

    Maybe i just wont add rowReorder compatability... not worth it for any users

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    RowReorder is only really useful if you already have a sequence number in the data I would have said.

    Allan

This discussion has been closed.