weixin_33671935 2014-08-25 07:49 采纳率: 0%
浏览 39

如何从json向列表视图添加行?

我正在尝试从json(根据响应)向列表视图添加行(匹配格式):

results : { _id: 53f8c48ddc1f5f0419f2ed53,
  bName: 'Microsoft',
  phone: 35588319,
  Email: 'microsoft@gmail.com',
  field: 'QA',
  exp: '0-2',
  __v: 0,
  location: [ { longitude: 7.8, latitude: 7.8, _id: 53f8c48ddc1f5f0419f2ed54 } ] }end

列表:

<table summary="This table lists all the Jobs." cellpadding="40" cellspacing="40" vspace="40">
    <caption id = "jobsListView" >
        JOBS
    </caption>
    <thead>
        <tr>
            <th scope="col">Company Name</th>
            <th scope="col">Contact</th>
            <th scope="col">Email</th>
            <th scope="col">Field</th>
            <th scope="col">Exp</th>

        </tr>
    </thead>
    <tbody>
        <!-- every row = a row in the file 
        <tr>
            <th scope="row">Microsoft</th> // comapny name
            <td>0508558319</td> // contact
            <td>Microsoft@mcsf.com (JFK)</td> // email 
            <td>QA (JFK)</td> // field 
            <td>0-2 (JFK)</td> // exp
        </tr>

       -->
    </tbody>
</table>

bName =公司名称,电话=联系人...没有地址。然后我通过ajax发送ec:

$.ajax({
      type: 'GET',
      dataType: 'JSON',
      url:'http://localhost:3000/find',
      data: workerInfo, 
     success: function(jobs){
      }
    });
  • 写回答

1条回答 默认 最新

  • weixin_33720452 2014-08-25 09:47
    关注

    Suppose if the response is array of objects -

    var $body = $("table tbody"); //select table (change ID / class if required anad locate to tbody)
    $.ajax({
          type: 'GET',
          dataType: 'JSON',
          url:'http://localhost:3000/find',
          data: workerInfo, 
          success: function(response){ 
              var jobs = JSON.parse(response); //parse the response.
              $.each(jobs, function(j, e) { //every object represents row so iterate thru it
                  //generate table row 
                  var row='<tr>';
                  row+='<td>'+e.bName+'</td>';
                  row+='<td>'+e.phone+'</td>';
                  row+='<td>'+e.Email+'</td>';
                  row+='<td>'+e.field+'</td>';
                  row+='<td>'+e.exp+'</td>';
                  row+='</tr>';   
                  $body.append(row); //Append it to tbody
              });
          }
    });
    

    And if the response is single object -

    var $body = $("table tbody"); //select table (change ID / class if required anad locate to tbody)
    $.ajax({
          type: 'GET',
          dataType: 'JSON',
          url:'http://localhost:3000/find',
          data: workerInfo, 
          success: function(response){ 
              var jobs = JSON.parse(response); //Parse the response
                  //generate table row 
                  var row='<tr>';
                  row+='<td>'+jobs.bName+'</td>';
                  row+='<td>'+jobs.phone+'</td>';
                  row+='<td>'+jobs.Email+'</td>';
                  row+='<td>'+jobs.field+'</td>';
                  row+='<td>'+jobs.exp+'</td>';
                  row+='</tr>';   
                  $body.append(row); //Append it to tbody
          }
    });
    

    Hope it helped!

    评论

报告相同问题?

悬赏问题

  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。