我想使用codenigator在数据表中添加编辑和删除按钮

时间:2018-05-04 07:53:26

标签: javascript php ajax codeigniter

我想使用codenigator在数据表中添加编辑和删除按钮。这是我的控制器代码,我在显示数据。现在我想添加按钮,但可以制作如何打印这些按钮的逻辑。我正在使用codeginator

CONVERT(expr,type), CONVERT(expr USING transcoding_name)

这是我的观点

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

4 个答案:

答案 0 :(得分:0)

我认为这可能有用

控制器中的

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

并在视野中:

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

答案 1 :(得分:0)

你也可以在你的代码中尝试这个:

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

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

           );
      }

答案 2 :(得分:0)

在表格中添加另一列以进行编辑和删除按钮。 (例如行动栏)。 只需将列数与$data变量

中的数据数相匹配即可
$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>'
               );
          }

答案 3 :(得分:0)

您可以添加帮助程序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> 
           ";
}

并在控制器中调用

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