两个问题:我正在研究spfx,对于这个特定的示例,我不了解“发送”按钮如何与setComment()函数关联。我创建了一个“更新”按钮,我想用它来更新刚刚创建的项目。即使我已经将道具传递给它,它也没有执行任何操作(可能不正确!)
对于“更新”按钮,我尝试使用React在JSX中使用onClick传递道具,但这是行不通的。我也尝试过在没有$的情况下执行此操作。我创建了一组单独的函数_readListItem()和_getListItem()来帮助我,但不确定如何将它们与_updateListItem()结合使用,或者不确定是否需要它们。
import { Environment, EnvironmentType, Version } from '@microsoft/sp-core-library';
import { ISPHttpClientOptions, SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http';
import { escape } from '@microsoft/sp-lodash-subset';
import { BaseClientSideWebPart, IPropertyPaneConfiguration, PropertyPaneTextField } from '@microsoft/sp-webpart-base';
import * as strings from 'FeedbackWebpartWebPartStrings';
import styles from './FeedbackWebpart.module.scss';
import { IFeedbackWebPartProps } from './IFeedbackWebPartProps';
import {ISPListItem} from './ISPListItem';
export default class FeedbackWebpartWebPart extends BaseClientSideWebPart<IFeedbackWebPartProps> {
public render(): void {
this._updateListItem = this._updateListItem.bind(this);
this._readListItem = this._readListItem.bind(this);
this.domElement.innerHTML = `
<div>
<p class='${styles.titleText}'>Job Evaluation Form</p>
<i class='ms-Icon ms-Icon--NoteForward' aria-hidden='true'></i>
<p class='${styles.labelText}'>Name</p>
<input type='text' class='${styles.input}' maxlength='255' placeholder='${escape(this.properties.hintText)}' />
<br>
<br>
<p class='${styles.labelText}'>Job Title</p>
<input type='text' class='${styles.input}' maxlength='255' placeholder='${escape(this.properties.hintText)}' />
<br>
<p class='${styles.successIndicator}'></p>
<br>
<br>
<br>
<button type='button' class='ms-Button'><span class='ms-Button-label'>Send</span></button>
<br>
<br>
<button type='button' class='ms-Button' onClick='${this._updateListItem}'><span class='ms-Button-label'>Update</span></button>
</div>`;
//The binding below allows setComment to be used in the render and outside of it.
this.setComment = this.setComment.bind(this);
this._updateListItem = this._updateListItem.bind(this);
this._readListItem = this._readListItem.bind(this);
//Grab ALL the <input> elements
// [0] - just want the first element
// cast this generic element to specific type of element (which I know that it is) as the variable is expecting a specific type
const textInput: HTMLInputElement = this.domElement.getElementsByTagName("INPUT") [0] as HTMLInputElement;
// setComment is used in an event listener here to be called upon whenever a user types into this input field.
textInput.addEventListener("keyup", this.setComment);
this.sendFeedback = this.sendFeedback.bind(this);
const submitbutton: HTMLButtonElement = this.domElement.getElementsByTagName("BUTTON") [0] as HTMLButtonElement;
submitbutton.onclick = this.sendFeedback;
submitbutton.disabled = true;
}
private _updateListItem(): void {
const url: string = this.context.pageContext.site.absoluteUrl+"/_api/web/lists/getbytitle('Feedback')/items(1)";
const itemDefinition : any = {
"Title" : "Modified title field value!"};
const headers:any = {
"X-HTTP-Method":"MERGE",
"IF-MATCH": "*",
};
const spHttpClientOptions: ISPHttpClientOptions = {
"headers": headers,
"body": JSON.stringify(itemDefinition)
};
this.context.spHttpClient.post(url, SPHttpClient.configurations.v1,spHttpClientOptions)
.then((response: SPHttpClientResponse) => {
if (response.status === 204) {
this._operationResults.innerHTML = "Update: List Item updated successfully.";
} else {
this._operationResults.innerHTML = "Update: List Item update failed. " +response.status+" - "+response.statusText;
}
});
}
private sendFeedback(): void {
this.context.statusRenderer.clearError(this.domElement);
const paragraphElement: HTMLParagraphElement = this.domElement.getElementsByClassName(styles.successIndicator) [0] as HTMLParagraphElement;
paragraphElement.innerHTML="";
if(this._commentText === undefined || this._commentText.length === 0) {
this.context.statusRenderer.renderError(this.domElement, "Please type in a comment or a suggestion.");
return;
}
if(Environment.type === EnvironmentType.Local) {
this.context.statusRenderer.renderError(this.domElement, "Feedback can't be saved when running in local workbench");
return;
}
const url: string = this.context.pageContext.site.absoluteUrl+"/_api/web/lists/getbytitle('Feedback')/items";
const item : any = {
"Title": this._commentText,
"URL": window.location.href,
"JobTitle": this._jobTitle
};
const spHttpClientOptions: ISPHttpClientOptions = {
"body": JSON.stringify(item)
};
很抱歉,有很多代码,但是我不知道如果我不提供它,您将如何理解我需要知道的内容。
我希望了解单击“发送”按钮的工作方式和“更新”按钮以更新任何更改的项目。
答案 0 :(得分:1)
看起来“发送”按钮的onClick事件是用以下代码设置的:
const submitbutton: HTMLButtonElement = this.domElement.getElementsByTagName("BUTTON") [0] as HTMLButtonElement;
submitbutton.onclick = this.sendFeedback;
此处getElementsByTagName(“ BUTTON”)[0]返回在上述HTML中使用创建的第一个元素。
我想采用相同的方法,更新按钮可以这样处理:
const updateButton: HTMLButtonElement = this.domElement.getElementsByTagName("BUTTON") [1] as HTMLButtonElement;
updateButton.onclick = this._updateListItem.bind(this);
请注意将索引从[0]更改为[1],以使用第二个按钮,以及bind()的用法。如果没有绑定,则在调用_updateListItem时将无法定义“ this”。