Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 从json数据中动态检索列_Jquery - Fatal编程技术网

Jquery 从json数据中动态检索列

Jquery 从json数据中动态检索列,jquery,Jquery,我有一个列数组: var columns=["title","length","status"]; 这是我的密码: function executeSearch(query) { var url = ajaxPath+ "?s=search&r="+resource+"&q="+query; $.getJSON(url, function(data){

我有一个列数组:

var columns=["title","length","status"]; 
这是我的密码:

    function executeSearch(query)
    {                       
        var url = ajaxPath+ "?s=search&r="+resource+"&q="+query;
        $.getJSON(url, function(data){          
            var html = '<table class="table table-hover">';
            html += '<thead><tr>';
            $(columns).each(function(index,value){
                html +='<td>'+value+'</td>';
            })
            html += '</tr></thead>';
            html += '<tbody>';          
            $(data).each(function(i,item){
                html += '<tr>';
                $(item).each(function(e,itm){
                    for(var propt in itm){
                        if (columns.hasOwnProperty(propt)) {
                            html += '<td>'+itm[propt]+'</td>';
                        }
                    }                   
                })
                html += '</tr>';
            });
            html += '</tbody>';
            html += '</table>';
            $("#datatable").html(html);
        });
    }   
我正在尝试根据列获取数据。我正在使用columns.hasOwnProperty

它似乎工作不正常

是否有jquery方法来处理此问题?

在您的示例中,列是一个数组,您应该使用它来确定列是否在数组中

// check if the propt is in the array (-1 means not found)
if (coulmns.indexOf(propt) !== -1) {
但是,在您正在查看的情况下,您可能应该迭代columns数组,而不是items属性。您正在构建一个表,并且希望列按指定的顺序排列


而不是$item.each,你应该$.eachcolumns,functionindex,propt{并使用item[propt]

构建你的表。这很有效,但是我认为我需要迭代每个tr项,并将列应用到每个td。不管怎样,它工作得很好。谢谢