2016-09-22 22 views
1

我有一個由對象組成的數組。每個對象都有屬性和值。屬性是緯度,經度和名稱。我需要在數據網格視圖中顯示此數組。下面是我的陣列,如何在數據網格中顯示由屬性和值對象組成的數組?

enter image description here

可以在任何一個份額的例子有什麼建議或

謝謝!

+0

該數據網格插件,您使用的是它的jqGrid或數據表或其他別的嗎? –

+0

我試圖使用juqery網格。還有其他方法嗎? – Aparna

+0

我建議使用https://datatables.net/,因爲有很多可用的幫助。 –

回答

4

試試這個:

$(function(){ 
 
var data = [{ 
 
    "Latitude": 18.00, 
 
    "Longitude": 23.00, 
 
    "Name": "Pune" 
 
}, { 
 
    "Latitude": 14.00, 
 
    "Longitude": 24.00, 
 
    "Name": "Mumbai" 
 
}, { 
 
    "Latitude": 34.00, 
 
    "Longitude": -4.00, 
 
    "Name": "Delhi" 
 
},{ 
 
    "Latitude": 23.00, 
 
    "Longitude": 23.00, 
 
    "Name": "Jaipur" 
 
}]; 
 
$.each(data,function(index,value) { 
 

 
     row="<tr><td>"+value.Latitude+"</td><td>"+value.Longitude+"</td><td>"+value.Name+"</td><tr>"; 
 
    
 
    $("#grid").append(row); 
 
}); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="grid"> 
 
    <tr> 
 
    <th> 
 
     Latitude 
 
    </th> 
 
    <th> 
 
     Longitude 
 
    </th> 
 
    <th> 
 
     Place 
 
    </th> 
 
    </tr> 
 
</table>

隨着動態標題

但是,如果所有的對象具有相同的屬性

$(function() { 
 
    var data = [{ 
 
    "Latitude": 18.00, 
 
    "Longitude": 23.00, 
 
    "Name": "Pune" 
 
    }, { 
 
    "Latitude": 14.00, 
 
    "Longitude": 24.00, 
 
    "Name": "Mumbai" 
 
    }, { 
 
    "Latitude": 34.00, 
 
    "Longitude": -4.00, 
 
    "Name": "Delhi" 
 
    }, { 
 
    "Latitude": 23.00, 
 
    "Longitude": 23.00, 
 
    "Name": "Jaipur" 
 
    }]; 
 
    header = "<tr>" 
 
    $.each(data[0], function(prop, value) { 
 
    header += "<th>" + prop + "</th>"; 
 
    }); 
 
    header += "</tr>" 
 
    $("#grid").append(header); 
 

 
    $.each(data, function(index, obj) { 
 

 
    row = "<tr>"; 
 
    $.each(obj, function(prop, value) { 
 
     row += "<td>" + value + "</td>"; 
 
    }); 
 
    row += "</tr>"; 
 

 
    $("#grid").append(row); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="grid"> 
 
</table>
它只會工作

+0

謝謝!但是有沒有一種方法可以擁有一個動態表頭,而不需要對頭部名稱進行硬編碼? – Aparna

+0

@Aparna用動態頭檢查更新的答案 –

2

你可以簡單的方式和非常大的文檔和社區可用於自定義數據表幫助使用dataTables jquery plugin

var dataSet = [{ 
 
    "Latitude": 18.00, 
 
    "Longitude": 23.00, 
 
    "Name": "Pune" 
 
}, { 
 
    "Latitude": 14.00, 
 
    "Longitude": 24.00, 
 
    "Name": "Mumbai" 
 
}, { 
 
    "Latitude": 34.004654, 
 
    "Longitude": -4.005465, 
 
    "Name": "Delhi" 
 
},{ 
 
    "Latitude": 23.004564, 
 
    "Longitude": 23.007897, 
 
    "Name": "Jaipur" 
 
}]; 
 
$(document).ready(function() { 
 
    $('#example').DataTable({ 
 
     data: dataSet, 
 
     "columns": [ 
 
      { "data": "Name" ,"title":"Custom Name"}, 
 
      { "data": "Latitude" }, 
 
      { "data": "Longitude" }, 
 
      
 
     ] 
 
    }); 
 
});
@import "https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css";
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script> 
 
<table id="example" class="display" cellspacing="0" width="100%"> 
 
     <thead> 
 
      <tr> 
 
       <th>Name</th> 
 
       <th>Latitude</th> 
 
       <th>Longitude</th> 
 
       
 
      </tr> 
 
     </thead> 
 
     
 
    </table>

+0

謝謝!但是有沒有一種方法可以擁有一個動態表頭,而不需要對頭部名稱進行硬編碼? – Aparna

+0

是的。你可以檢查它的文檔.... –

+0

我已經修改爲獲取動態名稱的列名 –

相關問題