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.       {  
  5.         header: {  
  6.           description: strings.PropertyPaneDescription  
  7.         },  
  8.         groups: [  
  9.           {  
  10.             groupName: strings.BasicGroupName,  
  11.             groupFields: [                  
  12.               PropertyPaneSlider('sliderproperty',{  
  13.                 label:"Max Items",  
  14.                 min:5,  
  15.                 max:20,  
  16.                 value:5,  
  17.                 showValue:true,  
  18.                 step:1                
  19.               })  
  20.             ]  
  21.           }  
  22.         ]  
  23.       }  
  24.     ]  
  25.   };  
  26. }   
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 = `  
  4.     <div class="${styles.listItemsForm}">  
  5.       <div class="${styles.Table}">  
  6.           
  7.       </div>  
  8.     </div>`;  
  9.     this.LoadData();  
  10. }  
  11.   
  12. private LoadData(): void{  
  13.   if(this.properties.sliderproperty != undefined){  
  14.     let url: string = this.context.pageContext.web.absoluteUrl + `/_api/web/lists/getbytitle('TestList')/items?$select=Title,Created,Author/Title&$expand=Author&$top=${this.properties.sliderproperty}`;  
  15.     this.GetListData(url).then((response)=>{  
  16.       // Render the data in the web part  
  17.       this.RenderListData(response.value);  
  18.     });  
  19.   }  
  20. }  
  21.   
  22. private GetListData(url: string): Promise<spListItems>{  
  23.   // Retrieves data from SP list  
  24.   return this.context.spHttpClient.get(url, SPHttpClient.configurations.v1)  
  25.   .then((response: Response) => {  
  26.     return response.json();  
  27.   });  
  28. }  
  29.   
  30. private RenderListData(listItems: spListItem[]): void{  
  31.   let itemsHtml: string = `<div class="${styles.Heading}">  
  32.           <div class="${styles.Cell}">Title</div>  
  33.           <div class="${styles.Cell}">Created</div>  
  34.           <div class="${styles.Cell}">Author</div>  
  35.         </div>`;  
  36.   // Displays the values in table rows  
  37.   listItems.forEach((listItem: spListItem)=>{  
  38.     itemsHtml += `<div class="${styles.Row}">`;  
  39.     itemsHtml += `<div class="${styles.Cell}"><p>${listItem.Title}</p></div>`;  
  40.       itemsHtml += `<div class="${styles.Cell}"><p>${listItem.Created}</p></div>`;  
  41.       itemsHtml += `<div class="${styles.Cell}"><p>${listItem.Author.Title}</p></div>`;  
  42.   
  43.     itemsHtml += `</div>`;  
  44.   });  
  45.   this.domElement.querySelector("."+styles.Table).innerHTML =itemsHtml;  
  46. }   
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
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.

Up Next
    Ebook Download
    View all
    Learn
    View all