【问题标题】:Sent jQuery var value to Scala Play route function将 jQuery var 值发送到 Scala Play 路由函数
【发布时间】:2015-07-12 05:22:35
【问题描述】:

我有一些路线和功能看起来像:

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 值?

【问题讨论】:

  • 经过进一步研究,您可以使用更多代码执行与原始问题类似的操作。我不在电脑旁,所以稍后会更新。这个链接应该可以帮助semisafe.com/coding/2015/03/31/…

标签: javascript jquery scala playframework-2.0


【解决方案1】:

我无法准确说出您的目的,所以这里有几个选项:

纯 jQuery 无播放功能

$.get('http://localhost:9000/item-state/' + data,function(success){

 //handle logic here.Not sure if you need to append the string id to data but hopefully this makes sense
});

Play 的 javascript 路由器

Play 有两种方式来生成 javascript 路由器:

  1. 使用 * @javascriptRouter* 指令嵌入到 Scala 模板中
  2. 在您的网页可以使用它们的操作中

有用的链接:

Play docs on JSRouting

Nice tutorial with recent Play version (2.3.4)

【讨论】:

  • 没有看到任何活动 - 您需要更多信息吗?
【解决方案2】:

好吧,您可以借助 AJAX GET 调用轻松完成此操作。每当该人选择该行时,它将生成一个 ajax 获取请求,然后该请求将路由到控制器功能。

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 框架来处理产品的 web 应用程序,我们已经做这些事情很长一段时间了。所以,这是一个可行的解决方案。此外,明确说明您的需求。有任何疑问,请随时提出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2018-04-10
    • 2021-03-13
    • 2017-09-21
    • 1970-01-01
    相关资源
    最近更新 更多