根据对象数组长度如何更改模态弹出窗口的内容,使用JavaScript?

huangapple go评论64阅读模式
英文:

How to change the modal popup content based on array of object length using javaScript?

问题

我有一个对象数组数据,如下所示,

let loginMessages = [{ NotificationID: 123, NotificationName: "first Message", NotificationDesc: "First Message", ValidFrom: "2019-09-25T00:00:00", ValidTo: "2019-10-03T00:00:00" },
{ NotificationID: 456, NotificationName: "second Message", NotificationDesc: "Second Message", ValidFrom: "2019-09-25T00:00:00", ValidTo: "2019-10-03T00:00:00" }
];

在这里,我在页面加载时显示第 123 条通知弹出窗口。在第一个通知弹出窗口中,我有一个"下一个"按钮。如果单击"下一个"按钮,第一个弹出窗口应该被第二个通知数据覆盖。如果对象数组的长度达到最后一项,那时应显示"上一个"按钮。

我不知道如何做这个。请帮助我。

我所做的是,

loginMessages.forEach((d) => {
      let listMessag = document.getElementById("modal-content");
      let listTitle = document.getElementById("modal-title");
      $('#infoPopup2').modal('show');
      let el = d.NotificationDesc;
      let ele = d.NotificationName;
      listMessag.innerHTML += el;
      listTitle.innerHTML += ele;
});
<div class="modal fade settings-modal" id="infoPopup2" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
  <div class="modal-content sp_modal_content auto-height" >
    <div class="modal-header sp_modal_header">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">
          <img class="sp_modal_close_img" src="/images/button_close_small.png">
        </span>
      </button>
      <h4 class="modal-title sp_modal_heading" id="modal-title"> </h4>
    </div>
    <div class="modal-body text-editor-p" id="modal-content">

      </div>
      <div class="modal-footer">

        <div class="row">
          <div class="col-sm-12">

            <p class="pull-left">
              <input type='checkbox' store="checkbox3" /> Don't show again
              @*<input type='checkbox' store="checkbox2" />*

            </p>
          </div>
        </div>

        <div class="row">
          <div class="col-sm-12">

            <button type="button" id="next" class="sp_button sp_login_button pull-left">NEXT</button>
            <p class="pull-right">2/3</p>
          </div>
        </div>
      </div>
    </div>
  <!-- modal-content -->
</div>
</div>

这是我的输出,
根据对象数组长度如何更改模态弹出窗口的内容,使用JavaScript?

在这里,两条通知数据在一个弹出窗口中显示。我需要通过单击"下一个"按钮单独显示第二个消息弹出窗口。

英文:

I have array of object data like below,

let loginMessages = [{ NotificationID: 123, NotificationName: &quot;first Message&quot;, NotificationDesc: &quot;First Message&quot;, ValidFrom: &quot;2019-09-25T00:00:00&quot;, ValidTo: &quot;2019-10-03T00:00:00&quot; },
{ NotificationID: 456, NotificationName: &quot;second Message&quot;, NotificationDesc: &quot;Second Message&quot;, ValidFrom: &quot;2019-09-25T00:00:00&quot;, ValidTo: &quot;2019-10-03T00:00:00&quot; }
];

Here, I displayed the first 123 notification modal popup on page loading. In that first notification modal I have Next button. If we click on Next button, that first popup should override with the second notification data. If the array of object length reached the last item, that time previous button should display.

I don't know how to do this. Please help me.

what I did is,

