“Table JSON à HTML” Réponses codées

Convertir les données JSON en une table HTML

var myList = [
  { "name": "abc", "age": 50 },
  { "age": "25", "hobby": "swimming" },
  { "name": "xyz", "hobby": "programming" }
];

// Builds the HTML Table out of myList.
function buildHtmlTable(selector) {
  var columns = addAllColumnHeaders(myList, selector);

  for (var i = 0; i < myList.length; i++) {
    var row$ = $('<tr/>');
    for (var colIndex = 0; colIndex < columns.length; colIndex++) {
      var cellValue = myList[i][columns[colIndex]];
      if (cellValue == null) cellValue = "";
      row$.append($('<td/>').html(cellValue));
    }
    $(selector).append(row$);
  }
}

// Adds a header row to the table and returns the set of columns.
// Need to do union of keys from all records as some records may not contain
// all records.
function addAllColumnHeaders(myList, selector) {
  var columnSet = [];
  var headerTr$ = $('<tr/>');

  for (var i = 0; i < myList.length; i++) {
    var rowHash = myList[i];
    for (var key in rowHash) {
      if ($.inArray(key, columnSet) == -1) {
        columnSet.push(key);
        headerTr$.append($('<th/>').html(key));
      }
    }
  }
  $(selector).append(headerTr$);

  return columnSet;
}
Inexpensive Ibex

Table JSON à HTML

Convert JSON to HTML Table
Step 1: Select your input. Option 1 - Choose JSON file Encoding. Option 2 - Enter an URL. ...
Step 2: Choose output options (optional) Output Field Separator: , ; : Bar-| Tab Other-Choose. Include header in first row. ...
Step 3: Generate output. Result Data: Save your result: .htm Download Result EOL:
koder

Réponses similaires à “Table JSON à HTML”

Questions similaires à “Table JSON à HTML”

Plus de réponses similaires à “Table JSON à HTML” dans HTML

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code