用于“编辑/删除”按钮(Codeigniter)的数据表-在href标记上传递参数

时间:2018-10-05 07:27:35

标签: codeigniter datatable edit

任何人都知道如何传递要编辑/删除的行的ID?我似乎无法弄清楚。

控制器:

公共函数branch_page()      {

$this->load->helper('url');
$this->load->helper('form');
$this->load->model('insertBranch_model');
  // Datatables Variables

$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));

$query = $this->db->select('branchCode,branchName,email,phone,date_created, CONCAT(address1, " ", address2, " ", address3) AS address',FALSE)
                ->where('status','active')
                ->get('branchdetails');

$data = [];

foreach($query->result() as $r) {
   $data[] = array(
            $r->branchCode,
            $r->branchName,
            $r->address,
            $r->email,
            $r->phone,
            $r->date_created,
            $r->href='<a href="editBranch">Edit</a>
            <a href="deleteBranch">Delete</a>'
   );
}

$result = array(
   "draw" => $draw,
    "recordsTotal" => $query->num_rows(),
    "recordsFiltered" => $query->num_rows(),
    "data" => $data
);


echo json_encode($result);
exit();

}

查看:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Branch List</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.13/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.13/datatables.min.js"></script> 
    </head>
    <body>
    <h1>Branch List</h1>
    </body>

<form method="post" action="<?=site_url('main_controller/branch_page/');?>">
<table id="branch-table" class="table table-bordered table-striped table-hover">
<thead>
<tr>
   <th>Branch Code</th>
   <th>Branch Name</th>
   <th>Address</th>
   <th>Email</th>
   <th>Contact Number</th>
   <th>Date Created</th>
   <th>Action</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

<?php echo '<a href = "addBranch">Click Here</a> to add new branch.'; ?>

</form>
</html>

<script type="text/javascript">
$(document).ready(function() {

    $('#branch-table').DataTable({
        "ajax": {
            url : "<?php echo site_url("main_controller/branch_page")?>",
            type : 'GET'
        },
    });
});

</script>

如果我在URL上手动编写了id / branchCode,则可以进入“编辑”页面。我已经包括了视图和控制器。现在,如何通过“编辑”按钮传递它? 谢谢!

2 个答案:

答案 0 :(得分:1)

您可以像下面那样在url中传递变量

foreach($query->result() as $r) 
{
   $data[] = array(
            $r->branchCode,
            $r->branchName,
            $r->address,
            $r->email,
            $r->phone,
            $r->date_created,
            $r->href=''<a href="'.site_url('main_controller/editBranch/'.$r->branchCode).'">Edit</a>
            <a href="deleteBranch">Delete</a>'
   );
}

您还需要更改网址以进行删除

答案 1 :(得分:0)

您需要像这样从表中添加行ID或branchCode:

$query = $this->db->select('branchCode,branchName,email,phone,date_created, CONCAT(address1, " ", address2, " ", address3) AS address',FALSE)
                ->where('status','active')
                ->get('branchdetails');

$data = [];

foreach($query->result() as $r) {
   $data[] = array(
            $r->branchCode,
            $r->branchName,
            $r->address,
            $r->email,
            $r->phone,
            $r->date_created,
            $r->href='<a href="editBranch" data-code="'.$r->branchCode.'" class="linkEdit">Edit</a>
            <a href="deleteBranch" data-code="'.$r->branchCode.'" class="linkDelete">Delete</a>'
   );
}

$result = array(
   "draw" => $draw,
    "recordsTotal" => $query->num_rows(),
    "recordsFiltered" => $query->num_rows(),
    "data" => $data
);


echo json_encode($result);
exit();

然后,您使用jquery来获取数据代码属性,如下所示:

$(".linkEdit").click(function(){
    editBranch($(this).attr("data-code"));
});
$(".linkDelete").click(function(){
    deleteBranch($(this).attr("data-code"));
});

在您的editBranch和deleteBranch函数中,您需要捕获branchCode参数。