2015-02-06 130 views
0

我正在使用PHP Phalcon。获得第一个值在“点击”并传递给控制器​​

我有一张桌子的视图。该表具有动态的行数。我想单击一行并获取第一列中的值。这是我的HTML表格:

<?php 
     $counter = count($prescriptions); 
     $i = 0; 
     if ($counter == 0) 
     { echo "You have no earlier prescriptions." ;} 
     else { 
     ?> 

<Table class="inbox_table" cellspacing="0" style="width: 998px"> 
    <tr> 
     <td class="inbox_table_cell inbox_table_heading">ID</td> 
     <td class="inbox_table_cell inbox_table_heading">Doctor ID</td> 
     <td class="inbox_table_cell inbox_table_heading">Patient Name</td> 
     <td class="inbox_table_cell inbox_table_heading">Ailment</td> 
     <td class="inbox_table_cell inbox_table_heading">Date</td> 
    </tr> 

    <?php 
      While ($i < $counter) 
      { 
      ?> 
    <tr onclick=""> 
     <td class="inbox_table_cell" id="ID"><?php echo $prescriptions[$i]->ID; ?></td> 
     <td class="inbox_table_cell" id="Doc_ID"><?php echo $prescriptions[$i]->Doctor_ID; ?></td> 
     <td class="inbox_table_cell" id="P_Name"><?php echo $patient_name; ?></td> 
     <td class="inbox_table_cell" id="Ailment"><?php echo $prescriptions[$i]->Ailment; ?></td> 
     <td class="inbox_table_cell" id="Date"><?php echo $prescriptions[$i]->Date_; ?></td> 
    </tr> 
    <?php 
      $i++; 
      } 
      } 
      ?> 
</Table 

> 

这里是相关的操作方法:

public function PrescriptionTableAction(){ 
    //Select the earlier prescriptions of the online patient. 

    $current_PID = $this->session->current_patient_ID; 

    $get_prescriptions = "Select Doctor_ID,Patient_ID,Ailment,ID,Date_ from Prescriptions where Patient_ID = '$current_PID'"; 
    $this->view->prescriptions = $this->modelsManager->executeQuery($get_prescriptions); 

    $this->view->patient_name = $this->session->current_patient->Full_Name; 
} 

我想要做什么:

抽象,当我点击一个行的表,它开启了完整处方的视图,其中包括药物名称和每种药物的说明,写处方的医生的名字等等。

更具体地说,当我点击一行时,我想从“ID”列中获取该行的值。 (它对应于处方表数据库中的主键)。我想将此值传递给同一控制器中的另一个操作方法,其中可以从数据库中提取处方的详细信息,然后显示在相应的视图中。

我已经阅读了本网站上的类似问题和解决方案,但几乎所有的解决方案都提供了“警报”。我想把值传回控制器,我该怎么做?

+0

但阿贾克斯是javascript – 2015-02-06 19:08:11

+0

编辑我的问题。我在脚本编写方面经验不足。我注意到人们在单独的标题下分别提供“Js”和“AJAX”解决方案。 – 2015-02-06 19:11:07

+1

您可以将它作为参数传递给http://api.jquery.com/jquery.ajax/'$ .ajax()'方法,因为它看起来像是在使用jQuery。具体查看底部'$ .ajax附近的示例(类型:“POST”, url:“/ controller/view /”, data:{id:$(this).find('td:first')) .text()},success:doStuff() })'将数据发送到您的控制器您的控制器应该返回部分html,而不是一个完整的页面,您可以更新某种容器div /元素与返回数据使用'doStuff()'方法。如果你愿意,你可以显示完整视图,但我不认为这就是你想要的。 – 2015-02-06 19:56:18

回答

1

我已经以我想要的方式解决了问题;使用简单的JavaScript。

这是我的脚本功能:

function func(e){ 
var id = e.target.parentNode.firstElementChild.innerText; 
      window.location.href = "ActionMethod?id=" + id; 
} 

并在视图我做了这个编辑:

<tr onclick="func(event)"> 
... 
</tr> 

这解决了我的问题!