Trying to get data by json and pass it to the view

Trying to get data by json and pass it to the view

GrigoreGrigore Posts: 15Questions: 4Answers: 0
edited January 2015 in Free community support

I'm using CodeIgniter + Datatable and I have the following code

In the view :

<script type="text/javascript">
    $(document).ready(function() {
        // Setup - add a text input to each footer cell
        $('#loggins tfoot th').each( function () {
            var title = $('#loggins thead th').eq( $(this).index() ).text();
            $(this).html( '<input type="text" style="width: 100%" class="form-control input-sm" placeholder="Cautare '+title+'" />' );
        } );

        // DataTable
        var table = $('#loggins').DataTable( {
            dom: 'T<"clear">lfrtip',
            tableTools: {
                "sSwfPath": "<?php echo base_url(); ?>assets/plugins/datatable/TableTools/swf/copy_csv_xls_pdf.swf"
            },
    
            ajax: "<?php echo site_url('profile/test'); ?>",
            "columns": [
                { "aaData": "ip_address" },
                { "aaData": "user_id" },
            ],
            deferRender:    true,
            dom:            "frtiS",
            scrollY:        200,
            scrollCollapse: true,
        } );
     
        // Apply the search
        table.columns().eq( 0 ).each( function ( colIdx ) {
            $( 'input', table.column( colIdx ).footer() ).on( 'keyup change', function () {
                table
                    .column( colIdx )
                    .search( this.value )
                    .draw();
            } );
        } );
    } );
</script>
                <table id="loggins" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th>ip_address</th>
                            <th>user</th>

                        </tr>
                    </thead>

                    <tfoot>
                        <tr>
                            <th>ip_address</th>
                            <th>user</th>
                        </tr>
                    </tfoot>
                </table>

Controller :

    public function test()  {
        $data['aaData'] = json_encode($this->Profile_model->getLoggin($this->user->id));

        return $data;
    }

Modal :

    public function getLoggin() {
        $data = array();

        $this->db->select()
                 ->from('user_logins')
                 ->order_by("login", "DESC");

        $query = $this->db->get();
        if($query->num_rows() > 0)  {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
        }
        return $data;
    }

and a <?php print_r($aaData); ?>

{"id":"440","ip_address":"86.34.130.10","user_id":"1","login":"2015-01-29 14:35:29","deleted":"0"},
{"id":"434","ip_address":"86.34.130.10","user_id":"1","login":"2015-01-29 14:29:44","deleted":"0"},{"id":"433","ip_address":"86.34.130.10","user_id":"1","login":"2015-01-29 14:21:35","deleted":"0"},{"id":"430","ip_address":"86.34.130.10","user_id":"1","login":"2015-01-29 09:22:32","deleted":"0"},{"id":"429","ip_address":"86.34.130.10","user_id":"1","login":"2015-01-29 09:14:10","deleted":"0"},{"id":"428","ip_address":"::1","user_id":"1","login":"2015-01-28 18:09:10","deleted":"0"},{"id":"427","ip_address":"86.34.130.10","user_id":"1","login":"2015-01-28 14:15:02","deleted":"0"},{"id":"424","ip_address":"86.34.130.10","user_id":"1","login":"2015-01-28 08:44:52","deleted":"0"},{"id":"421","ip_address":"86.34.130.10","user_id":"1","login":"2015-01-27 15:07:48","deleted":"0"},

I'm new to datatable and json and dont no the exact way to get data and show it in the view ... I'm trying to integrate Scroller to be able to use datatable with 100k records ...

Error message i get is :

Cannot read property 'error' of null

Thanks a loot for your time !!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin

    Use ajax.dataSrc and set it to be an empty string if you are just returning an array of data.

    Allan

  • GrigoreGrigore Posts: 15Questions: 4Answers: 0

    Sorry but i have tried that and i can't make it work ... any other ideas ?

  • humbughumbug Posts: 9Questions: 1Answers: 0
    edited January 2015

    im just learning myself but this is how i return json from php

      $response = json_encode(array(
            'draw' => intval($_GET['draw']),
            'iTotalRecords' => intval($number_of_stations),
            'iTotalDisplayRecords' => intval($number_of_stations),
            'aaData' => $return_arr
    echo $response;
    

    i think it might be looking for 'draw','itotalrecords' and 'itotaldisplayrecords'.

    I could be completly wrong ofcourse :)

  • GrigoreGrigore Posts: 15Questions: 4Answers: 0

    humbug .. I don't think is a model problem .. since i send the data to the view and can echo it ..

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Can you link to the page you are working on so I can see the current configuration and the exact data that the server is returning please.

    Allan

  • GrigoreGrigore Posts: 15Questions: 4Answers: 0

    Thanks for the support. ATM i don't have it set up on a server .. will do that in weekend and post the link.

This discussion has been closed.