简体   繁体   English

来自 3 个 URL 的 AJAX 请求以填充同一个表

[英]AJAX Request from 3 URLS to Populate Same Table

Is it possible to have 3 different AJAX requests, and have them all populate to the same DataTable(table)?是否可以有 3 个不同的 AJAX 请求,并将它们全部填充到同一个 DataTable(table)? I have tried and tried to create a AJAX with multiple URLS and it didn't work, but when I used just one URL, it works fine.我已经尝试并尝试创建一个带有多个 URL 的 AJAX 并且它不起作用,但是当我只使用一个 URL 时,它工作正常。 The issue is I need to pull from three different subsites.问题是我需要从三个不同的子站点中提取。

Here is my code:这是我的代码:

$(document).ready(function() {
  $('#myTable').DataTable({
    'ajax': {
      'url': "_api/web/lists/getbytitle('XDeliverables')/items?$select=Program, Deliverable, To, Date, Approved, Notes",
      'headers': { 'Accept': 'application/json;odata=nometadata' },
      'dataSrc': function(data) {
        return data.value.map(function(item) {
          return [
            item.Program,
            item.Deliverable,
            item.To,
            item.Date,
            item.Approved,
            item.Notes
          ];
        });
      }
    },
    columnDefs: [{
     
    }]
  });
});

Is it possible to do something along the lines of: (or atleast something similar)是否可以按照以下方式做一些事情:(或至少类似的事情)

$(document).ready(function() {
  $('#myTable').DataTable({
    'ajax': {
      'url': "_api/web/lists/getbytitle('XDeliverables')/items?$select=Program, Deliverable, To, Date, Approved, Notes",
      'headers': { 'Accept': 'application/json;odata=nometadata' },
      'dataSrc': function(data) {
        return data.value.map(function(item) {
          return [
            item.Program,
            item.Deliverable,
            item.To,
            item.Date,
            item.Approved,
            item.Notes
          ];
        });
      }
    },
    'ajax': {
      'url': "_api/web/lists/getbytitle('YDeliverables')/items?$select=Program, Deliverable, To, Date, Approved, Notes",
      'headers': { 'Accept': 'application/json;odata=nometadata' },
      'dataSrc': function(data) {
        return data.value.map(function(item) {
          return [
            item.Program,
            item.Deliverable,
            item.To,
            item.Date,
            item.Approved,
            item.Notes
          ];
        });
      }
    },
    'ajax': {
      'url': "_api/web/lists/getbytitle('ZDeliverables')/items?$select=Program, Deliverable, To, Date, Approved, Notes",
      'headers': { 'Accept': 'application/json;odata=nometadata' },
      'dataSrc': function(data) {
        return data.value.map(function(item) {
          return [
            item.Program,
            item.Deliverable,
            item.To,
            item.Date,
            item.Approved,
            item.Notes
          ];
        });
      }
    },
    columnDefs: [{
     
    }]
  });
});

You could do the ajax calls separately and concatenate the results together and then create the datatable您可以单独执行 ajax 调用并将结果连接在一起,然后创建数据表

First make a function with all your ajax calls首先使用所有 ajax 调用创建一个函数

async function getTableData() {
let baseURL = "_api/web/lists/getbytitle('${type}')/items?$select=Program, Deliverable, To, Date, Approved, Notes"

let tempURL = baseURL.replace("${type}", "XDeliverables");
let response1 = await $.ajax({
  url: tempURL,
  headers: { 'Accept': 'application/json;odata=nometadata' }
});

tempURL = baseURL.replace("${type}", "YDeliverables");
let response2 = $.ajax({
  url: tempURL,
  headers: { 'Accept': 'application/json;odata=nometadata' }
});

tempURL = baseURL.replace("${type}", "ZDeliverables");
let response3 = $.ajax({
  url: tempURL,
  headers: { 'Accept': 'application/json;odata=nometadata' }
});

let dataSet = [...response1, ...response2, ...response3];

// call function that creates the datatable
initializeTable(dataSet);

};

Then just initialize the table with the data然后用数据初始化表

function initializeTable(dataSet) {

    $('#myTable').DataTable( {
        data: dataSet,
        columns: [
            { title: "Name" },
            { title: "Position" },
            { title: "Office" },
            { title: "Extn." },
            { title: "Start date" },
            { title: "Salary" }
        ]
    });
};

Some docs.一些文档。

async/await异步/等待

Js source DataTable js源数据表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM