简体   繁体   English

将值从控制器传递到模态页面

[英]Passing values from controller to Modal page

Trying to pass my data from controller to modal page. 试图将我的数据从控制器传递到模态页面。

Following this link: https://qtzar.com/2017/03/24/ajax-and-thymeleaf-for-modal-dialogs/ 点击此链接: https//qtzar.com/2017/03/24/ajax-and-thymeleaf-for-modal-dialogs/

First: In controller a simple method to query database 第一:在控制器中查询数据库的简单方法

@PostMapping("/showarea")
public String areaModel(Model model) {
    LOG.info("/showarea");

    List<Zona> zonas = zonaService.findAll();

    model.addAttribute("area", zonas.get(0).getNom_ZONA());
    LOG.info("area: " + zonas.get(0).getNom_ZONA());

    return "modal::modalContents";
}

I put area in my model. 我把区域放在我的模型中。

Second: I used an example from: 第二:我用了一个例子:
https://v4-alpha.getbootstrap.com/components/modal/#modal-components https://v4-alpha.getbootstrap.com/components/modal/#modal-components

to add a text. 添加文字。

<button type="button" class="btn btn-primary" data-toggle="modal"
        data-target="#exampleModal">Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog"
     aria-labelledby="exampleModalLabel" aria-hidden="true" th:fragment="modalContents">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p th:if="${area != null}" th:text="${area}"/>
                <p th:unless="${area == null}">area == null</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">
                    Close
                </button>
                <button type="button" class="btn btn-primary">
                    Save changes
                </button>
            </div>
        </div>
    </div>
</div>

Third: I the end of this file I add a simple javascript from bootstrap plus an ajax function: 第三:我在这个文件的末尾我从bootstrap添加一个简单的javascript和一个ajax函数:

<script>
    $('#exampleModal').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget); // Button that triggered the modal
        var recipient = button.data('whatever'); // Extract info from data-* attributes
        // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
        // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
        var modal = $(this);
        modal.find('.modal-title').text('New message to ' + recipient);
        modal.find('.modal-body input').val(recipient);

        $.ajax({
            type: 'POST',
            url: "/showarea",
            success: function (data) {
            }
        });
    })
</script>

I get the data from database correctly, but I'm not be able to show the data in my modal html page. 我正确地从数据库中获取数据,但是我无法在我的模态html页面中显示数据。

UPDATE 1: 更新1:

Not able to show the data in my modal. 无法在我的模态中显示数据。 For instance, I added 例如,我补充说

<div class="modal-body" id="areaValue">
    <p id="area" th:if="${area != null}" th:text="${area}"/>
</div>

in javascript: 在javascript中:

$.ajax({
    type : 'POST',
    url : "/showarea",
    success : function(data) {
           $('#areaValue').html(data);
    }
});

debugging with firebug I get values in modal, but not showded!!! 用firebug调试我得到模态的值,但没有显示!

I would like to show a list of values, but unable to show a simple text... 我想显示一个值列表,但无法显示简单的文本...

Any suggestions? 有什么建议?

Thanks 谢谢

By reading your code I understand that you are doing an AJAX call in order to 通过阅读您的代码,我了解您正在进行AJAX调用

  • take values from DB 从DB获取值
  • populate HTML 填充HTML
  • view modal 查看模态

The code you wrote is syntactically correct but you are wrong in one thing... you must populate the modal HTML in the success method of the ajax call In this case I would modify the code in the following way (I'm writing it in spring mvc way because i'm not expert of spring boot but you can adapt it to springboot code): 你编写的代码在语法上是正确的,但你错了一件事......你必须在ajax调用的成功方法中填充模态HTML在这种情况下我会用以下方式修改代码(我在写它spring mvc方式因为我不是spring boot的专家,但你可以将它改编为springboot代码):

@RequestMapping(value = "/showarea", method = RequestMethod.POST)
    public @ResponseBody String areaModel() {
        LOG.info("/showarea");

        List<Zona> zonas = zonaService.findAll();


        return  zonas.get(0).getNom_ZONA();
    }


    <button type="button" class="btn btn-primary" data-toggle="modal"
                    data-target="#exampleModal">Launch demo modal</button>

                <!-- Modal -->
                <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog"
                    aria-labelledby="exampleModalLabel" aria-hidden="true" th:fragment="modalContents">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                                <button type="button" class="close" data-dismiss="modal"
                                    aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body" id="areaValue">

                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary"
                                    data-dismiss="modal">Close</button>
                                <button type="button" class="btn btn-primary">Save
                                    changes</button>
                            </div>
                        </div>
                    </div>
                </div>

    <script>
            $('#exampleModal').on('show.bs.modal', function(event) {
                var button = $(event.relatedTarget) 
                var recipient = button.data('whatever') 
                var modal = $(this)
                modal.find('.modal-title').text('New message to ' + recipient)
                modal.find('.modal-body input').val(recipient)
                //I would prevent the default behaviour of the button
                event.preventDefault();
                //AJAX Call
                $.ajax({
                    type : 'POST',
                    url : "/showarea",
                    success : function(data) {
                         //Get Area name after AJAX call
                         var nomArea = data;
                         //Valorize HTML
                         $("#areaValue").html(nomeArea);
                         //Finally open popup
                    }
                });
            })
        </script>

I hope it's usefull 我希望它有用

Angelo 安杰洛

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM