Javascript 已将jQuery变量值发送到Scala播放路由函数
我有一些路线和功能看起来像:Javascript 已将jQuery变量值发送到Scala播放路由函数,javascript,jquery,scala,playframework-2.0,Javascript,Jquery,Scala,Playframework 2.0,我有一些路线和功能看起来像: GET/item state/:id controllers.Application.getState(id:String) 我使用jQuery数据表,需要处理行单击。我尝试这样做: $('#statTable tbody').on( 'click', 'tr', function () { $(this).toggleClass('selected'); var data = table.row(this).data(); console.lo
GET/item state/:id controllers.Application.getState(id:String)
我使用jQuery数据表,需要处理行单击。我尝试这样做:
$('#statTable tbody').on( 'click', 'tr', function () {
$(this).toggleClass('selected');
var data = table.row(this).data();
console.log(data);
@routes.Application.getState($(data"id"));
} );
});
但这种方式是错误的。如何在scala函数中使用jQuery var value?我不能确切地说出您的目的,因此这里有几个选项: 普通jQuery无播放功能 Play的javascript路由器 Play有两种生成javascript路由器的方法:
好吧,在AJAX GET调用的帮助下,您可以很容易地做到这一点。每当用户选择行时,它都会生成一个ajaxget请求,该请求将路由到控制器函数
var callurl = "/item-state/{id}";
$('#statTable tbody').on( 'click', 'tr', function () {
$(this).toggleClass('selected');
var data-id = $(document).find('.selected').attr("data-id");
//now by getting the id of the selected row,pass it to the url.
callurl = callurl.replace("{id}",data-id);
$.ajax({
url:callurl,
type:GET,
success:function(data){ //if any data returned from the API,usually a JSON
do-something;
}
error:function(xhr){ //display the error if resource not found or server error on the request
notify-with-message;
}
)};
});
通过这种方式,您可以将jquery或任何脚本变量传递给路由函数。
我的公司正在使用play框架来处理产品的webapp,我们已经做了很长时间了。因此,这是一个有效的解决方案。此外,请明确说明您的需求。如有任何疑问,请随时提问。进一步研究后,您可以使用更多的代码来完成与原始问题类似的操作。我不在电脑旁,所以稍后会更新。这个链接应该有助于你没有看到任何活动-你需要任何进一步的信息吗?
var callurl = "/item-state/{id}";
$('#statTable tbody').on( 'click', 'tr', function () {
$(this).toggleClass('selected');
var data-id = $(document).find('.selected').attr("data-id");
//now by getting the id of the selected row,pass it to the url.
callurl = callurl.replace("{id}",data-id);
$.ajax({
url:callurl,
type:GET,
success:function(data){ //if any data returned from the API,usually a JSON
do-something;
}
error:function(xhr){ //display the error if resource not found or server error on the request
notify-with-message;
}
)};
});