如何在ajax中访问2D数组

时间:2014-05-12 06:24:10

标签: jquery ajax arrays codeigniter

所以基本上我的函数的返回值是一个2D数组,我打算显示每一行和每一行;但是,我无法单独访问它们,即行[0] [0]我已尝试过类似问题中的字符串化功能,尽管它没有运气使其工作。这是代码:

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);

返回2D数组的模型代码:

        $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>");
        }      

1 个答案:

答案 0 :(得分:2)

请勿进行字符串化。语法分析。基于您返回的对象....

$.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的内容。