loginMessages.forEach((d) =&gt; {
let listMessag = document.getElementById(&quot;modal-content&quot;);
let listTitle = document.getElementById(&quot;modal-title&quot;);
$(&#39;#infoPopup2&#39;).modal(&#39;show&#39;);
let el = d.NotificationDesc;
let ele = d.NotificationName;
listMessag.innerHTML += el;
listTitle.innerHTML += ele;
});
&lt;div class=&quot;modal fade settings-modal&quot; id=&quot;infoPopup2&quot; tabindex=&quot;-1&quot; role=&quot;dialog&quot;&gt;
&lt;div class=&quot;modal-dialog&quot; role=&quot;document&quot;&gt;
&lt;div class=&quot;modal-content sp_modal_content auto-height&quot; &gt;
&lt;div class=&quot;modal-header sp_modal_header&quot;&gt;
&lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;
&lt;span aria-hidden=&quot;true&quot;&gt;
&lt;img class=&quot;sp_modal_close_img&quot; src=&quot;/images/button_close_small.png&quot;&gt;
&lt;/span&gt;
&lt;/button&gt;
&lt;h4 class=&quot;modal-title sp_modal_heading&quot; id=&quot;modal-title&quot;&gt; &lt;/h4&gt;
&lt;/div&gt;
&lt;div class=&quot;modal-body text-editor-p&quot; id=&quot;modal-content&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;modal-footer&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-sm-12&quot;&gt;
&lt;p class=&quot;pull-left&quot;&gt;
&lt;input type=&#39;checkbox&#39; store=&quot;checkbox3&quot; /&gt; Don&#39;t show again
@*&lt;input type=&#39;checkbox&#39; store=&quot;checkbox2&quot; /&gt;*@
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-sm-12&quot;&gt;
&lt;button type=&quot;button&quot; id=&quot;next&quot; class=&quot;sp_button sp_login_button pull-left&quot;&gt;NEXT&lt;/button&gt;
&lt;p class=&quot;pull-right&quot;&gt;2/3&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;!-- modal-content --&gt;
&lt;/div&gt;

</div>

This is my output,
根据对象数组长度如何更改模态弹出窗口的内容,使用JavaScript?

here, two notifications data displaying in one popup. I need to display the second message popup separately by clicking next button.

答案1

得分: 1

你只是遍历数组并将值显示到HTML中。

尝试这个。当前索引将始终显示第一个数组,当调用showNotification(currentIndex); 时。

单击"下一个"将递增索引并再次触发showNotification函数,索引将被更新。

let loginMessages = [
  { NotificationID: 123, NotificationName: "第一条消息", NotificationDesc: "第一条消息", ValidFrom: "2019-09-25T00:00:00", ValidTo: "2019-10-03T00:00:00" },
  { NotificationID: 456, NotificationName: "第二条消息", NotificationDesc: "第二条消息", ValidFrom: "2019-09-25T00:00:00", ValidTo: "2019-10-03T00:00:00" }
];

let currentIndex = 0;
const modal = document.getElementById("modal");
const nextButton = document.getElementById("nextButton");
showNotification(currentIndex);

nextButton.addEventListener("click", () => {
  currentIndex++;
  showNotification(currentIndex);
});

function showNotification(index) {
  const messages = loginMessages[index];
  modal.innerHTML = 
    `<p>ID: ${messages.NotificationID}</p>
    <p>Name: ${messages.NotificationName}</p>
    <p>Description: ${messages.NotificationDesc}</p>
    <p>Valid from: ${messages.ValidFrom}</p>
    <p>Valid to: ${messages.ValidTo}</p>`;
}
英文:

You were just going through the arrays and displaying the values to html.

Try this. Current index will always show the 1st array, when the showNotification(currentIndex); is called.

Clicking next will increment the index and trigger the showNotification function again, with the index being updated.

let loginMessages = [
{ NotificationID: 123, NotificationName: &quot;first Message&quot;, NotificationDesc: &quot;First Message&quot;, ValidFrom: &quot;2019-09-25T00:00:00&quot;, ValidTo: &quot;2019-10-03T00:00:00&quot; },
{ NotificationID: 456, NotificationName: &quot;second Message&quot;, NotificationDesc: &quot;Second Message&quot;, ValidFrom: &quot;2019-09-25T00:00:00&quot;, ValidTo: &quot;2019-10-03T00:00:00&quot; }
];
let currentIndex = 0;
const modal = document.getElementById(&quot;modal&quot;);
const nextButton = document.getElementById(&quot;nextButton&quot;);
showNotification(currentIndex);
nextButton.addEventListener(&quot;click&quot;, () =&gt; {
currentIndex++;
showNotification(currentIndex);
});
function showNotification(index) {
const messages = loginMessages[index];
modal.innerHTML = 
`&lt;p&gt;ID: ${messages.NotificationID}&lt;/p&gt;
&lt;p&gt;Name: ${messages.NotificationName}&lt;/p&gt;
&lt;p&gt;Description: ${messages.NotificationDesc}&lt;/p&gt;
&lt;p&gt;Valid from: ${messages.ValidFrom}&lt;/p&gt;
&lt;p&gt;Valid to: ${messages.ValidTo}&lt;/p&gt;
`;
}

huangapple
  • 本文由 发表于 2023年2月27日 17:13:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定