0

I am trying to insert sharepoint list items in UL. right now each item is added in separate UL. please help how to iterate and add all sharepoint list item in one UL.

public componentDidMount(){ 
      if(this.props.Dropdownprop != undefined){
      }
      alert("second" + this.props.Dropdownprop);
        var reactHandler = this; 
        $.ajax({ 
            url: "https://sharepointprod.sharepoint.com/teams/SharepointPOC/_api/lists(guid'"+this.props.Dropdownprop+"')/items", 
            type: "GET", 
            headers:{'Accept': 'application/json; odata=verbose;'}, 
            success: function(resultData) {          
              reactHandler.setState({ 
                items: resultData.d.results,
              }); 
            }, 
            error : function(jqXHR, textStatus, errorThrown) { 
            } 
        }); 
      }
      public render(): React.ReactElement<IScrollTickerWebPartProps> {
        return ( 
           <div>
                 {this.state.items.map(function(item,key){     
                   return (<div className={styles.marquee} key={key}> 
                   <span>
                       <div>
                       <ul className={styles.jsnews}>
                       <li>{item.Title}</li>
                       </ul>
                       </div> 
                       </span>
                     </div>); 
                 })}                    
             </div> 
       ); 
     }   
swetha s
  • 33
  • 1
  • 1
  • 9

1 Answers1

0

Review this part of your code in your render method:

                 {this.state.items.map(function(item,key){     
                   return (<div className={styles.marquee} key={key}> 
                   <span>
                       <div>
                       <ul className={styles.jsnews}>
                       <li>{item.Title}</li>
                       </ul>
                       </div> 
                       </span>
                     </div>); 
                 })}

You are using map to turn each this.state.items into a div className={styles.marquee}. Since each of these divs has a ul in it, you are actually creating one ul for each of the items in this.state.items.

Instead, you should turn each of the this.state.items into a li, and put all of it into one ul. You can still do this with map, but it will have to be moved deeper into the HTML structure. See this example:

<ul className={styles.jsnews}>
    {this.state.items.map(function(item,key){
        return (<li>{item.Title}</li>);
    }}
</ul>
Richard Li
  • 301
  • 1
  • 5
  • So I'm struggling with a click event on the
  • tag. If i want to update the UX based on the id of the listitem that is clicked, how do I add a onclick event to the return statement, and pass the id of the item?
  • – Tim Wheeler Mar 25 '19 at 21:16
  • Untested, but I usually do something like this: `return (
  • ...
  • );`. And the function looks like: `var doSomething = function(itemId, ...otherArgs) { /* do whatever */ }`. The [`bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind) allows you to pass through any additional arguments - for example, values that would be available in the scope of the React code, but not in the scope of the onClick call. – Richard Li Mar 26 '19 at 13:49
  • Here's [a very basic Pen](https://codepen.io/lirichard2013/pen/BbEMjM) demonstrating that use of `bind`. – Richard Li Mar 26 '19 at 14:07