从反应共享点中的两个不同 table/lists 检索数据并显示

Retrieve data and display from two different table/lists in react sharepoint

我在 2 个不同的 table/lists 中有 2 个不同的数据 [一个是 APP_NAME1,另一个是 APP_NAME2]。在一个 table 中,我有姓名和 ID,在另一个中,我有套件、ID、温度。有什么方法可以从 2 tables 中获取和显示数据到 1.

我可以像这样从 table 或 APP_NAME1 中获取详细信息:

$.ajax({
      url: `${this.props.siteUrl}/_api/Web/Lists/GetByTitle('APP_NAME1')/items`,
      type: "GET",
      dataType: "json",
      headers: {
        accept: "application/json;odata=verbose",
      },
      success: (resultData) => {
        var outputData = {
          accounting: [],
        };
        console.log(resultData.d.results);
        $.each(resultData.d.results, (index, value) => {
          outputData.accounting.push({
            Name: value.Name,
          });
        });

您可以使用 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[0]);
  console.log(values[0]);
});

在回调中,您可以通过循环合并这 2 个数组。

演示:

let finalTableData = [];
for(let i = 0; i < itemsA.length; i++)
{
  let lastIndex = finalTableData.push( {id: itemsA[i]["id"], prop1: itemsA[i]["prop1"]});

  for(let j = 0; j < itemsB.length; j++)
  {
    if(itemsB[j]["id"] === itemsA[i]["id"])
    {
      finalTableData[lastIndex]["propc"] = itemsB[j]["propc"];
      break;
    }
  }
}

参考: