"Bootstrap Draggable Row Table"
Bootstrap 4.0.0 Snippet by hzeynelcakmak

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<head>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js" integrity="sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30=" crossorigin="anonymous"></script>
</head>
<div class="container">
<div class="row">
<div class="col-lg-12">
<table class="table table-hover" id="myTable">
<thead>
<tr>
<th scope="col" class="text-center">#</th>
<th scope="col">Input</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr id="1" >
<td class="index">1</td>
<td class="indexInput"><input type="text" name="abc" id="index" value="1"></td>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr id="2">
<td class="index">2</td>
<td class="indexInput"><input type="text" name="abc" id="index" value="2"></td>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
td:hover{
cursor:move;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var fixHelperModified = function(e, tr) {
var $originals = tr.children();
var $helper = tr.clone();
$helper.children().each(function(index) {
$(this).width($originals.eq(index).width())
});
return $helper;
},
updateIndex = function(e, ui) {
$('td.index', ui.item.parent()).each(function (i) {
$(this).html(i+1);
});
$('input[type=text]', ui.item.parent()).each(function (i) {
$(this).val(i + 1);
});
};
$("#myTable tbody").sortable({
helper: fixHelperModified,
stop: updateIndex
}).disableSelection();
$("tbody").sortable({
distance: 5,
delay: 100,
opacity: 0.6,
cursor: 'move',
update: function() {}
});
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Related: See More


Questions / Comments:

Is it possible to use <input> inside <td> for serial number (#)? If yes can you please tell me how? like <td><input type="text" name="abc" id="index"></td>

Juned () - 6 years ago - Reply 0


Hi kanks, I fixed the code.

You can look.

hzeynelcakmak () - 6 years ago - Reply 0