简体   繁体   English

在编辑/更新时填充模态

[英]Populating Modal on Edit/Update

I am having a bit of an issue when trying to edit/update the persisted content.尝试编辑/更新持久内容时遇到了一些问题。 Basically when i hit edit I get a bunch of JSON displayed on the modal face as below基本上当我点击编辑时,我得到一堆 JSON 显示在模态面上,如下所示

在此处输入图像描述

I am fireing a bootstrap modal from within datatables and the below piece of code is what i am currently trying我正在从数据表中触发引导模式,下面的代码是我目前正在尝试的

 $(document).ready(function () {
        $("#EditModal").on("show.bs.modal", function (e) {
            var link = $(e.relatedTarget);
            $(this).find(".modal-body").load(link.attr("href"));
        })
    })

The below is how i am calling the modal inside the datatables anchor tag以下是我如何调用数据表锚标记内的模式

 {
                "data": "id",
                "render": function (data) {

                    return `
                            <a href="http://someurl/record/2"  data-toggle="modal" data-target="#EditModal" style='cursor:pointer'class='text-success'><i class="fas fa-edit text-success ml-3 mr-2"></i>Details</a>    
                        `;
                }, "width": "7%", "font-size": "9px"
            },

I know this is massively wrong, but I am unsure how to display the values of the data inside my modal input fields and have labels displayed etc... any help would be greatly appreciated我知道这是大错特错,但我不确定如何在我的模态输入字段中显示数据的值并显示标签等......任何帮助将不胜感激

As i already said in comments you can use ajax here because your server returns json as response then inside ajax success you can manipulate your htmls.正如我在评论中已经说过的,你可以在这里使用 ajax 因为你的服务器返回 json 作为响应然后在 ajax 成功你可以操纵你的 htmls。 So, your code for ajax will look like below:因此,您的 ajax 代码如下所示:

$("#EditModal").on("show.bs.modal", function(e) {
  var selector = $(this)
  var link = $(e.relatedTarget);
  $.ajax({
    type: "GET", //change this if needed to post
    url: link.attr("href"), //your link
    dataType: "json", //json because server return json
    success: function(data) {
      console.log("result will come here")
      console.log(data.data.id) //access it like this 
      console.log(data.data.surname) //etc..etc
     selector.find(".modal-body").text(data.data.id) //add result inside your dom like this ..
    }
  })
})

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

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