1

In the snippet of code from my no-framework SPFX, I'm trying to understand how to not show "NULL" in any of the four fields in the html table. The first three columns are not editable, while the fourth column is an input to allow new/updated data to be submitted.

private _renderList(items: ISPList[]): void {
    let html: string = '<table border=1><th>Name of Client</th><th>Age</th><th>Height</th><th>Recommended Strategy</th>';
    items.forEach((item: ISPList) => {
      html += `    
    <tr>             
        <td>${item.Title}</td>
        <td>${item.Age}</td>
        <td>${item.Height}</td>
        <td><input id="txtRecommendedStrategy" type="text"  value=${item.RecommendedStrategy} /></td>
    </tr>
    `;
    });
  
    html += '</table>';

    const listContainer: Element = this.domElement.querySelector('#spListContainer');
    listContainer.innerHTML = html;
  }

1 Answer 1

1

You can try following code:

private _renderList(items: ISPList[]): void {

  let html: string = '<table border=1><th>Name of Client</th><th>Age</th><th>Height</th><th>Recommended Strategy</th>';
  items.forEach((item: ISPList) => {
    html += `    
      <tr>             
        <td>${item.Title ? item.Title : ''}</td>
        <td>${item.Age ? item.Age : ''}</td>
        <td>${item.Height ? item.Height : ''}</td>
        <td><input id="txtRecommendedStrategy" type="text" value="${item.RecommendedStrategy ? item.RecommendedStrategy : ''}" /></td>
      </tr>
    `;
  });

  html += '</table>';

  const listContainer: Element = this.domElement.querySelector('#spListContainer');
  listContainer.innerHTML = html;
}

By using the conditional rendering logic {item.fieldX ? item.fieldX : ''}, you can ensure that NULL is not displayed in the table when the values are empty or null.

1
  • 1
    Works great! Thank you!!!
    – Susan T.
    May 24 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.