javascript - 如何在单个页面中使用来自两个 REST API 的数据表显示两个表?
问题描述
我正在使用 Sharepoint REST API 在 HTML 页面中使用数据表显示列表项。我想在同一页面中显示两个表格。所以,我有两个 REST API,我能够在第一个表中显示第一个 REST API 数据。但无法在第二个表中显示第二个休息 api 数据。如果在下面的代码中发现错误,请清除我的错误:
文件.js
$(document).ready(function () {
loadTableItems();
loadCountItems();
});
function loadTableItems() {
var oDataUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('abc')/items?$select=Title,Count,Modified,OData__x0075_hb9";
$.ajax({
url: oDataUrl,
type: "GET",
dataType: "json",
headers: {
"accept": "application/json;odata=verbose"
},
success: mySuccHandler,
error: myErrHandler
});
}
function mySuccHandler(data) {
try {
var dataTable = $('#table_id').DataTable();
if (dataTable != 'undefined') {
dataTable.destroy();
}
dataTable = $('#table_id').DataTable({
responsive: true,
"lengthChange": false,
paging: true,
bAutowidth: false,
columnDefs: [
{ className: "dt-head-center", "targets": [0, 1, 2, 3] },
],
"aaData": data.d.results,
"aoColumns": [{
"mData": "OData__x0075_hb9",
},
{
"mData": "Title", sWidth: '30%'
},
{
"mData": "Count", "sClass": "column-align"
},
{
"mData": "Modified", "sClass": "column-align",
"render": function (mData) {
var date = moment(mData);
return (date.format('ddd DD-MM-YYYY hh:mm A'));
}
}],
"dom": 'lBftipr',
buttons: [
{
extend: 'excelHtml5',
text: 'Export to Excel',
title: 'Analytics',
}]
});
}
catch (e) {
alert(e.message);
}
}
function myErrHandler(data, errCode, errMessage) {
alert("Error: " + errMessage);
}
function loadCountItems() {
var restUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('xyz')/items?$select=vmmm,zpdq,zlvx,t75f";
$.ajax({
url: restUrl,
type: "GET",
dataType: "json",
headers: {
"accept": "application/json;odata=verbose"
},
success: mySuccHandler,
error: myErrHandler
});
}
function mySuccHandler(data) {
try {
var countTable = $('#art_id').DataTable();
if (countTable != 'undefined') {
countTable.destroy();
}
countTable = $('#art_id').DataTable({
responsive: true,
"lengthChange": false,
paging: true,
bAutowidth: false,
"aaData": data.d.results,
"aoColumns": [{
"mData": "vmmm",
},
{
"mData": "zpdq",
},
{
"mData": "zlvx",
},
{
"mData": "t75f",
}
],
});
}
catch (e) {
alert(e.message);
}
}
function myErrHandler(data, errCode, errMessage) {
alert("Error: " + errMessage);
}
文件.html
<body>
<div>
<table id="table_id" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Practice</th>
<th>Title</th>
<th>Visits</th>
<th>Last Visited</th>
</tr>
</thead>
</table>
</div>
<br>
<br>
<br>
<h3 style="text-align:left; color:rgb(0, 112, 173)">Artifacts per Practice</h3>
<br>
<div>
<table id="art_id" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Engagement</th>
<th>Process</th>
<th>Practice</th>
<th>Artifact Count</th>
</tr>
</thead>
</table>
</div>
</body>
解决方案
解决了。两个表应该有不同的成功和错误函数。
推荐阅读
- html - 为什么我的 axios 获取请求不起作用(登录)
- scheduling - 如何触发现在预定的 oozie 作业?
- react-native - React Native onTextLayout 方法在 iOS 上的 Text 组件问题
- javascript - 如何在 vue-echarts-v4.1 中使用 updateAxisPointer 函数?
- angular - Angularx-social-login-redirect_uti,spring boot 的问题
- java - Gradle 没有解决任何依赖关系
- java - Micronaut HTTP 客户端 - 反序列化泛型类型 - 用于 API 测试
- sql - 在 SQL Server 2012 中查找多个结果(不重复)
- dart - 如何在 Dart 中解析 m3u
- javascript - 当我使用 setstate 调用函数时,状态值不会改变