0

I want to add edit and delete buttons in datatables using codenigator . This is my controller code where i am displaying the data. Now i want to add buttons but can made the logic that how to print those buttons. I am using codeginator

public function dataa()
    {
        $draw = intval($this->input->get("draw"));
          $start = intval($this->input->get("start"));
          $length = intval($this->input->get("length"));
          $this->load->model('mymodel');
          $this->load->library('Datatables');
          $this->Datatables->add_column('edit','<a href="#">edit</a>');


          $record = $this->mymodel->records();

          $data = array();

          foreach($record->result() as $r) {

               $data[] = array(
                    $r->email,
                    $r->name,
                    $r->fathername,
                    $r->phone 
               );
          }

          $output = array(
               "draw" => $draw,
                 "recordsTotal" => $record->num_rows(),
                 "recordsFiltered" => $record->num_rows(),
                 "data" => $data

            );
          echo json_encode($output);
          exit();
    }

and this is my view

<script type="text/javascript">
$(document).ready(function() {
    $('#record-table').DataTable({
        "ajax": {
            url : "<?php echo site_url("mycontroller/dataa") ?>",
            type : 'GET'
        },

    });
});
</script>

4 Answers4

0

I think this might be useful

in your Controller

if (!empty($record->result())) {
        foreach ($record->result() as $r) {
            $nestedData['email'] = $r->emailemail;
            $nestedData['name'] = $r->name;
            $nestedData['fathername'] = $r->fathername;
            $nestedData['phone'] = $r->phone;
            $nestedData['action'] = "<a href='#'></a>";

            $data[] = $nestedData;
        }
    }

    $json_data = array(
        "draw" => intval($this->input->post('draw')),
        "recordsTotal" => intval($totalData),
        "recordsFiltered" => intval($totalFiltered),
        "data" => $data
    );

    echo json_encode($json_data);

and in view:

<script type="text/javascript">
    $(document).ready(function() {
        $('#record-table').DataTable({
            "ajax": {
                url : "<?php echo site_url("mycontroller/dataa") ?>",
                type : 'POST'
            },
            columns: [
                        {data: "email"},
                        {data: "name"},
                        {data: "fathername"},
                        {data: "phone"},
                        {data: "action"},
                    ],

        });
    });
    </script>
Let's Enkindle
  • 57
  • 2
  • 17
  • Its not loading my data – Qadeer Malik May 04 '18 at 08:23
  • Error in response to storage.get: TypeError: Cannot read property '1' of null at getWebspeed (chrome-extension://pgjjikdiikihdfpoppgaidccahalehjh/webspeed.js:83:72) at Object.chrome.storage.local.get [as callback] (chrome-extension://pgjjikdiikihdfpoppgaidccahalehjh/webspeed.js:95:31) at Object.callback (chrome-extension://pgjjikdiikihdfpoppgaidccahalehjh/webspeed.js:94:29) jquery.min.js:2 POST http://ziksoft.com/task2/mycontroller/dataa 500 (Internal Server Error) – Qadeer Malik May 04 '18 at 09:21
0

you can also try this in your code:

foreach($record->result() as $r) {

           $data[] = array(
                $r->email,
                $r->name,
                $r->fathername,
                $r->phone ,
                "<a href='#'>edit</a>"

           );
      }
Let's Enkindle
  • 57
  • 2
  • 17
  • gives me that error Failed to load resource: the server responded with a status of 500 (Internal Server Error) /favicon.ico:1 Failed to load resource: the server responded with a status of 404 (Not Found) – Qadeer Malik May 04 '18 at 09:13
0

Add another column to your table for your edit and delete button. (ex. action column). just match the number of columns to the number of data inside your $data variable

$data = array();

          foreach($record->result() as $r) {

               $data[] = array(
                    $r->email,
                    $r->name,
                    $r->fathername,
                    $r->phone,
                    '<a href="#">edit</a>',
                    '<a href="#">delete</a>'
               );
          }
Drenyl
  • 906
  • 1
  • 18
  • 41
  • @QadeerMalik If you are using google chrome as browser, you can check developer tools. Go to network tab. refresh your page first then try to load the page where the datatable should be load. It will show the error/response – Drenyl May 04 '18 at 09:12
  • try to remove this line`$this->Datatables->add_column('edit','edit');` , Have you add another column for your `edit` and `delete` button?. just like for email,name, etc – Drenyl May 04 '18 at 09:14
  • DataTables warning: table id=record-table - Requested unknown parameter 'email' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4 – Qadeer Malik May 04 '18 at 09:28
0

you can add helper database_helper

function callback_delete($id)
{
   return "<button class='btn btn-danger delete' data-
           id='{$id}'>Delete</button>";
}

function callback_edit($id)
{
    return "<button class='btn btn-primary edit' data-id='{$id}' data-
           toggle='modal' data-target='#editInsertmodal'>Edit</button> 
           ";
}

and call like this in controller

public function get_user()
{
    $this->load->helper('database');
    $this->datatables-
>select('user_id,user_firstname,user_lastname,user_email')
        ->from('users')
        ->add_column('Edit','$1','callback_edit(user_id)')
        ->add_column('Delete','$1','callback_delete(user_id)');

    echo $this->datatables->generate();
}
Vijay Makwana
  • 911
  • 10
  • 24