如何在数据表中创建自定义数据过滤器?,
然后datatables将在datatables中显示选项框的值:
我从数据库
中的表dept显示连接中的数据(Department)在我的数据表中我在数据库中使用了表emp
这是我的代码选项框
<div class="row">
<div class="col-md-6">
<form action="<?=site_url('proses/pindah_departemen');?>" method="get">
<div class="panel panel-primary">
<div class="panel-heading">Filter Data</div>
<div class="panel-body">
<table class="table table-bordered">
<tr>
<td width="20%">Pilih Departemen</td>
<td width="30%">
<select class="form-control" name="filter_departemen">
<option value="" <?php if(empty($this->input->get('filter_departemen'))) echo 'selected';?>>Show All data</option>
<?php
foreach($groups as $city)
{
$selected = $this->input->get('filter_departemen')==$city['dept_id_auto'] ? 'selected':'';
echo '<option value="'.$city['dept_id_auto'].'" '.$selected.'>'.$city['dept_name'].'</option>';
}
?>
</select>
</td>
</tr>
</table>
</div>
<div class="panel-footer text-right">
<button type="submit" class="btn btn-primary">Tampilkan Data</button>
</div>
</div>
</form>
</div>
</div>
这是我的控制器命名(Proses):
public function pindah_departemen()
{
$data = array(
'title' => 'Pindah Departemen',
'data' => $this->Pindah_dept_model->GetSiswa($this->input->get('filter_departemen'))
);
$data['groups'] = $this->Pindah_dept_model->getAllGroups();
$this->template->load('template','proses/pindah_departemen', $data);
}
这个我的模型命名(Pindah_dept_model):
var $table = 'emp';
public function GetSiswa()
{
/*i dont know exactly your query. please fix this is this wrong*/
$this->db->select(array('emp_id', 'first_name', 'nik', 'gender', 'pin', 'dept_name'))
->from('emp AS e')
->join('dept AS d','d.dept_id_auto = e.dept_id_auto', 'left');
if(!empty($dep)) $this->db->where('d.dept_id_auto', $dep);
$data = $this->db->order_by('emp_id','ASC')->get();
return $data->result_array();
}
public function getAllGroups()
{
$query = $this->db->query('SELECT dept_id_auto,dept_name FROM dept');
return $query->result_array();
}
答案 0 :(得分:0)
如果我已正确理解该问题,您需要在selectbox的“getAllGroup”函数中添加一个过滤器。如果我错了,请纠正我。
您应该为select添加一个名称,我将其命名为“dept_filter”。
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">Filter Data</div>
<form action="" method="post">
<div class="panel-body">
<table class="table table-bordered">
<tr>
<td width="20%">Pilih Departemen</td>
<td width="30%">
<select class="form-control" name="dept_filter">
<?php
foreach($groups as $city)
{
echo '<option value="'.$city['dept_id_auto'].'">'.$city['dept_name'].'</option>';
}
?>
</select>
</td>
</tr>
</table>
</div>
<div class="panel-footer text-right">
<input type="submit" class="btn btn-primary" value="Tampilkan Data">
</div>
</form>
</div>
</div>
</div>
在你的Proses控制器中:
public function pindah_departemen()
{
//we take the current selected option (return false when none)
$filter_dept = $this->input->post('dept_filter');
//and we pass it to the model
$data = array(
'title' => 'Pindah Departemen',
'data' => $this->Pindah_dept_model->GetSiswa($filter_dept)
);
$data['groups'] = $this->Pindah_dept_model->getAllGroups();
$this->template->load('template','proses/pindah_departemen', $data);
}
在您的模型中:我已在codeigniter查询生成器语法中转换您的SQL查询。
/*to avoid any conflict if this function is used anywhere else,
I made the param optionnal, so if there is none,
the filter is set to "false"*/
public function GetSiswa($filter = FALSE)
{
$this->db->select();
$this->db->from($this->table); //here "emp"
//do the same join as your query
$this->db->join('dept', 'debt.dept_id_auto = emp.dept_id_auto');
//when the filter isn't false, we add a "where" condition
if($filter){
$this->db->where("dept_id_auto", $filter);
}
$this->db->order_by("emp_id", "DESC");
$query = $this->db->get();
return $data->result_array();
}
Udpate:代码更新为当前问题。
Udpate2:在条件
的SQL中为“dept_id_auto”更改“dept_name”如果你没有取得好成绩:
<select>...</select>
?<form>
吗?
method="post"
使用<form>
吗?var_dump($this->input->post());
echo $this->db->last_query();
在回归之前?答案 1 :(得分:0)
你的过滤器html
<div class="row">
<div class="col-md-6">
<form action="<?=site_url('proses/pindah_departemen');?>" method="get">
<div class="panel panel-primary">
<div class="panel-heading">Filter Data</div>
<div class="panel-body">
<table class="table table-bordered">
<tr>
<td width="20%">Pilih Departemen</td>
<td width="30%">
<select class="form-control" name="filter_departemen">
<option value="" <?php if(empty($this->input->get('filter_departemen'))) echo 'selected';?>>Show All data</option>
<?php
foreach($groups as $city)
{
$selected = $this->input->get('filter_departemen')==$city['dept_id_auto'] ? 'selected':'';
echo '<option value="'.$city['dept_id_auto'].'" '.$selected.'>'.$city['dept_name'].'</option>';
}
?>
</select>
</td>
</tr>
</table>
</div>
<div class="panel-footer text-right">
<button type="submit" class="btn btn-primary">Tampilkan Data</button>
</div>
</div>
</form>
</div>
</div>
你的控制器
public function pindah_departemen()
{
// MASUKKAN PARAMETER DATA DISINI, BIASANYA HASIL DARI QUERY
$data = array(
'title' => 'Pindah Departemen',
'data' => $this->Pindah_dept_model->GetSiswa($this->input->get('filter_departemen'))
);
$data['groups'] = $this->Pindah_dept_model->getAllGroups();
$this->template->load('template','proses/pindah_departemen', $data);
}
你的模特
var $table = 'emp';
public function GetSiswa($dep=NULL)
{
$this->db->select(array('emp_id', 'first_name', 'nik', 'gender', 'pin', 'dept_name')) //or select(array('emp_id, first_name, nik, gender, pin, dept_name')
->from('emp AS e')
->join('dept AS d','d.dept_id_auto = e.dept_id_auto', 'left');
if(!empty($dep)) $this->db->where('dept_id_auto', $dep);
$data = $this->db->order_by('emp_id','ASC')->get();
return $data->result_array();
}
public function getAllGroups()
{
$query = $this->db->query('SELECT dept_id_auto,dept_name FROM dept');
return $query->result_array();
}
public function get_by_id_siswa($id)
{
$this->db->from($this->table);
$this->db->where('emp_id',$id);
$query = $this->db->get();
return $query->row();
}
建议:下次,plz在再次构建新应用之前学习jquery和ajax。