0

我有一個參數模板是這樣的:獲取模板的參數在播放框架

@(people: List[models.Person]) 
<html> 
<head>  
</head> 
<body> 
<table class="centered" id="table_people"> 
<thead> 
    <tr class="table_header"> 
     <th></th> 
     <th class="people_column">Name</th> 
     <th class="people_column">Active</th> 
    </tr> 
</thead> 
<tbody>  
    @for(p <- people) { 
    <tr> 
     <td><button id="timesheet_view_" >View</button</td> 
     <td><b>@p.getName()</b></td> 
     <td>@p.isActive()</td>   
    </tr>   
    } 
</tbody> 
</table> 

該代碼顯示的人的名單。現在,我想單擊按鈕視圖時,它會顯示人的信息。要做到這一點,我必須寫這樣的事情:

<script> 
    $("#timesheet_view_<%= people[i].id %>") 
     .button() 
     .click(function() { 
     people = '<%= people[i].name %>'; 
     showTimeSheet('current', '<%= people[i].name %>');    
      }) 
    </script> 

但我找不到如何在Javascript中獲取模板的價值人蔘數。我嘗試使用@字符,但它不起作用。

回答

0

你不能那樣做。 Javascript運行在客戶端,而Play運行在服務器端。

當您的Javascript正在運行時,頁面已經被渲染並且Play已經完成了它的一部分。

您可以做的是從people中捕獲相關數據並將其放入數據屬性中。

<tbody>  
    @for(p <- people) { 
    <tr data-people-id="@p.getId()" data-people-name="@p.getName()"> 
     <td><button class="timesheet_view" >View</button</td> 
     <td><b>@p.getName()</b></td> 
     <td>@p.isActive()</td>   
    </tr>   
    } 
</tbody> 

而在腳本中,您可以獲取數據屬性。

<script> 
    $(".timesheet_view") 
    .button() 
    .click(function() { 
     var row = $(this).closest("tr"); 
     var peopleId = row.data("peopleId"); 
     var peopleName = row.data("peopleName"); 
     // Do whatever you want with this data... 
     // Not sure how the showTimeSheet is working so leave it up to you 
     //showTimeSheet('current', '<%= people[i].name %>');    
    }); 
</script> 
1

首先,你必須解決您的HTML代碼: <td><button id="timesheet_view_" >View</button</td>
正確關閉按鈕元素:</button以 '>'。

其次,你必須給每個按鈕一個唯一的ID:

您的代碼:

@for(p <- people) { 
    <tr> 
    <td><button id="[email protected]">View</button></td> 
    <td><b>@p.getName()</b></td> 
    <td>@p.isActive()</td>   
    </tr>   
} 

<script> 
    $(document).ready(function() { 
    // There are better ways to do this, but it's just for your example 
    @for(p <- people) { 
     $("#[email protected]").click(function(e) { 
     ... // your javascript code here 
     }); 
    } 
    }); 
</script> 

在我看來,最好使用一個鏈接:

@for(p <- people) { 
    <tr> 
    <td><button id="timesheet_view_" >View</button</td> 
    <td><b>@p.getName()</b></td> 
    <td>@p.isActive()</td>   
    </tr>   
} 

與試用這裏(而不是按鈕),因爲對於每個按鈕,您需要爲onclick事件編寫額外的javascript代碼。嘗試一個鏈接和玩!會做的工作適合你:

@for(p <- people) { 
    <tr> 
    <td><a class="btn" href="@routes.Application.view(p.id)">View</a></td> 
    <td><b>@p.getName()</b></td> 
    <td>@p.isActive()</td>   
    </tr>   
} 

順便說一句,如果你使用Twitter的引導,你可以使用class="btn"和你的鏈接看起來像一個按鈕。

開始玩吧!應用程序並查看HTML源代碼。你會看到你的代碼<button id="[email protected]">View</button>的意志是這樣的:

<button id="timesheet_view_1">View</button>

<button id="timesheet_view_2">View</button>

...

<button id="timesheet_view_9">View</button>

希望這有助於。

最好的問候, gfjr

+0

我不認爲你的答案是正確的,人們參數未利用的腳本標記。 – Riddle 2014-02-21 15:05:38