Sort by colum

2

I try to sort by ACEPTACION , how do I do it?

I want to use jQuery DataTables to use

 order: [
      [3, 'asc']

let validacion = [1, 3, 5, 4, 2];
let personas = ["ana", "juan", "pedro", "maria", "gustavo"];

var html = "<table border=2 id='examples'>";
html += "<thead>";
html += "<tr><th colspan='3'>Validacion de " + " </th></tr>";
html += "<tr>";
html += "<th > Parametros </th>";
html += "<th > Aceptacion </th> </thead><tbody>";

for (let i = 0, y = 0; i < validacion.length; i++, y = y + 2) {
  html += "<tr>";
  html += "<td>" + personas[i] + "</td>";
  html += "<td>" + validacion[i] + "</td>";
  html += "</tr>";
}
html += "</tbody>";
html += "</table>";

document.getElementById("tabla").innerHTML = html;


$(document).ready(function() {
  $('#myTable').DataTable({
    "order": [
      [1, "desc"]
    ]
  });
});
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td,
th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https:////cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

<div id='tabla'>tabla</div>
    
asked by hubman 19.12.2018 в 15:35
source

1 answer

3

You are not using the id of the table correctly, which in your case is "examples" so the script does not work, here is a functional example taking your code

 let validacion = [1, 3, 5, 4, 2];
      let personas = ["ana", "juan", "pedro", "maria", "gustavo"];

      var html = "<table border=2 id='examples'>";
      html += "<thead>";
      html += "<tr><th colspan='3'>Validacion de " + " </th></tr>";
      html += "<tr>";
      html += "<th > Parametros </th>";
      html += "<th > Aceptacion </th> </thead><tbody>";

      for (let i = 0, y = 0; i < validacion.length; i++, y = y + 2) {
        html += "<tr>";
        html += "<td>" + personas[i] + "</td>";
        html += "<td>" + validacion[i] + "</td>";
        html += "</tr>";
      }
      html += "</tbody>";
      html += "</table>";

      document.getElementById("tabla").innerHTML = html;


      $(document).ready(function() {
        $('#examples').DataTable({
          "order": [
            [1, "desc"]
          ]
        });
      });
 <div id="tabla"></div>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script src="https:////cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    
answered by 19.12.2018 / 16:05
source