JSON编码到表

时间:2017-11-13 09:21:06

标签: javascript php json

我想将JSON数组显示到表中, 我的db表结构包括:act_id,sales_id,cust_name,act_type,notes,date_add,date_modified,action。

这是脚本(/api.php):

public function get_todo($id = null)
{
    $this->_require_login();   

    if ($id != null){
        $this->db->where([
            'act_id' => $id,
            'user_id' => $this->session->userdata('user_id')
        ]);
    } else {
        $this->db->where('user_id', $this->session->userdata('user_id'));
    }
    $this->db->where('user_id', $this->session->userdata('user_id'));
    $query = $this->db->get('activity');
    $result = $query->result_array();

    $this->output->set_output(json_encode($result));
}

我的event.js

var load_todo = function() {
    $.get('api/get_todo', function(o){

        var output = '';
        for (var i = 0; i < o.length; i++){
            output += Template.todo(o[i]);
        }

        $("#list_todo").html(output);
    }, 'json');
};

这是我的template.js

output +='<table style="width:80%;">';
    output +='<thead>';
    output += '<tr>';
    output +='<th>' + "Client Name" + '</th>';
    output +='<th>' + "Activity" + '</th>';
    output +='<th>' + "Notes" + '</th>';
    output +='<th>' + "Start" + '</th>';
    output +='<th>' + "Finish" + '</th>';
    output +='<th>' + "Action" + '</th>';
    output +='</tr>';
    output += '</thead>';

    output +='<tbody>';
    output += '<tr>'
    output +='<td>' + obj.cust_name + '</td>';
    output +='<td>' + obj.act_type + '</td>';
    output +='<td>' + obj.act_notes + '</td>';
    output +='<td>' + obj.date_added +'</td>';
    output +='<td>' + obj.date_modified + '</td>';
output += '<td>' + '<a class="todo_update" data-id="' + obj.act_id + '"data-completed="' + data_completed + '" href="api/update_todo">' + data_completed_text + '</a>' + '</td>';
    output += '</tr>';
    output +='</tbody>'; output +='</table>';

但是它会在每个行视图中创建重复的列名 enter image description here

任何帮助都会非常感激。谢谢。

1 个答案:

答案 0 :(得分:0)

保持表格的标题部分不在循环中。我的建议是首先将程序分成两半,对头部分进行编码,查看其输出,然后对其余数据进行编码,然后根据需要加入。