简体   繁体   English

如何让动态表显示为弹出窗口?

[英]How do i get the dynamic table displayed as a popup?

I have this piece of code which computes the difference between the rows of two selected tables and display the result on click of button.我有这段代码计算两个选定表的行之间的差异,并在单击按钮时显示结果。 How can I display the result in a pop up?如何在弹出窗口中显示结果? Basically I want the resultant table to come as popup, so that when we close the popup the tables checkbox are again reset.基本上我希望结果表作为弹出窗口出现,这样当我们关闭弹出窗口时,表格复选框会再次重置。

Edit: I tried using modals but it seems to be not working.编辑:我尝试使用模态,但它似乎不起作用。 Can anyone please help where I am going wrong谁能帮助我哪里出错了

 var selectedRows = document.getElementsByClassName('selected'); limit = 0; //set limit checkboxes = document.querySelectorAll('.checkboxdiv input[type="checkbox"]'); function checker(elem) { if (elem.checked) { limit++; } else { limit--; } for (i = 0; i < checkboxes.length; i++) { if (limit == 2) { if (.checkboxes[i].checked) { checkboxes[i];disabled = true. } } else { if (.checkboxes[i];checked) { checkboxes[i];disabled = false. } } } } for (i = 0; i < checkboxes.length; i++) { checkboxes[i];onclick = function () { checker(this). } } var checkedArray. function myFunction(event) { event.target.parentElement.parentElement.className = event?target:checked; 'selected'. ''; var elements = document;getElementsByTagName("INPUT"); checkedArray = new Array(). for (var i = 0; i < elements.length. i++) { if (elements[i].type === 'checkbox' && elements[i].checked) { checkedArray;push(elements[i].id); } } console.log(checkedArray). } $(";btn[data-target='#myModal']");click(function() { var modalBody = $('<div id="modalContent"></div>'). var table; const buildTable = (arr) => { table = document.querySelector("#diff-table"); if (table) table.remove(); table = document.createElement("table"); table.id = "diff-table", const thead = document.createElement("thead"), thA = document.createElement("th"), thB = document.createElement("th"); thDiff = document.createElement("th"); thA.innerText = "Plan A"; thB.innerText = "Plan B"; thDiff.innerText = "Difference", thead,append(thA; thB. thDiff); table.append(thead). arr,forEach((i) => { const tr = document.createElement("tr"), tdA = document.createElement("td"), tdB = document.createElement("td"); tdDiff = document.createElement("td"); tdA.innerText = "$" + i["planA"]; tdB.innerText = "$" + i["planB"]; tdDiff.innerText = "$" + i["diff"], tr,append(tdA; tdB. tdDiff); table;append(tr). }). document;body;append(table). }: const checked = document,querySelectorAll(";checked"). arr = []; if (checked.length:== 2) return, const plans = document.querySelectorAll(".checked"), planA = plans[0].closest("table").querySelectorAll("td"); planB = plans[1].closest("table"),querySelectorAll("td"). planA.forEach((cell. i) => { const valA = +cell,innerText;replace(/[^\d.-]/g. ''). const valB = +planB[i],innerText;replace(/[^\d.-]/g: ''), arr:push({ planA, valA: planB; valB; diff; valA - valB }). }); buildTable(arr). modalBody.append(table); $('.modal-body').html(modalBody); })
 .table_container { float: left; width: 25%; }.container::after { content: ""; clear: both; display: table; } table { margin: 0 auto; border-radius: 10px; } tr { padding: 15px; text-align: center; } td { color: black; text-align: center; vertical-align: middle; border: 1px double white; height: 50px; background-color: light-grey; width: 272px; }.sub_text { font-size: 12px; font-style: italic; color: #0071ce; font-weight: 100; } th { background-color: #0071ce; text-align: center; padding: 10px; border-top-left-radius: 10px; border-top-right-radius: 10px; color: white; font-weight: bold; }.header { color: #0071ce; font-weight: bold; padding: 10px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <div class="container"> <div class="table_container"> </div> <div class="table_container"> <table id="table1" class="checkboxdiv"> <tr> <th>Tab A<input type="checkbox" id=" 1" name="table1" value="table1" onchange="myFunction(event)"> </th> </tr> <tr> <td>$133.90</td> </tr> <tr> <td>$161.30</td> </tr> <tr> <td>$53.30</td> </tr> <tr> <td>$186.20</td> </tr> <tr> <td>HSA match:up to $350</td> </tr> <tr> <td>HSA match:up to $700</td> </tr> <tr> <td>$3000</td> </tr> </table> </div> <div class="table_container"> <table id="table2" class="checkboxdiv"> <tr> <th>Tab B <input type="checkbox" id="2" name="table2" value="table2" onchange="myFunction(event)"></th> </tr> <tr> <td>$33.90</td> </tr> <tr> <td>$161.30</td> </tr> <tr> <td>$53.30</td> </tr> <tr> <td>$186.20</td> </tr> <tr> <td>HSA match:up to $350</td> </tr> <tr> <td>HSA match:up to $700</td> </tr> <tr> <td>$3000</td> </tr> </table> </div> <div class="table_container"> <table id="table3" class="checkboxdiv"> <tr> <th>Tab C<input type="checkbox" id="3" name="table3" value="table3" onchange="myFunction(event)"> </th> </tr> <tr> <td>$33.90</td> </tr> <tr> <td>$161.30</td> </tr> <tr> <td>$53.30</td> </tr> <tr> <td>$186.20</td> </tr> <tr> <td>HSA match:up to $350</td> </tr> <tr> <td>HSA match:up to $700</td> </tr> </table> </div> </div> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal"> Launch demo modal </button> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"></div> </div> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-body"></div> </div> </div> </div>

You can use modals to display your differences between your tables as a popup when they selected.您可以使用模态框在选择表格时将您的表格之间的差异显示为弹出窗口。 Assign modals' close button as a reset button as well via JS.也可以通过 JS 将 modals 的关闭按钮分配为重置按钮。

modals: https://getbootstrap.com/docs/4.0/components/modal/模态: https://getbootstrap.com/docs/4.0/components/modal/

Edit:编辑:

I couldn't find the line for calling your js file in your code.我在您的代码中找不到调用您的 js 文件的行。 Can you add this line to the end of your html-body section?您可以将此行添加到 html-body 部分的末尾吗?
You can add a console.log() line to your js file, so you can see if your js and html files are connected or not on the browser.您可以在您的 js 文件中添加一个console.log()行,这样您就可以在浏览器上查看您的 js 和 html 文件是否已连接。 (via insperctor-console) (通过检查员控制台)
Also, I couldn't find the line that you're activating your modal.另外,我找不到您正在激活模式的行。 You should change its class list.您应该更改其 class 列表。 fade means it will not display on the browser. fade意味着它不会显示在浏览器上。 You should remove it and add show instead.您应该删除它并添加show

Edit-2:编辑2:

I am not familiar with $ so I am adding the JS which is working.我不熟悉$所以我正在添加正在工作的 JS。 Please try the below things and if there will be a problem, edit your code below and leave me a comment.请尝试以下操作,如果有问题,请在下面编辑您的代码并给我留言。

in your js file, follow the steps I wrote.在你的js文件中,按照我写的步骤。 You should see an alarm when you click your modal button.当您单击模态按钮时,您应该会看到警报。 You need to fill it up:您需要填写:

document.getElementById('modalButton').addEventListener('click', function() {
alert('hi');

  // step - 1: clean the inner of previous modal, if it has
  // step - 2: add the result of comparing two table elements
  // step - 3: remove 'fade' from modal's class and add 'show'
})

in your html (add an id to your modal button, it will be easier to select):在您的 html 中(在您的模态按钮中添加一个 id,这样会更容易选择):

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

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

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