Search code examples
javascriptjquerytwitter-bootstrapbootstrap-table

Move row up or down in bootstraptable


I'm using Bootstrap-Table in a project and I'd like to move rows up or down.

I have these action events :

window.actionEvents = {
'click .up': function (e, value, row, index) {
var thisrow = $(this).parents("tr:first"),
thisrow.prev().data('index', rowindex);
},
'click .down': function (e, value, row, index) {
var thisrow = $(this).parents("tr:first");
thisrow.insertAfter(thisrow.next());
},
}

It moves rows on screen but it doesn't work really well as row index haven't changed...

So I tried to change the row .data('index') but it doesn't work...

Has someone succeeded in moving row ?


Solution

  • Here is something :

    Fiddle : https://jsfiddle.net/dfpo899p/1/

    Js :

    window.actionEvents = {
        'click .up': function (e, value, row, index) {
            var source = JSON.stringify($('#table').bootstrapTable('getData')[index]);
            var target = JSON.stringify($('#table').bootstrapTable('getData')[index - 1]);
            $('#table').bootstrapTable('updateRow', {'index':index - 1, 'row': JSON.parse(source)});
            $('#table').bootstrapTable('updateRow', {'index':index, 'row': JSON.parse(target)});
            },
        'click .down': function (e, value, row, index) {
            var source = JSON.stringify($('#table').bootstrapTable('getData')[index]);
            var target = JSON.stringify($('#table').bootstrapTable('getData')[index + 1]);
            $('#table').bootstrapTable('updateRow', {'index':index + 1, 'row': JSON.parse(source)});
            $('#table').bootstrapTable('updateRow', {'index':index, 'row': JSON.parse(target)});
            }
    }