在另一个表的td中显示表onclick

时间:2018-03-29 17:54:11

标签: jquery codeigniter

我有一个表,其中包含多次查找数据的行。单击具有该编号的链接后,将部署第二个表以显示行数。如果表只有一行但是如果在单击链接后它有多个行,那么此操作将完美地运行,该表不仅在相关行中的所有行中进行部署。我该如何解决这个问题?

1- table

<table >
<tr>
<th>date</th>
<th>medicine</th>
</tr>
<?php foreach($rows as $row)
{
?>
<tr>
<td><?=$row->date_val;?></td>
<td>
<?=$row->medicine;?>
<a id="<?=$row->medicine?>"  class="deploy-medicine" title="Deploy" style="cursor:pointer"><?=$row->Total;?></a>
<div  class="toggle-medicine" style="display:none"></div>
</td>
</tr>

<?php
}
?>

</table>

2-JQUERY

$('.deploy-medicine').click(function() {
var medicine = $(this).attr('id');
$.get( "<?php echo base_url();?>admin/get_medicine_name?medicine="+medicine, function( data ){
$(".toggle-medicine").html(data).slideToggle("slow"); 
}); 

});

3-控制器

public function get_medicine_name()
{
$medicine =$this->input->get('medicine');
$data['medicine'] = $this->model_admin->get_medicine($medicine);
$this->load->view('medicine',$data);
}

4- MODEL

function get_medicine($data)
{
$this->db->select('inserted_time,operator');
$this->db->from('table_medicine');
$this->db->where('medicine',$data);
$query = $this->db->get();
return $query->result();    
}

5-点击

后包含表格的视图
<table>
<tr>
<th>date</th>
<th>Operador</th>
</tr>
<?php foreach($medicine as $row)

{
?>
<tr>
<td><?=$row->inserted_time?></td>
<td><?=$row->operator?></td>
</tr>
<?php } ?>
</table>

1 个答案:

答案 0 :(得分:0)

您需要找到距离最近的td并找到.toggle-medicine选择器。

这将有效:

$(document).ready(function() {
  $(".deploy-medicine").click(function() {
    console.log('deploying...');
    var medicine = $(this).attr("id");
    var el = $(this).closest('td').find('.toggle-medicine');
    el.html("Hello World").show();
  });
});

示例:https://codepen.io/anon/pen/KoowPy