2015-02-06 77 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> 

這解決了我的問題!

相關問題