简体   繁体   English

在将JSON数据绑定到HTML表Jquery的同时,从JSON绑定每个表列(标题)的选择下拉列表

[英]Bind select dropdown for each table column (Head) from JSON, while binding JSON data to HTML table Jquery

I have a dynamic table populated from JSON data. 我有一个从JSON数据填充的动态表。 I want to have a select dropdown for each column. 我希望每个列都有一个选择下拉列表。 The number of columns are not fixed. 列数不固定。 They are populated by JSON. 它们由JSON填充。 So I want the select dropdown at the top each column. 所以我想在每个列的顶部选择下拉菜单。 This Dropdown also should be populated by JSON. 此下拉列表也应由JSON填充。

HTML: HTML:

<table id="jsonTable" class="table table-hovere">
</table>

Jquery: jQuery:

function addAllColumnHeaders(myList) {
         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));
            }
         }
         }
         $("#jsonTable").append(headerTr$);

         return columnSet;
         }

         $.getJSON("data.json", function (data) {
         var columns = addAllColumnHeaders(data);

         for (var i = 0; i < data.length; i++) {
         var row$ = $('<tr/>');
         for (var colIndex = 0; colIndex < columns.length; colIndex++) {
            var cellValue = data[i][columns[colIndex]];

            if (cellValue == null) { cellValue = ""; }

            row$.append($('<td/>').html(cellValue));
         }
         $("#jsonTable").append(row$);
         }
         });

JSON: JSON:

[
  {
    "Model": "Iphone 18",
    "Name": "iOS",
    "Share": 57.56,
    "Price Range": "$800 - $1000",
    "Brand": "Apple"
  },
  {
    "Model": "Nexus 23",
    "Name": "Android",
    "Share": 24.66,
    "Price Range": "$600 - $800",
    "Brand": "Samsung"
  },
  {
    "Model": "Tom-tom",
    "Name": "Java ME",
    "Share": 10.72,
    "Price Range": "$200 - $900",
    "Brand": "X Brand"
  }
]

Currently, this is my table: 目前,这是我的表: 目前,这是我的表:

I want the table to be like this: 我希望桌子是这样的: 我希望桌子是这样的:

Please Help. 请帮忙。 Thanks in advance. 提前致谢。

You can do it this way- 你可以这样-

https://jsfiddle.net/9eemmbjc/1/ https://jsfiddle.net/9eemmbjc/1/

Your json 您的json

var jsonResult=[
  {
    "Model": "Iphone 18",
    "Name": "iOS",
    "Share": 57.56,
    "Price Range": "$800 - $1000",
    "Brand": "Apple"
  },
  {
    "Model": "Nexus 23",
    "Name": "Android",
    "Share": 24.66,
    "Price Range": "$600 - $800",
    "Brand": "Samsung"
  },
  {
    "Model": "Tom-tom",
    "Name": "Java ME",
    "Share": 10.72,
    "Price Range": "$200 - $900",
    "Brand": "X Brand"
  }
]

jQuery jQuery的

var rowArray = new Array();

$.each(jsonResult,function(index,item){
  row=$("<tr/>");
    row.prepend('<td><select><option>Some option</select></td>');
         $.each(item,function(inx,el){
         $(row).append('<td>'+el+'</td>');
    });
    rowArray.push(row);
})

$('table').html(rowArray);

Update 更新资料

https://jsfiddle.net/9eemmbjc/2/ https://jsfiddle.net/9eemmbjc/2/

Not a good way but should do what you want from jsonObject. 这不是一个好方法,但是应该按照jsonObject的要求进行操作。

I've changed something in the script for showing the select box row at the top before headers. 我更改了脚本中的某些内容,以便在标题之前的顶部显示选择框行。

var selectRow= {
        Model:$('<select/>'),
        Name:$('<select/>'),
        Share:$('<select/>'),
        PriceRange:$('<select/>'),
        Brand:$('<select/>')
};

//select boxes at the top
$.each(jsonResult,function(index,item){
    selectRow.Model.append('<option>'+item.Model+'</option>');
    selectRow.Name.append('<option>'+item.Name+'</option>');
    selectRow.Share.append('<option>'+item.Share+'</option>');
    selectRow.PriceRange.append('<option>'+item["Price Range"]+'</option>');
    selectRow.Brand.append('<option>'+item.Brand+'</option>');
});

var dropdownRow= '<tr><th>'+selectRow.Model[0].outerHTML+'</th><th>'+selectRow.Name[0].outerHTML+'</th><th>'+selectRow.Share[0].outerHTML+'</th><th>'+selectRow.PriceRange[0].outerHTML+'</th><th>'+selectRow.Brand[0].outerHTML+'</th></tr>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM