【问题标题】:How to hide a Null text when a column is empty in a Share Point 2013 List?当 Share Point 2013 列表中的列为空时,如何隐藏 Null 文本?
【发布时间】:2019-10-09 22:42:16
【问题描述】:

我有一个分享点列表,在某些情况下,某些列是空的;在这种情况下,显示文本 null。每当一列为空时,我都需要 null 消失。这是我原来的代码。

var buildModal = function(items) {
var modalContent = $('#modalContent');

for (let i = 0; i < items.length; i++) {

var modalContentCard = '<div class="modal fade" id=basicExample'+items[i].ID+'  tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"><div class="modal-dialog modal-lg"  role="document"><div class="modal-content"><div class="modal-header"><h5 class="modal-title" id="exampleModalLabel"><strong>' + items[i].Title +
'</strong></h5><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button></div><div class="modal-body"><h5>Personal:</h5><p><strong>A dream I have is to:</strong> '+ items[i].Dream +'</p><p><strong>You would be surprised to learn that I:</strong>'+ items[i].Learn +'</p><p><strong>My motto is:</strong>'+ items[i].Motto +
'</p><p><strong>The mission of our agency is:</strong>'+ items[i].Mission +'</p><hr><h5>Our current priorities are:</h5><p>'+ items[i].Priorities +'</p><h5>Our biggest accomplishments in the past year include:</h5><p>'+ items[i].Accomplishments +'</p><div class="agency"><h5>One thing you did not know about our agency is:</h5><p>'+ items[i].Agency+'</p></div><div class="mt-3 mb-3"><a href="'+ items[i].Link.Url +'" target="_blank">'+ items[i].Website +'</a></div><div class="modal-footer"><button type="button" class="btn btn-primary" data-dismiss="modal">Close</button></div></div></div></div></div>';
modalContent.prepend(modalContentCard);
}

});

};

我尝试添加此代码但没有结果

var empty = $('p');
console.log(empty);

$.each(empty, function(element) {
       var text = element.innerHTML;    
  if (text === " ") {
    text.style.display = 'none';
  }
<div class="modal-body">
  <h5>Personal:</h5>
  <p><strong>A dream I have is to:</strong> </p>
  <div class="ExternalClass44F41EEF57F6405EBDCDF166AAE64839">
    <p>Travel the world, starting with a trip around the US in an RV!&nbsp;<br></p>
  </div>
  <p></p>
  <p><strong>You would be surprised to learn that I:</strong></p>
  <div class="ExternalClass609F53F5A4794345A04360BEA3C8A50F">
    <p>Was extremely shy as a child.</p>
  </div>
  <p></p>
  <p><strong>My motto is:</strong>null</p>

</div>

【问题讨论】:

  • 无论您在哪里连接文本,都将yourVariable 替换为(yourVariable || '')
  • @ayrton 感谢您的完美回复。如果我想完全隐藏这个&lt;p&gt;&lt;strong&gt;My motto is:&lt;/strong&gt;null&lt;/p&gt; 甚至连我的座右铭都不显示怎么办?
  • 看看我的回答

标签: javascript jquery html sharepoint


【解决方案1】:

要简单地将“null”文本替换为空文本,请转到您的 concats 并将 yourVariable 替换为 (yourVariable || '')

如果您希望完全隐藏文本为空的元素,您应该单独构建它们并将它们包装在if 块中。例如,隐藏您的 &lt;p&gt; 元素:

var modalContent = ...

if(items[i].Motto) {
    modalContent += "<p><strong>My motto is:</strong>"+ items[i].Motto +
"</p>";
}

如果您只想检查null,请在您的if 中添加!== null

PS:还要注意,使用字符串可能不是创建 HTML 元素的最佳方式。您可能应该使用 createElement 和类似名称。

文档:https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多