3
I have a table and using jquery
( sortable
), I’d like when you drag the line by modifying the html
of the table, the order of the array
change
Example:
CAMPO1
CAMPO2
/// DRAG THIS ONE DOWN
CAMPO3
/// SOON THIS ONE GOES UP
ARRAY ORIGINAL: [{id:1, campo:1},{id:2, campo:2},{id:3, campo:3}
]
ARRAY DEPOIS : [{id:1, campo:1},{id:3, campo:3},{id:2, campo:3}]
Code example:
<html ng-app="listagemIndex" style="background-color: #FFF;" ng-cloak>
<met charset="UFT-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<title ng-bind="apps"></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<script>
angular.module("listagemIndex", []);
angular.module("listagemIndex").controller("listagemIndexControl", function ($scope){
$scope.conjunto = [{id:1, nome:"ana1"},{id:2, nome:"ana2"},{id:3, nome:"ana3"},{id:4, nome:"ana4"}];
////////////////////////
});
</script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#sortable" ).sortable();
$( "#sortable" ).disableSelection();
} );
</script>
</head>
<body class="container-fluid" ng-controller="listagemIndexControl">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>NOME</th>
</tr>
</thead>
<tbody id="sortable">
<tr ng-repeat="obj in conjunto | orderBy: obj.nome" >
<td>{{obj.id}}</td>
<td><input type="text" ng-model="obj.nome"></td>
</tr>
</tbody>
</table>
</body>
</html>
Thank you very much guy! perfect! worked as I wanted! living and learning haha!
– Danilo Fernando
Good afternoon Virgilio, would it be possible to do this without Angular? for example only with jquery?
– Renan
@Renan has. Open a question on the subject explaining your doubts, referencing this as an example, but explaining your real doubt with an example
– novic