1

Hi I successfully implemented data table on codeigniter but I got one problem I need on my last column table to have an action buttons like edit,delete

here's how I made my data table on codeigniter

On View

<div class="table-responsive">
        <table id="table" class="table">
            <thead>
                <tr>
                    <th>Loan</th>
                    <th>Loan Type</th>
                    <th>Loan Amount</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
            <tfoot>
                <tr>
                    <th>Loan</th>
                    <th>Loan Type</th>
                    <th>Loan Amount</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Action</th>
                </tr>
            </tfoot>
        </table>

<script type="text/javascript">

    var table;

    $(document).ready(function() {

        //datatables
        table = $('#table').DataTable({ 

            "processing": true, //Feature control the processing indicator.
            "serverSide": true, //Feature control DataTables' server-side processing mode.
            "order": [], //Initial no order.

            // Load data for the table's content from an Ajax source
            "ajax": {
                "url": "<?php echo site_url('home/ajax_list')?>",
                "type": "POST"
            },

            //Set column definition initialisation properties.
            "columnDefs": [
            { 
                "targets": [ 0 ], //first column / numbering column
                "orderable": false, //set not orderable
            },
            ],

        });

    });
    </script>

Now here's my script from my controller where in I pass my data through json.

public function ajax_list()
    {
        $list = $this->employee->get_datatables();
        $data = array();
        $no = $_POST['start'];
        foreach ($list as $customers) {
            $no++;
            $row = array();
            $row[] = $no;
            $row[] = $customers->loan;
            $row[] = $customers->loan_type;
            $row[] = $customers->loan_amount;
            $row[] = $customers->firstname;
            $row[] = $customers->lastname;

            $data[] = $row;
        }

        $output = array(
                        "draw" => $_POST['draw'],
                        "recordsTotal" => $this->employee->count_all(),
                        "recordsFiltered" => $this->employee->count_filtered(),
                        "data" => $data,
                );
        //output to json format
        echo json_encode($output);
    }

Is it possible to add an action buttons on datatables?

3
  • Try this, this will help you Commented Oct 15, 2019 at 8:57
  • how can save-html-formatted-text-to-database help me sir Commented Oct 15, 2019 at 9:02
  • You can use textarea and type html in that and before save use htmlspecialchars(), this will help to save html in database and while pull back from database use htmlentities() Commented Oct 15, 2019 at 9:05

1 Answer 1

3

If someone might encounter like this problem maybe this would help you.

$row = array();
            $row[] = $no;
            $row[] = $customers->loan;
            $row[] = $customers->loan_type;
            $row[] = $customers->loan_amount;
            $row[] = $customers->firstname;
            $row[] = $customers->lastname;
            $row[] = '<button type="button" name="update" id="'.$customers->id.'" class="btn btn-warning btn-xs update">Update</button>';

I added the button directly from the data array

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.