Datatables Jquery does not render column button in Codeigniter

Asked

Viewed 231 times

0

I’m trying to put action buttons on my table through the datatTable plugin.

My Javascript code

$(document).ready(function(){

    var table = $('#dataTable').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": {
            "contentType": "application/json",
                "url": "http://localhost:8000/usuario/listar"                           
        },
        "columnDefs": [{
            "targets": -1,
            "data": null
            }
        }]
    });


});

and my Codeigniter Controller

<?php


class Usuario extends CI_Controller {


    public function __construct(){
        parent::__construct();
        $this->load->model('UsuarioModel');
    }



    public function listar(){
        $data = $this->UsuarioModel->all()->result();
        $row = [];
        foreach($data as $dados){
            $row[] = array($dados->nome, $dados->endereco);

        }
        $d['data'] = $row;
        echo json_encode($d);
    }



}

The idea is that by Datatable it create action buttons with "edit", "remove", "view" with the id on the button of my json reply.

  • By the code presented you are not creating the buttons, for Datatables. You can do this creation by PHP itself.

1 answer

1


Replace that part of your code:

            $row[] = array($dados->nome, $dados->endereco);

for that:

           $row[] = $dados->nome;
           $row[] =  $dados->endereco;
           $row[] = "<a href='" . base_url() . "controle/editar/" . $dados->id . "'class='btn btn-primary' >Editar</a>";
  • Explain your code, sometimes the person does not understand, does not know where to put, does not know any function, etc.

  • I’ve already made the change

Browser other questions tagged

You are not signed in. Login or sign up in order to post.