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