简体   繁体   中英

I want to add edit and delete buttons in datatables using codenigator

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>

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>

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>"

           );
      }

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

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM