Bootstrap:用于动态编辑数据的模式对话框

克里斯

这个问题可能是重复的,但我不知道要寻找所需解决方案的任何关键词。

我正在一个向用户显示表格的网站上工作。该表在一列中包含用户可以编辑的注释。为此,我为该列中的每一行添加了一个按钮,以打开引导模式对话框,用户可以在其中编辑与特定行关联的注释。我还有一个JavaScript函数“ saveNote(recordId)”,它从模式对话框的输入字段中读取输入的文本,然后通过ajax发布将其发送到服务器。

现在我的问题是:如何以及在哪里存储当前正在编辑的行的ID,以便可以将其传递给saveNote()函数?我在引导程序文档中找到了一个示例,但是该示例仅介绍了将数据动态传递到模式对话框的方法(变化模式内容)。在模式对话框中,有什么通用的方法可以做到这一点,或者我需要在JavaScript中使用全局变量吗?

卡尔·比纳拉

基本上,将id每行的放到data-id行按钮属性中。然后将模式绑定到show.bs.modal事件,然后使用$(e.relatedTarget).data('id')来获取data-id打开模式的按钮。检查评论以获取其他解释

$(function() {
  $('#exampleModal').on('show.bs.modal', function(e) {
    $('.modalTextInput').val('');
    let btn = $(e.relatedTarget); // e.related here is the element that opened the modal, specifically the row button
    let id = btn.data('id'); // this is how you get the of any `data` attribute of an element
    $('.saveEdit').data('id', id); // then pass it to the button inside the modal
  })

  $('.saveEdit').on('click', function() {
    let id = $(this).data('id'); // the rest is just the same
    saveNote(id);
    $('#exampleModal').modal('toggle'); // this is to close the modal after clicking the modal button
  })
})

function saveNote(id) {
  let text = $('.modalTextInput').val();
  $('.recentNote').data('note', text);
  console.log($('.recentNote').data('note'));
  console.log(text + ' --> ' + id);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <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">
        <input class="modalTextInput form-control" placeholder="Text Here" />
      </div>
      <div class="modal-footer">
        <button type="button" class="saveEdit btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

<table class="table table-bordered">
  <tbody>
    <tr>
      <td>Some data here</td>
      <td>
        <button class="btn btn-success btn-sm" data-id="1" data-toggle="modal" data-target="#exampleModal">Edit</button>
      </td>
    </tr>
    <tr>
      <td>Another data here</td>
      <td>
        <button class="btn btn-success btn-sm" data-id="2" data-toggle="modal" data-target="#exampleModal">Edit</button>
      </td>
    </tr>
    <tr>
      <td>More data here</td>
      <td>
        <button class="btn btn-success btn-sm" data-id="3" data-toggle="modal" data-target="#exampleModal">Edit</button>
      </td>
    </tr>
  </tbody>
</table>

<p class="recentNote"></p>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Bootstrap:处理多个模式对话框

来自分类Dev

如何从JavaScript获取Bootstrap 3动态下拉模式对话框中的选定项目

来自分类Dev

动态对话框

来自分类Dev

编辑模式下的 Magnolia 5.5 对话框

来自分类Dev

动态退出jQGrid中的编辑对话框

来自分类Dev

通过Angular-Bootstrap对话框模式将数据追加到现有json数组

来自分类Dev

在引导程序中动态调整模式对话框的大小

来自分类Dev

在引导程序中动态调整模式对话框的大小

来自分类Dev

无法获得简单的Twitter Bootstrap模式对话框弹出

来自分类Dev

最小化或折叠Bootstrap 2.3.2模式对话框?

来自分类Dev

无法获得简单的Twitter Bootstrap模式对话框弹出

来自分类Dev

Twitter Bootstrap 3模式对话框不显示

来自分类Dev

vuejs的动态对话框

来自分类Dev

Watson对话框动态数据变量输入/输出

来自分类Dev

打开一个模式对话框,其中包含表格单元格上的动态数据

来自分类Dev

如何编辑Android对话框标题

来自分类Dev

如何编辑Android对话框标题

来自分类Dev

CKeditor的html,编辑mathjax对话框

来自分类Dev

如何编辑对话框文本

来自分类Dev

用于编辑的单个对话框-渲染时更改对象-React Modal

来自分类Dev

对话框过滤器,用于编辑文本的最小特征

来自分类Dev

用于编辑的单个对话框-渲染时更改对象-React Modal

来自分类Dev

NSWindow级别和模式对话框

来自分类Dev

单例对话框模式

来自分类Dev

jQuery对话框模式问题

来自分类Dev

使用WindowFinder查找模式对话框

来自分类Dev

获取PrintDialog的模式对话框句柄

来自分类Dev

单击AngularJS打开模式对话框

来自分类Dev

反应模式/对话框的焦点输入

Related 相关文章

热门标签

归档