【问题标题】:How to send data from one webpart to another webpart using dynamic data in spfx如何使用 spfx 中的动态数据将数据从一个 Web 部件发送到另一个 Web 部件
【发布时间】:2020-03-29 03:56:35
【问题描述】:

我在一个项目中有两个 web 部件,我试图将值发送到另一个 web 部件。我浏览了 microsoft sharepoint 中有关动态数据的文档。但我几乎什么都得不到。

一个 webpart 将进行过滤,另一个 webpart 将显示过滤结果。我只是想知道传递该值,以便在第二个 webpart 中进行过滤。

任何建议都会有所帮助

【问题讨论】:

    标签: reactjs sharepoint sharepoint-online spfx


    【解决方案1】:

    Connect SharePoint Framework components using dynamic data

    示例测试演示:

    SourceWebPart:

    import { Version } from '@microsoft/sp-core-library';
    import {
      BaseClientSideWebPart,
      IPropertyPaneConfiguration,
      PropertyPaneTextField
    } from '@microsoft/sp-webpart-base';
    import { escape } from '@microsoft/sp-lodash-subset';
    import { IDynamicDataCallables, IDynamicDataPropertyDefinition } from '@microsoft/sp-dynamic-data';
    import styles from './SourceWebPartWebPart.module.scss';
    import * as strings from 'SourceWebPartWebPartStrings';
    
    export interface IPoint { 
      x: number;
      y: number;
    }
    
    export interface ISourceWebPartWebPartProps {
      description: string;
    }
    
    export default class SourceWebPartWebPart extends BaseClientSideWebPart<ISourceWebPartWebPartProps> implements IDynamicDataCallables{
      protected onInit(): Promise<void> {
    
        this.context.dynamicDataSourceManager.initializeSource(this);
    
        return Promise.resolve();
      }
      public getPropertyDefinitions(): ReadonlyArray<IDynamicDataPropertyDefinition> {
        return [
          {
            id: 'x',
            title: 'Mouse-X'
          },
          {
            id: 'y',
            title: 'Mouse-y'
          }
        ];
      } 
      public getPropertyValue(propertyId: string): number {
        switch (propertyId) {
          case 'x':
            return this.mousePosition.x;
          case 'y':
            return this.mousePosition.y;
        }
    
        throw new Error('Bad property id');
      }
      private mousePosition: IPoint;  
    
      public onMouseClick(e) {  
        this.mousePosition = { x: e.clientX, y: e.clientY };
        this.context.dynamicDataSourceManager.notifyPropertyChanged('x');
        this.context.dynamicDataSourceManager.notifyPropertyChanged('y');
      }
    
      public render(): void {
        this.domElement.innerHTML = `
        <div id="webpartdiv" style="width: 700px; height: 200px; background-color: yellow">
    
        </div>`;
    
    
      this.domElement.onclick=this.onMouseClick.bind(this);
    
      }
    
      protected get dataVersion(): Version {
        return Version.parse('1.0');
      }
    
      protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
        return {
          pages: [
            {
              header: {
                description: strings.PropertyPaneDescription
              },
              groups: [
                {
                  groupName: strings.BasicGroupName,
                  groupFields: [
                    PropertyPaneTextField('description', {
                      label: strings.DescriptionFieldLabel
                    })
                  ]
                }
              ]
            }
          ]
        };
      }
    }
    

    TargetWebPart:

    import { Version } from '@microsoft/sp-core-library';
    import {
      BaseClientSideWebPart,
      IPropertyPaneConfiguration,
      PropertyPaneTextField,
      IWebPartPropertiesMetadata,
      IPropertyPaneConditionalGroup,
      DynamicDataSharedDepth,
      PropertyPaneDynamicFieldSet,
      PropertyPaneDynamicField
    } from '@microsoft/sp-webpart-base';
    import { escape } from '@microsoft/sp-lodash-subset';
    
    import styles from './TargetWebPartWebPart.module.scss';
    import * as strings from 'TargetWebPartWebPartStrings';
    
    import { DynamicProperty } from '@microsoft/sp-component-base';
    
    export interface ITargetWebpartWebPartProps {
      description: string;
      x: DynamicProperty<number>;
      y: DynamicProperty<number>;
    }
    
    export default class TargetWebpartWebPart extends BaseClientSideWebPart<ITargetWebpartWebPartProps> {
    
      public render(): void {
    
        const x: number | undefined = this.properties.x.tryGetValue();
        const y: number | undefined = this.properties.y.tryGetValue();
        console.log(x);
        this.domElement.innerHTML = `
          <div class="${ styles.targetWebPart }">
            <div class="${ styles.container }">
              <div class="${ styles.row }">
                <div class="${ styles.column }">
                  <span class="${ styles.title }">TargetWebpart</span>                 
                    <div>Mouse X: ${ x == undefined ? '0' : x }</div>
                    <div>Mouse Y: ${ y == undefined ? '0' : y }</div>
                </div>
              </div>
            </div>
          </div>`;
      }
    
      protected get dataVersion(): Version {
        return Version.parse('1.0');
      }
    
      protected get propertiesMetadata(): IWebPartPropertiesMetadata {
        return {
          'x': {
            dynamicPropertyType: 'number'
          },
          'y': {
            dynamicPropertyType: 'number'
          }
        };
      }
    
      protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
        return {
          pages: [
            {
              groups: [
                {
                  groupFields: [
                    PropertyPaneDynamicFieldSet({
                      label: 'Select data source',
                      fields: [
                        PropertyPaneDynamicField('x', {
                          label: 'Position x'
                        })
                      ]
                    }),
                    PropertyPaneDynamicFieldSet({
                      label: 'Select data source',
                      fields: [
                        PropertyPaneDynamicField('y', {
                          label: 'Position y'
                        })
                      ]
                    })
                  ]
                }
              ]
            }
          ]
        };
      }
    }
    

    或者

    Sharing data between SPFx web parts

    【讨论】:

    • 我想在收到修改后的值并将值传递给该方法时调用一个方法?该怎么做?
    猜你喜欢
    • 2011-11-05
    • 2020-11-15
    • 2023-03-26
    • 1970-01-01
    • 2018-12-28
    • 2020-08-08
    • 2020-05-29
    • 2020-02-22
    • 2022-10-19
    相关资源
    最近更新 更多