用于编辑/删除按钮的数据表(代码点火器) - 在 href 标签上传递参数



>有人知道如何传递要编辑/删除的行的 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,我可以转到编辑页面。我已经包括了视图和控制器。现在,如何通过"编辑"按钮传递它来获取它? 谢谢!

您可以在 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>'
);
}

您还需要更改要删除的网址

您需要从表中添加行 ID 或分支代码,如下所示:

$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 参数。

相关内容

  • 没有找到相关文章

最新更新