在来自两个 REST API 或来自 2 个不同的 getbytitle 名称的 React Sharepoint 中的单个 table 中显示数据
Display data in a single table in React Sharepoint from two REST API or from 2 different getbytitle name
我在 1 API/URL 中有 2 列 [即- 名称和 ID] 和 2 API/URL 中的 3 列 [即 ID、电源和温度]。如何使用 ajax REST API get 方法连接这 2 API/URL 中的列以在网页上显示数据。
也就是说,我的一组数据在APP_NAME1,另一组在APP_NAME2
目前为止我一直在用这个API:
$.ajax({
url: `${this.props.siteUrl}/_api/Web/Lists/GetByTitle('APP_NAME')/items?$filter=Name eq '${this.state.Name}'`,
type: "GET",
dataType: "json",
headers: {
accept: "application/json;odata=verbose",
},
success: (resultData) => {
var outputData = {
accounting: [],
};
console.log(resultData.d.results);
}
});
您可以使用 Promise.all() 加入这 2 个请求:
let promise1= $.ajax({url: ".../_api/Web/Lists/GetByTitle('APP_NAME1')/items?..."});
let promise2= $.ajax({url: ".../_api/Web/Lists/GetByTitle('APP_NAME2')/items?..."});
Promise.all([promise1, promise=2]).then((values) => {
console.log(values);
});
在回调中,您可以通过循环合并这 2 个数组。
演示:左连接
for(let i = 0; i < itemsA.length; i++)
{
let hassameid= itemsB.find(element => element['id'] == itemsA[i]["id"]);
if(hassameid){
itemsA[i]["tableB_pro01"]= hassameid["tableB_pro01"];
}
}
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
我在 1 API/URL 中有 2 列 [即- 名称和 ID] 和 2 API/URL 中的 3 列 [即 ID、电源和温度]。如何使用 ajax REST API get 方法连接这 2 API/URL 中的列以在网页上显示数据。
也就是说,我的一组数据在APP_NAME1,另一组在APP_NAME2
目前为止我一直在用这个API:
$.ajax({
url: `${this.props.siteUrl}/_api/Web/Lists/GetByTitle('APP_NAME')/items?$filter=Name eq '${this.state.Name}'`,
type: "GET",
dataType: "json",
headers: {
accept: "application/json;odata=verbose",
},
success: (resultData) => {
var outputData = {
accounting: [],
};
console.log(resultData.d.results);
}
});
您可以使用 Promise.all() 加入这 2 个请求:
let promise1= $.ajax({url: ".../_api/Web/Lists/GetByTitle('APP_NAME1')/items?..."});
let promise2= $.ajax({url: ".../_api/Web/Lists/GetByTitle('APP_NAME2')/items?..."});
Promise.all([promise1, promise=2]).then((values) => {
console.log(values);
});
在回调中,您可以通过循环合并这 2 个数组。
演示:左连接
for(let i = 0; i < itemsA.length; i++)
{
let hassameid= itemsB.find(element => element['id'] == itemsA[i]["id"]);
if(hassameid){
itemsA[i]["tableB_pro01"]= hassameid["tableB_pro01"];
}
}
参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find