如何在控制器视图中显示消息框?

发布于 2024-12-03 09:58:42 字数 225 浏览 0 评论 0原文

用户在表单上输入一些数字和其他数据后,就会计算出总计。用户还可以选择输入折扣金额。我使用 jQuery AJAX 方法将所有数据发送到 Load 控制器中的 getTotal 方法。如果用户输入的折扣金额大于计算出的总额,我希望弹出一个消息框(类似于 JavaScript alert 框),说明折扣必须小于总额。有没有什么干净的方法可以从控制器执行此操作?

After a user enters some numbers and other data on a form a total is calculated. The user might also choose to enter a discount amount. I use a jQuery AJAX method to send all the data to the getTotal method in the Load controller. If the user entered a discount amount greater than the calculated total I want a message box to pop up (similar to a JavaScript alert box) saying the discount must be less than the total. Is there any clean way to do this from the controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光礼记 2024-12-10 09:58:42

不。您需要在 ajax 响应中发送回结果,告诉客户端显示您的消息框。然后您可能会使用 JavaScript 来显示它。有很多用于显示消息框的 jQuery 插件。例如:jQueryUI 的对话框

No. You'd want to send a result back in your ajax response that tells the client to display your message box. Then you'd probably use JavaScript to show it. There are a lot of jQuery plugins for displaying message boxes. For example: jQueryUI's Dialog.

分開簡單 2024-12-10 09:58:42

这就是我所做的。在我看来,我的 JavaScript 方法之一中有以下代码。我使用 jQuery 的 getJSON 而不是 ajax 因为它似乎更适合(更干净)。

$.getJSON(
  "/truckingmanagement/load/getTotal",
  {cargoSource:cargoSource, cargo:cargo, haulRate:haulRate, tonnage:tonnage, mileage:mileage, discount:discount, taxExempt:taxExempt},
  function(result) {
    if(result.message != null){
      alert(result.message);
      $("#discount").val("");
      $("#totalCell").html(result.total);
    }
    else{
      $("#totalCell").html(result.total);
    }
  });

在我的控制器中,我仍然在闭包中使用 render 方法,但将其修改为 JSON,确保 import grails.converters.*

render(contentType:"text/json") {
    total = g.textField(name: 'total', value: totalBill, readonly: 'readonly')
    message = errorMessage
}

totalBill 值是在调用 render 之前计算出来的,而 errorMessage 只是一个字符串,其中包含基于错误内容的消息(负数)折扣值或折扣值大于总计)或根本没有消息。因此,如果消息为null,则不会显示任何消息。

Here is what I did. In my view I have the following code in one of my JavaScript methods. I use jQuery's getJSON instead of ajax because it seemed to fit better (cleaner).

$.getJSON(
  "/truckingmanagement/load/getTotal",
  {cargoSource:cargoSource, cargo:cargo, haulRate:haulRate, tonnage:tonnage, mileage:mileage, discount:discount, taxExempt:taxExempt},
  function(result) {
    if(result.message != null){
      alert(result.message);
      $("#discount").val("");
      $("#totalCell").html(result.total);
    }
    else{
      $("#totalCell").html(result.total);
    }
  });

I my controller I still used the render method inside my closure, but modified it for JSON, making sure to import grails.converters.*.

render(contentType:"text/json") {
    total = g.textField(name: 'total', value: totalBill, readonly: 'readonly')
    message = errorMessage
}

The totalBill value has been calculated prior to calling render, and errorMessage is just a string that contains a message based on what the error was (negative discount value or discount value greater than total) or no message at all. Thus if the message is null no message will be displayed.

残疾 2024-12-10 09:58:42

我认为从技术上讲,让控制器生成 javasacript 作为对 ajax 调用的响应是可能的,但这是一种非常丑陋的方法。

更好的是发送 JSON。类似于:

result = []
result.success = total >= discount
result.total = total
render result as JSON

在 Ajax 调用中,检查是否成功,如果成功则显示总数,如果失败则显示消息。

I think it is technically possible to have your controller generate javasacript as response to an ajax call, but that'd be a very ugly way to do it.

Better is to send JSON. Something like:

result = []
result.success = total >= discount
result.total = total
render result as JSON

And in the Ajax call, check for success, show the total if successful, show the message if not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文