jQuery:管理员视图示例

时间:2014-09-30 07:59:28

标签: jquery

            success: function(data){
            $('#rightContent').prepend('<h2>' + data.information['myPosition'] + '</h2>');![enter image description here][1]
            $.each(data.information.Firstname, function(key, value){
                $('#rightContent').append('<span>' + value + '</span>');
            });
        }

enter image description here

我需要把它放在桌子上或任何好看的视图。对于firstname,middlename,lastname。只是一个样本会很棒。感谢。

1 个答案:

答案 0 :(得分:1)

我希望这就是你要找的......(按下运行代码片段来查看

// this object is the object you showed above
var information = { 
  Firstname: ["Super","Bat"], 
  Lastname: ["Man","Woman"], 
  Middlename: ["Freakin","Na Na Na Na"] 
};

// hold an array for the rows we will need
// also creater the header row
var rows = [];
var $header = $("<tr/>");

// create the table
var $table = $("<table/>");

// for each item in your information object
// you may need to limit this to first/last/middle
for( item in information ) {
  
  // Store the item, I couldnt think of a proper name
  var level = information[item];
  var levelName = item;
  
  // append the object name to the header
  $header.append("<td>"+ levelName +"</td>");
  
  // now append all the items in the array for this item
  for( item in level ) {
    
    // because this loops, we only want to set the row once,
    // so set it to itself first if it's empty.
    rows[item] = rows[item] || $("<tr/>");
    rows[item].append("<td>" + level[item] + "</td>");

  }

}

// now see how many items were added to the
// rows array, and add them all to the table
for( item in rows ) {
  $table.append( rows[item] );
}

// insert the header at the start.
$table.prepend( $header );

// add your table to the body ( change this as needed );
$("body").append( $table );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>