我有一个项目,我需要React Js在mssql数据库中搜索特定查询,然后返回结果
左边是Form,右边是我想要推送数据的地方,它们位于不同的jsx文件中,因此我需要将数据库中返回的数据放入数组const并将其传递给父母并退回到右边的孩子
我将如何做到这一点
左框的代码
import React, { Component } from 'react';
import TextInput from '../SmallBits/FormItems/TextInput';
export default class LeftBox extends Component {
constructor(props){
super(props);
this.state = {
name: '',
forename: '',
surname: '',
caseID: '',
address: ''
}
this.handleSurnameChange = this.handleSurnameChange.bind(this);
this.handleForenameChange = this.handleForenameChange.bind(this);
this.handleCaseIDChange = this.handleCaseIDChange.bind(this);
this.handleAddressChange = this.handleAddressChange.bind(this);
}
handleSurnameChange(e) {
e.preventDefault();
this.setState({ surname: e.target.value });
}
handleForenameChange(e) {
e.preventDefault();
this.setState({ forename: e.target.value });
}
handleCaseIDChange(e) {
e.preventDefault();
this.setState({ caseID: e.target.value });
}
handleAddressChange(e) {
e.preventDefault();
this.setState({ address: e.target.value });
}
handleFormSubmit(e) {
e.preventDefault();
const formPayload = {
name: this.state.surname,
forename: this.state.forename
};
console.log('Send this in a POST request:', formPayload);
}
render() {
return (
<div className="row">
<div className="col-md-12 well-mod well-lg">
<form>
<div className="row">
<div className="col-md-12">
<div className="col-md-5">
<span className="input-group-addon"><i className="glyphicon glyphicon-user"></i>{nbsp}Forename</span>
<TextInput className="form-control" inputType={'text'} name={'Forename'} controlFunc={this.handleForenameChange} content={this.state.forename} placeholder={'Enter Forename'} />
</div>
</div>
</div>
<div className="row">
<div className="col-md-12">
<div className="col-md-5">
<span className="input-group-addon"><i className="glyphicon glyphicon-user"></i>{nbsp}Surname</span>
<TextInput className="form-control" inputType={'text'} name={'Surname'} controlFunc={this.handleSurnameChange} content={this.state.surname} placeholder={'Enter Surname'} />
</div>
</div>
</div>
<div className="row">
<div className="col-md-12">
<div className="col-md-5">
<span className="input-group-addon"><i className="glyphicon glyphicon-user"></i>{nbsp}Case ID</span>
<TextInput className="form-control" inputType={'number'} name={'CaseID'} controlFunc={this.handleCaseIDChange} content={this.state.caseID} placeholder={'Enter Case ID'} />
</div>
</div>
</div>
<div className="row">
<div className="col-md-12">
<div className="col-md-5">
<span className="input-group-addon"><i className="glyphicon glyphicon-envelope"></i>{nbsp}Email Address</span>
<TextInput className="form-control" inputType={'text'} name={'Address'} controlFunc={this.handleAddressChange} content={this.state.address} placeholder={'Enter Email Address'} />
</div>
</div>
</div>
<div className="col-md-12">
<div className="col-md-5">
<button className="btn btn-sm btn-success navbar-btn pull-right glyphicon glyphicon-search"> Search</button>
</div>
</div>
</form>
</div>
</div>
);
}
}
右框代码
import React, { Component } from 'react';
export default class RightBox extends Component {
render() {
return (
<div className="row">
<div className="col-md-12 well-mod well-lg">
<p>This is the Right Box</p>
</div>
</div>
);
}
}
主要内容代码
import React, { Component } from 'react';
import LeftBox from '../Boxes/LeftBox';
import RightBox from '../Boxes/RightBox';
import '../../../styles/Cust.css';
export default class MainContent extends Component {
render() {
return (
<div className="ModContainer">
<div className="col-md-12">
<div className="col-md-7">
<LeftBox />
</div>
<div className="col-md-1-mod">
</div>
<div className="col-md-4">
<RightBox />
</div>
</div>
</div>
);
}
}
文本输入代码
import React from 'react';
const InputHeight = {
height:'50px',
}
const TextInput = (props) => (
<div className="form-group">
<input
className="form-control"
name={props.name}
type={props.inputType}
value={props.content}
onChange={props.controlFunc}
placeholder={props.placeholder}
style={InputHeight}
required />
</div>
);
TextInput.propTypes = {
inputType: React.PropTypes.oneOf(['text', 'number', 'email']).isRequired,
name: React.PropTypes.string.isRequired,
controlFunc: React.PropTypes.func.isRequired,
content: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]).isRequired,
placeholder: React.PropTypes.string,
};
export default TextInput;
我将在sql之前为后端添加一层C#,这样我正在寻找将连接此搜索按钮连接到C#后端的方法。
我会很乐意帮助我,我会非常感激
答案 0 :(得分:1)
您的C#后端需要提供API端点。例如,您将定义一个接受查询参数对象的路由/api/search
,并以JSON格式返回结果。
在前端,您必须向后端发出HTTP请求,以根据查询检索结果。
您的MainComponent将负责调用API并为您的RightBox提供结果。