我是ReactJs的新手,我在MySQL上有一个产品表,并且在MySQL和Backend上使用Node.js在前端上在ReactJS上开发了一个动态表,我的动态表有四列:产品,数量,价格和总计,我当我在“产品”列上选择产品时,此价格将显示在其列上。
我的路由器是:
exports.getPrixprod = function(req,res) {
connection.query('SELECT PrixV FROM produits where Nomp = ?', [req.params.Nomp], function(error, results, fields) {
if (error) throw error;
res.send(JSON.stringify(results));
console.log(results);
});
}
我的课是:
class AjouterFacture extends Component {
constructor(props) {
super(props);
this.state = {
Produits: [],
Quantite: "",
Prix: "",
lineItemData: [],
selectprdt: props.match.params.selectprdt,
id: 0
};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleRowDelete = this.handleRowDelete.bind(this);
this.handleRowAdd = this.handleRowAdd.bind(this);
this.getTotal = this.getTotal.bind(this);
this.DatefactChanged = this.DatefactChanged.bind(this);
this.handleQuantiteChange = this.handleQuantiteChange.bind(this);
this.handleselectprdtChange = this.handleselectprdtChange.bind(this);
}
componentWillReceiveProps(nextProps) {
console.log("nextProps", nextProps);
}
componentDidMount() {
axios({
method: "get",
url: "/app/getNomprod/",
withCredentials: true,
}).then(response => {
if (response && response.data) {
this.setState({
Produits: response.data
});
}
}).catch(error => console.log(error));
}
popupAjout(event) {
const getAlert = () => (<SweetAlert
success
title="Ajout facture"
onConfirm={ this.handleSubmit}
>
UNe facture est ajoutée avec succés
</SweetAlert>);
this.setState({
alert: getAlert()
});
}
handleSubmit() {
var lp = {
Nomp: this.state.selectprdt,
QuantiteF: this.state.QuantiteF,
}
axios({
method: 'post',
url: '/app/ajouterlp/',
data: lp,
withCredentials: true,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}).then(function(response) {
this.setState({
alert: null
});
this.props.history.push('/factures/listefactures')
}.bind(this))
handleQuantiteChange(index, value) {
const rowDataCopy = this.state.rowData.slice(0);
rowDataCopy[index] = Object.assign({}, rowDataCopy[index], {
Quantite: parseInt(value, 10)
});
this.setState({
rowData: rowDataCopy
});
}
handleselectprdtChange(index, value) {
const rowDataCopy = this.state.rowData.slice(0);
rowDataCopy[index] = Object.assign({}, rowDataCopy[index], {
selectprdt: value
});
axios({
method: "get",
url: "/app/getPrixprod/" + this.props.match.params.selectprdt,
withCredentials: true,
}).then(response => {
if (response && response.data) {
this.setState({
Prix: response.data
});
}; console.log(response.data);
}).catch(error => console.log(error));
this.setState({
rowData: rowDataCopy
});
}
render() {
let {
Clients
} = this.state.Clients;
var Cd = {
pointerEvents: 'none'
}
let {
Produits
} = this.state.Produits;
let {
rowData
} = this.state.rowData;
const Prix = this.state;
return (<div className="animated fadeIn">
<h6> <Label ><strong>Veuillez ajouter au moins un produit : </strong></Label></h6>
<Table responsive style={items} >
<thead style={back}>
<tr>
<th>PRODUIT</th>
<th>QUANTITE</th>
<th>PRIX UNITAIRE</th>
<th>TOTAL</th>
<th></th>
</tr>
</thead>
<tbody>
{this.state.rowData.map((data, index) => (
<tr key={index} id={index}>
<td>
{" "} <Input type="select" name="selectedcl" id="selectcl"
placeholder="Veuillez sélectionner un produit" value={data.selectprdt}
onChange={(e) => this.handleselectprdtChange(index, e.target.value)} >
<option key={-1} hidden>Choisisr un produit</option>
{ this.state.Produits.map((pdt, i) =>
<option key={i} value="select">{pdt.Nomp}</option>
)}
</Input>
</td>
<td><Input type="number"
value={data.Quantite || 0} onChange={(e) => this.handleQuantiteChange(index, e.target.value)}/></td>
<td>
<p>{ this.state.Prix} DT</p>
</td>
<td >
<p key={index} className='pa2 mr2 f6'>{(data.Quantite || 0) * (Prix )} DT</p>
</td>
<td>
<Button onClick={(e) => this.handleRowDelete(index)} active style={center} size="sm" color="danger" className="btn-pill" aria-pressed="true">Effacer</Button>
</td>{" "}
</tr>
))}
<tr>
<td/>
<td/>
<td/>
<td/>
<td><Button onClick={this.handleRowAdd} active style={center} size="sm" color="info" className="btn-pill" aria-pressed="true">Ajouter une ligne</Button></td>
</tr>
</tbody>
<tfoot>
<tr>
<th></th>
<th >Grand total :</th>
<th>{this.getTotal()} DT</th>
<th></th>
</tr>
</tfoot>
</Table>
</div>);
}
getTotal() {
let grandTotal = 0;
const rowTotals = this.state.rowData.map(row => (row.Quantite * row.PrixV) || 0);
if (rowTotals.length > 0) {
grandTotal = rowTotals.reduce((acc, val) => acc + val);
}
return grandTotal;
}
handleRowDelete(row) {
const rowDataCopy = this.state.rowData.slice(0);
rowDataCopy.splice(row, 1);
this.setState({
rowData: rowDataCopy
});
}
handleRowAdd() {
let id = this.state.id;
id = id++;
const rowDataCopy = this.state.rowData.slice(0);
rowDataCopy.push({
selectprdt: "",
Quantite: 0,
Prix: 0
});
this.setState({
rowData: rowDataCopy,
id: id
});
}
}
export default AjouterFacture;
当我使用Postman运行后端时,它运行良好,但是当运行前端时,我在控制台上获得了价格[]
,并且在网络控制台上获得了选择undefined
的值:
我该如何解决?
答案 0 :(得分:1)
更改:
let {
Produits
} = this.state.Produits;
为
let {
Produits
} = this.state;
答案 1 :(得分:0)
要引用this.props.match.params
,您需要使用react-router
。如果您已经在使用该库,则需要使用withRouter
HOC包装该组件:
import { withRouter } from 'react-router';
class AjouterFacture extends Component {
...
}
export default withRouter(AjouterFacture);
然后您将获得this.props.match.params.selectprdt
。但是不要忘了以这种方式在路由器组件中某个位置的反应路由器路径中包含此属性:
<Route path='/facture/:selectprdt' component={AjouterFacture}>
并尝试不使用(它不应该顺便工作):
let {
Clients
} = this.state.Clients;
let {
Produits
} = this.state.Produits;
let {
rowData
} = this.state.rowData;
并改用这些:
const {
Clients, Produits, rowData
} = this.state;
您可以详细了解here