【问题标题】:how to access a 2D array in ajax如何在ajax中访问二维数组
【发布时间】:2014-05-12 06:24:09
【问题描述】:

所以基本上我的函数的返回值是一个二维数组,我打算显示每一行和每一列;但是,我在单独访问它们时遇到了麻烦,即 row[0][0] 我已经尝试了我在类似问题中看到的 stringify 函数,但没有让它工作。这是代码:

ajax:

$.ajax({
    url: "viewDayDocuments",
    type: 'post',
    data: {currentDay: currentDay, currentMonth: currentMonth, currentYear: currentYear},
    success: function(result){
        $('.list').text('');
        $('.list').remove();
        incomingDoc = JSON.stringify(result);
        $(".listIncoming").html("<p class = 'list'>This is the: "+ incomingDoc['referenceNo'] + "</p>");
        $("#myform").show(500);
    }
 });

控制器代码:

$date = $data['year']."-".$data['month']."-".$data['day'];

        $this->load->model('search_form');
        $output['details'] = $this->search_form->searchDateRetrievedIncoming($date);

        echo json_encode($output);

返回二维数组的模型代码:

        $rows[] = array();
        $rows2[] = array();
        $rows3[] = array();

        $i = 0;
        $companyName = $this->db->query("SELECT id, name from company");
        foreach($companyName->result_array() as $row2){
            $rows2[$i]['id'] = $row2['id'];
            $rows2[$i]['name'] = $row2['name'];
            $i++;
        }
        //get all company names

        $i = 0;
        $staffName = $this->db->query("SELECT id, surname, firstName, middleInitial from employee");
        foreach($staffName->result_array() as $row3){
            $rows3[$i]['id'] = $row3['id'];
            $rows3[$i]['name'] = $row3['surname'].", ".$row3['firstName']." ".$row3['middleInitial'];
            $i++;
        }
        //get all employee names
        $i= 0;

        $output = $this->db->query("SELECT * from incoming WHERE dateReceived BETWEEN '$searchQuery' AND DATE(NOW())
                                    ORDER BY incomingId LIMIT 20");
        if ($output->num_rows() > 0) {
            foreach($output->result_array() as $row){
                $count = 0;
                $j = 0;
                $rows[$i]['incomingId'] = $row['incomingId'];
                $rows[$i]['referenceNo'] = $row['referenceNo'];
                $rows[$i]['documentTypeId'] = $row['documentTypeId'];
                $rows[$i]['documentDate'] = $row['documentDate'];
                $rows[$i]['dateReceived'] = $row['dateReceived'];
                $rows[$i]['sender'] = $row['sender'];

                while($count < sizeof($rows2)){
                    if($rows2[$j]['id'] != $row['companyId']){
                        $j++;
                    }else{
                        $rows[$i]['companyName'] = $rows2[$j]['name'];
                        break;
                    }
                    $count++;
                }
                $j= 0;
                $count = 0;
                while($count < sizeof($rows3)){
                    if($rows3[$j]['id'] != $row['responsibleStaffId']){
                        $j++;
                    }else{
                        $rows[$i]['responsibleStaffName'] = $rows3[$j]['name'];
                        break;
                    }
                    $count++;
                }

                $rows[$i]['subject'] = $row['subject'];
                $rows[$i]['actionDone'] = $row['actionDone'];

                $rows[$i]['track'] = $row['track'];
                $rows[$i]['completed'] = $row['completed'];
                $rows[$i]['remarks'] = $row['remarks'];
                $i++;
            }

            return $rows;
        }
        return false;

工作答案:

incomingDoc = JSON.parse(result);
        for(var i = 0; i <  incomingDoc.details.length; i++){
            $(".listIncoming").html("<p class = 'list'>This is the: "+ incomingDoc.details[i].referenceNo + "</p>");
        }      

【问题讨论】:

  • 你能把你的 2D 数组贴出来看看吗?
  • 基本上是 print_r($output) 我理解对了吗??
  • Array ( [0] => Array ( [incomingId] => 24 [referenceNo] => 908941 [documentTypeId] => 1 [documentDate] => 2014-05-06 [dateReceived] => 2014-05-12 [发件人] => 我 [companyName] => 新人民军 [responsibleStaffName] => Man, Alf B [subject] => testing [actionDone] => round [track] => 1 [完成] = > 0 [remarks] => round ) ) ...这是我的搜索功能的结果,无论如何它使用相同的功能
  • 添加 console.log(result)console.log(incomingDoc) 以检查您收到的输出。您可以在 Firebug 中查看输出,以了解结果的结构。
  • 我的数组是这样的:{"details":[{"incomingId":"24","re​​ferenceNo":"908941","documentTypeId":"1","documentDate":" 2014-05-06","dateReceived":"2014-05-12","sender":"me","companyName":"新人民军","re​​sponsibleStaffName":"Man, Alf B","subject ":"testing","actionDone":"round","track":"1","completed":"0","re​​marks":"round"}]}

标签: jquery ajax arrays codeigniter


【解决方案1】:

不要字符串化。解析。根据您返回的对象....

$.ajax({
url: "viewDayDocuments",
type: 'post',
data: {currentDay: currentDay, currentMonth: currentMonth, currentYear: currentYear},
success: function(result){
    $('.list').text('');
    $('.list').remove();
    incomingDoc = JSON.parse(result);

    incomingDoc.details[/*indexhere*/].referenceNo; //gives you a reference number.

   }
 });

我会让你来循环遍历incomingDoc.details 的索引。

【讨论】:

  • 哦,原来如此......谢谢......为什么不顺便串一下呢?只是好奇,因为我在环顾 2D 数组和 ajax 时看到它被提及
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 阅读有关内置函数的信息。 :) 阅读可以帮助你学得更好。
  • 感谢您提供的链接,我需要阅读更多内容,尤其是一般的 javascript
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-11
  • 2012-06-01
相关资源
最近更新 更多