Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 507
Amandine Avatar asked May 06 '15 07:05

Amandine


1 Answers

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)});
        }
}
like image 53
BENARD Patrick Avatar answered Sep 27 '22 22:09

BENARD Patrick