0

I want to show JSON array into table, my db table structure consist: act_id, sales_id, cust_name, act_type, notes, date_add, date_modified, action.

here are the scripts (/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));
}

my 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');
};

this is my 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>';

however it creates duplicate column name in each rows view enter image description here

any help would really appreciated. thank you.

6
  • 1
    So move header table part out of template. Maybe your templates allow injecting one into another.
    – u_mulder
    Commented Nov 13, 2017 at 9:23
  • 1
    You are probably looping the header every time you are adding a row Commented Nov 13, 2017 at 9:24
  • @u_mulder do you mean I put the header table into the view? if I move it out of the template, how to retrieve the data from the event? thanks
    – Syns
    Commented Nov 13, 2017 at 9:58
  • @Llewellyn1411 I guess you're right, but I still can't find any workaround here. any idea?
    – Syns
    Commented Nov 13, 2017 at 9:58
  • You can put it anywhere, even create another template and append it to your html.
    – u_mulder
    Commented Nov 13, 2017 at 10:00

1 Answer 1

0

Keep the header part of the table out of the loop. My advice is to split the program in two halves first, encode the header part, see it's output and then encode the rest of the data and then join as you require.

1
  • I'd like to, but I can't, the site won't let me (don't have enough point or something) :D
    – Syns
    Commented Nov 16, 2017 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.