简体   繁体   English

Javascript通过AJAX实现Bootstrap模式

[英]Javascript Implementing Bootstrap Modal via AJAX

Current Program Concept: 当前计划概念:

  1. Page populates using the passed dataInput id which then creates a table with a row that has a button input type=submit that makes a call to the function edit() when pressed using onclick() with the dataInput id. 页面使用传递的dataInput id填充,然后创建一个表,该表的行具有按钮input type=submit ,当使用带有dataInput id的onclick()按下该按钮时,将调用函数edit()
  2. the edit() function then makes an ajax call to the /getData with the dataInput id value. 然后, edit()函数使用dataInput id值对/getData进行ajax调用。
  3. getData/ returns after a query call and with the results. getData/在查询调用之后返回并返回结果。
  4. Then the form is dynamically created and populated in the /editData page. 然后,将动态创建表单并将其填充在/editData页面中。

Desired Output: 所需输出:

  1. Page populates using the passed dataInput id which then creates a table with a row that has a button input type=submit that makes a call to the function edit() when pressed using onclick() with the dataInput id. 页面使用传递的dataInput id填充,然后创建一个表,该表的行具有按钮input type=submit ,当使用带有dataInput id的onclick()按下该按钮时,将调用函数edit()
  2. the edit() function then makes an ajax call to create a bootstrap 4 modal on the screen with /getData with the dataInput id value. 然后, edit()函数进行ajax调用,以使用/getData和dataInput id值在屏幕上创建引导程序4模态。
  3. getData/ returns after a query call and with the results. getData/在查询调用之后返回并返回结果。
  4. Then the form in the modal is dynamically created and populated in the current page with the data retrieved. 然后,将动态创建模式中的表单,并将其与获取的数据一起填充到当前页面中。

Here are my current functions: 这是我当前的功能:

function populateData(dataInput) {
  var row = $('<tr id=' + dataInput.id + '/>');
  $('#table').append(row);
  row.append($('<td>' + dataInput.name + '</td>'));
  row.append($('<td>' + dataInput.description + '</td>'));
  row.append($(
    '<td><input type="submit" class="btn btn-warning" value="edit" onclick="edit(' +
    dataInput.id + ')"/>'));
}

function edit(id) {
  $.ajax({
    type: 'GET',
    url: '/getData?id=' + id,
    success: function(data) {
      var dataInput = JSON.parse(data)[0];
      window.location = '/editData?id=' + dataInput.id;

    }
  })
}

app.get('/getData', function(req, res) {
  var content = {};
  mysql.pool.query('SELECT * FROM dataTable WHERE id=?', [req.query.id],
    function(err, rows, fields) {
      if (err) {
        next(err);
        return;
      }
      content.results = JSON.stringify(rows);
      res.send(content.results);
    });
});

handler with the html form layout: html表单布局的处理程序:

<div>
  <form id="form">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" placeholder="">

    <label for="description">Description:</label>
    <textarea id="description" name="description" rows="4" cols="50"></textarea>
  </form>
  <div class="centerButton">
    <input id="submit" class="btn btn-primary" type="submit" value="Save" onclick="save()" />
  </div>
</div>

Bootstrap Modal Example: Bootstrap模式示例:

<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">
        ...
      </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>

I had a similar problem, after researching a lot, I finally built a js function to create modals dynamically based on my requirements. 我有一个类似的问题,经过大量研究,我终于构建了一个js函数来根据自己的需求动态创建模式。 Using this function, you can create popups in one line such as: 使用此功能,您可以在一行中创建弹出窗口,例如:

puyModal({title:'Test Title',heading:'Heading',message:'This is sample message.'})

Or you can use other complex functionality such as iframes, video popups, etc. You might want to link it up to your edit function that is being called after the ajax request is successful. 或者,您可以使用其他复杂的功能,例如iframe,视频弹出窗口等。您可能希望将其链接到ajax请求成功后正在调用的编辑功能。 This way you would be creating the Modal in real time and would not have to worry about populating data inside HTML. 这样,您将实时创建模态,而不必担心在HTML内部填充数据。 Find it on https://github.com/aybhalala/puymodals For demo, go to http://pateladitya.com/puymodals/ https://github.com/aybhalala/puymodals上找到它有关演示,请访问http://pateladitya.com/puymodals/

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

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