top of page

Adding Slider Property To SPFX Web Part Properties Pane

Introduction

In this article, you will learn how to add slider property to SharePoint Framework Web part properties pane.


Several types of Web part properties are available, which can be added to SPFx Web part properties pane. Here, we will see how to add slider property and a basic sample to load SharePoint list content on the Web part, which is based on the slider value.


Slider property definition 

The definition of Property Pane slider method is given in the links below. 


Note

The property definition might be changing, since the SPFx is still under preview. The samples shown below are not for production environments, unless the SPFx is fully available for use.


The method PropertyPaneSlider(targetProperty,properties) will create the property in SPFx Web part properties pane. TargetProperty denotes the property name (identifier). The properties denote the property customizations. They contain,

  • label - Text to be displayed on the property.

  • min - Numeric minimum value of slider.

  • max - Numeric maximum value of slider.

  • step - Difference between the adjacent values (optional).

  • value - Initial value (optional).

  • showValue - Boolean, To show the value? (optional)

  • disabled - Boolean, to be disabled? (optional)


Implementation

Let us look at the implementation. Let us retrieve SharePoint items from SharePoint list, using the slider value. Here, the number of items retrieved are controlled, using the slider property.


The property is defined in the getPropertyPaneConfiguration method of the Web part property class. The snippet given below shows the property definition. Other properties can also be added.

  1. protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {  

  2. return {      

  3. pages: [        {          

  4. header: {            

  5. description: strings.PropertyPaneDescription          

  6. },          

  7. groups: [            {              

  8. groupName: strings.BasicGroupName,              

  9. groupFields: [                                

  10. PropertyPaneSlider('sliderproperty',{                  

  11. label:"Max Items",                  

  12. min:5,                  

  13. max:20,                  

  14. value:5,                  

  15. showValue:true,                  

  16. step:1                              

  17. })              

  18. ]            

  19. }         

  20.  ]        

  21. }      ]    

  22. };  

  23. }  


The property value can be retrieved, using the property name under the properties collection (this.properties.sliderproperty). Remember, this property name should be defined in Web part properties file. This has been explained in my previous article.


The custom methods are used to retrieve the data from SharePoint list. The filter is applied with the help of the slider value to the REST API to control the list items count. The methods given below shows the implementation.

  1. public render(): void {  

  2. // Render the items in tabular format

  3. this.domElement.innerHTML = `      <div class="${styles.listItemsForm}">        

  4. <div class="${styles.Table}">         

  5. </div>      

  6. </div>`;  

  7. this.LoadData();  

  8. }  

  9. private LoadData(): void{  

  10. if(this.properties.sliderproperty != undefined){      

  11. let url: string = this.context.pageContext.web.absoluteUrl + `/_api/web/lists/getbytitle('TestList')/items?

  12. $select=Title,Created,Author/Title&$expand=Author&$top=${this.properties.sliderproperty}`;  

  13. this.GetListData(url).then((response)=>{  

  14. // Render the data in the web part

  15. this.RenderListData(response.value);      

  16. });    

  17. }  

  18. }  

  19. private GetListData(url: string): Promise<spListItems>{  

  20. // Retrieves data from SP list

  21. return this.context.spHttpClient.get(url, SPHttpClient.configurations.v1)    

  22. .then((response: Response) => {  

  23. return response.json();    

  24. });  

  25. }  

  26. private RenderListData(listItems: spListItem[]): void{   

  27.  let itemsHtml: string = `<div class="${styles.Heading}">            

  28. <div class="${styles.Cell}">Title</div>            

  29. <div class="${styles.Cell}">Created</div>            

  30. <div class="${styles.Cell}">Author</div>          

  31. </div>`;  

  32. // Displays the values in table rows   

  33. listItems.forEach((listItem: spListItem)=>{      

  34. itemsHtml += `<div class="${styles.Row}">`;     

  35.  itemsHtml += `<div class="${styles.Cell}"><p>${listItem.Title}</p></div>`;        

  36. itemsHtml += `<div class="${styles.Cell}"><p>${listItem.Created}</p></div>`;        

  37. itemsHtml += `<div class="${styles.Cell}"><p>${listItem.Author.Title}</p></div>`;       

  38. itemsHtml += `</div>`;    

  39. });  

  40. this.domElement.querySelector("."+styles.Table).innerHTML =itemsHtml;  

  41. }  


The snapshot given below shows the slider property appended to the pane and the values loading, which is based on the slider property. 


The basic development and deployment steps are explained in the previous articles

In my previous articles, I have explained about adding the basic custom properties to the properties pane.


Summary

Thus, you have learned how to add a slider property to SharePoint Framework Web part properties pane and seen how the data can be rendered based on the slider property value.

0 comments

Comments


bottom of page