每个子阵列的Jquery / Javascript附加到div

时间:2013-09-06 16:17:04

标签: php javascript jquery html json

我正在使用一个名为LogMyCalls的公司的API,我们正在尝试提取最近的5个电话,并将它们显示在列表中,但我遇到了问题。这是json如何从他们身上出来的:

{"status":"success","matches":3,"results":[{"tracking_number":"8556979885","disposition":"ANSWERED","duration":70,"external_id":null,"id":4918363,"is_outbound":0,"file_url":"http://s3.amazonaws.com/LogMyCalls/call_recordings/8556979885_4808898944_6024711995_20130801-214811.mp3?AWSAccessKeyId=**********&Expires=1378497578&Signature=*******","ouid":14104,"caller_id":"**********","calldate":"2013-08-01 21:48:11","status":"active","ringto_number":"**********","title":null,"user_id":null,"scored":false,"tagged":false},{"tracking_number":"8556979885","disposition":"ANSWERED","duration":49,"external_id":null,"id":4916465,"is_outbound":0,"file_url":"http://s3.amazonaws.com/LogMyCalls/call_recordings/8556979885_4808898944_5153091261_20130801-203535.mp3?AWSAccessKeyId=**********&Expires=1378497578&Signature=*******","ouid":14104,"caller_id":"**********","calldate":"2013-08-01 20:35:35","status":"active","ringto_number":"**********","title":null,"user_id":null,"scored":false,"tagged":false},{"tracking_number":"8556979885","disposition":"ANSWERED","duration":626,"external_id":null,"id":4911038,"is_outbound":0,"file_url":"http://s3.amazonaws.com/LogMyCalls/call_recordings/8556979885_4808898944_6028812248_20130801-162317.mp3?AWSAccessKeyId=**********&Expires=1378497578&Signature=*******","ouid":14104,"caller_id":"**********","calldate":"2013-08-01 16:23:17","status":"active","ringto_number":"**********","title":null,"user_id":null,"scored":false,"tagged":false}],"transaction_id":**********}

这是一个更漂亮的json:

Array
(
[status] => success
[matches] => 3
[results] => Array
    (
        [0] => Array
            (
                [tracking_number] => 8556979885
                [disposition] => ANSWERED
                [duration] => 70
                [external_id] => 
                [id] => 4918363
                [is_outbound] => 0
                [file_url] => http://s3.amazonaws.com/LogMyCalls/call_recordings/8556979885_4808898944_6024711995_20130801-214811.mp3?AWSAccessKeyId=***********&Expires=1378497836&Signature=*******
                [ouid] => 14104
                [caller_id] => *********
                [calldate] => 2013-08-01 21:48:11
                [status] => active
                [ringto_number] => **********
                [title] => 
                [user_id] => 
                [scored] => 
                [tagged] => 
            )

        [1] => Array
            (
                [tracking_number] => 8556979885
                [disposition] => ANSWERED
                [duration] => 49
                [external_id] => 
                [id] => 4916465
                [is_outbound] => 0
                [file_url] => http://s3.amazonaws.com/LogMyCalls/call_recordings/8556979885_4808898944_5153091261_20130801-203535.mp3?AWSAccessKeyId=********&Expires=1378497837&Signature=*******
                [ouid] => 14104
                [caller_id] => ******
                [calldate] => 2013-08-01 20:35:35
                [status] => active
                [ringto_number] => **********
                [title] => 
                [user_id] => 
                [scored] => 
                [tagged] => 
            )

        [2] => Array
            (
                [tracking_number] => 8556979885
                [disposition] => ANSWERED
                [duration] => 626
                [external_id] => 
                [id] => 4911038
                [is_outbound] => 0
                [file_url] => http://s3.amazonaws.com/LogMyCalls/call_recordings/8556979885_4808898944_6028812248_20130801-162317.mp3?AWSAccessKeyId=*****&Expires=1378497837&Signature=*******
                [ouid] => 14104
                [caller_id] => ***********
                [calldate] => 2013-08-01 16:23:17
                [status] => active
                [ringto_number] => **********
                [title] => 
                [user_id] => 
                [scored] => 
                [tagged] => 
            )

    )

    [transaction_id] => *****
)

我现在正在使用的是jQuery中的以下内容,我能够得到第一个结果,([0])但是无论我怎么想都没有什么可以拉动剩下的。

function loadCalls(date1, id){
      $('#calltitle').empty();
      $('#calls').empty();
     $.getJSON("**********", 
          function(data) {

             var calldate = date1.toString('MMMM yyyy');
              $('#calltitle').html('Calls from ' + calldate);

              var callnum = 0;

              $.each(data['results'], function() {                
                  //if(data['results'][callnum].disposition == "ANSWERED") { var icon = 'icon-check'; } else { var icon = 'icon-check-empty'; };          
                  $('#calls').append('<li><div class="col1"><div class="cont"><div class="cont-col1"><div class="label label-info"><i class="' + 'icon-check' + '"></i></div></div><div class="cont-col2"><div class="desc">Call from ' + data['results'][callnum].caller_id + ' at ' + data['results'][callnum].calldate.toString('MMMM dd') + '</div></div></div></div><div class="col2"><div class="date">Just now</div></div></li>');
                  var callnum = callnum++;
              })
          }); 
  }

对不起,我必须 * 这么多,但是对于客户和我们的缘故,我们必须这样做。

基本上,在我的jQuery中,我需要一种方法来获取以下所有结果:

data['results'][0]
data['results'][1]
data['results'][2]
data['results'][3]
data['results'][4]

我将允许显示最多5个,并且上面是数组中它们的路径,但我无法增加数量(例如,我尝试设置var callnum = 0; in JS,并且通过使用var callnum = callnum ++来增加每一轮;(应该在理论上工作)并且它不起作用。)

任何帮助表示赞赏! :)

1 个答案:

答案 0 :(得分:0)

当您执行var callnum = callnum++;时,您基本上会重新分配它。

尝试:

function loadCalls(date1, id){
      $('#calltitle').empty();
      $('#calls').empty();
     $.getJSON("**********", 
          function(data) {

             var calldate = date1.toString('MMMM yyyy');
              $('#calltitle').html('Calls from ' + calldate);

              var callnum = 0;

              $.each(data['results'], function() {                
                  //if(data['results'][callnum].disposition == "ANSWERED") { var icon = 'icon-check'; } else { var icon = 'icon-check-empty'; };          
                  $('#calls').append('<li><div class="col1"><div class="cont"><div class="cont-col1"><div class="label label-info"><i class="' + 'icon-check' + '"></i></div></div><div class="cont-col2"><div class="desc">Call from ' + data['results'][callnum].caller_id + ' at ' + data['results'][callnum].calldate.toString('MMMM dd') + '</div></div></div></div><div class="col2"><div class="date">Just now</div></div></li>');
                  callnum++;
              })
          }); 
  }