1

I am starting to learn javascript.

Controller file:

 public function index(){  

    $this->load->view('crud_view.php');

    $this->load->model('pushnotification_model');

    $data['jsonData'] = json_encode($this->pushnotification_model->search_user());

    $this->load->view('pushnotification_group_wise_view',$data);
}

search_user is a method in the model pushnotification_model which retrieves data from the database and returns back to the controller for json encoding.

Now in the pushnotification_group_wise_view I have a table like below:

  <div id="showUserData" data-bind="visible: records().length > 0">
  <table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>GCM REG ID</th>
            <th>EBL SKY ID</th>
        <th style="text-align:center"><button data-bind="click :$root.processAll" class="btn btn-primary">Select All</button></th>
        </tr>
    </thead>
    <tbody data-bind="foreach: records">
        <tr>
            <td data-bind="text:gcm_regid"></td>
            <td data-bind="text:eblSkyId"></td>
            <td style="text-align:center"><input type="checkbox" data-bind="checked: isProcessed"></td>
        </tr>
    </tbody>
  </table>

Now I want to populate the table with the $data using javascript. How can I do that?

1
  • 1
    Why do you need to do this with javascript? The view is loaded and the html is outputted. You have $data['jsonData'] in the scope of your template. Just iterate over it in the html. To do this with ajax, you should have a controller that outputted only the json data and your view should have a javascript that, via ajax request, call this controller. Commented Dec 11, 2014 at 10:53

1 Answer 1

1

Since your pushnotification_group_wise_view is already have the jsonData you can do it using php no need to use javascript, but To do it using JavaScript you will need an endpoint to access the data result in json format

in controller

public function notification(){  

   $this->load->model('pushnotification_model');

   //return data as json 
   $this->output
           ->set_content_type('application/json')
           ->set_output( json_encode($this->pushnotification_model->search_user()) );
}

in JS using jQuery

$.getJSON( "/notification", function( data ) {

    $.each( data, function( key, val ) {
         //do the iteration and built the rows of  table for population here 
    });

    //and at last append it to `$('#showUserData table')`
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.