试图用JavaScript计算SharePoint列表值
Trying to count SharePoint List Values with JavaScript
Hello stack overflow community…
感谢您迄今为止提供的所有帮助,尽管我从未在这个网站上发布过。我是初学者……所以这意味着我开始工作的大部分东西都是在试图把一个方形的钉子塞进一个圆孔中,次数太多了,直到最后边缘被磨损得足够大,它实际上适合……
无论如何……由于SharePoint的Today()问题,以及需要将数据与当前日期/时间进行比较,我一直在努力编写自己的代码,并在我也可以访问的SharePoint网站上显示数据。
到目前为止,我所有的工作都按照预期进行,只有一个小缺陷…
我不知道如何计数值,即使我可以在两个值之间进行比较。我相信这与如何从SharePoint列表行中检索数据并将其添加到HTML表中有关。
相关代码如下:
$(document).ready(function () {
$().SPServices({
operation: "GetListItems",
async: false,
CAMLRowLimit: 20,
listName: "Announcements",
completefunc: FirstFunc
});
});
function FirstFunc(xData, Status) {
var index = 0;
$documentListtable = $("#tableFirstFunc");
$(xData.responseXML).find("z'':row, row").each(function () {
var LOTOSPLink =$(this).attr("ows_LOTODocLink");
var _Title = $(this).attr("ows_Title");
var ahref = "<a href='" + LOTOSPLink + "'>";
var anchor = "</a>"
var Titles = ahref + _Title + anchor
//Start of AReview
var _AReview = $(this).attr("ows_AReview");
var astartDateTime = $(this).attr("ows_AReview");
var astartDate = $(this).attr("ows_AReview").split(" ")[0];
var astartTime = $(this).attr("ows_AReview").split(" ")[1];
var astartDateParts = astartDate.split("-");
var aSPDYear = astartDateParts[0];
var aSPDMonth = astartDateParts[1];
var aSPDDay = astartDateParts[2];
var aSPDJoin = aSPDMonth+'/'+aSPDDay+'/'+aSPDYear;
var astartTimeParts = astartTime.split(":");
var aSPTHour = astartTimeParts[0];
var aSPTMin = astartTimeParts[1];
var aSPTSec = astartTimeParts[2];
//Combine SharePoint Date & Time split parts back together in JS Date Object
//format and than convert to millisecons to compare dates
var aReviewGraphic = ""
if(aSPDTValue === 18000000) {
aReviewGraphic="<img src="sites/Somesite/NAGlassyButton20.png>";
}
else if(aSPDTValue >= firstDCMonth && aSPDTValue < lastDCMonth) {
aReviewGraphic="<img src="/sites/Somesite/GreenButtNew20.png>";
}
else if(aSPDTValue >= firstDCYear && aSPDTValue < firstDCMonth) {
aReviewGraphic="<img src="/sites/Somesite//GreenButtOld20.png>";
}
else if(aSPDTValue < firstDCYear) {
aReviewGraphic="<img src="/sites/Somesite/RedButt20.png>";
}
var $row = $("#templates").find(".row-template").clone();
$row.find(".Titles").html(Titles);
$row.find(".aReviewGraphic").html(aReviewGraphic);
$row.find(".bReviewGraphic").html(bReviewGraphic);
$row.find(".cReviewGraphic").html(cReviewGraphic);
$row.find(".dReviewGraphic").html(dReviewGraphic);
$row.find(".NewModiDReviewGraphic").html(NewModiDReviewGraphic);
$documentListtable.append($row);
});
}
我正在按预期更改表格中的图形,数据正在按预期显示…但我不知道怎么计算。看起来好像是一次读取一行,然后一个接一个地放入HTML表中,所有的比较都是逐行进行的。我这么说是因为从sharepoint列表中拉入的每一行数据都会触发我的警报(消息)。
是否有办法将这些数据存储在本地数组中…当然有一些方法可以做到这一点,但我是新手,我甚至不知道该问什么正确的问题…
if语句中的18000000是1970年1月1日的测试日期,我现在使用这个日期进行测试,因为我发现sharepoint列表中任何空白的字段都会导致查询退出返回在此之后的任何行。我的解决方案是使用1/1/1970作为默认值,并使用它与将字段留空相同。我确信这是一个粗糙的方法来解决一个很容易解决的问题,但这是我所能想到的。
任何帮助都会很感激…史蒂夫。
我试着理解…我的建议是将所有html存储到一个变量中,然后将代码注入到表中。
// somewhere you should have your HTML code
// <table id="templates"></table>
$(document).ready(function () {
$().SPServices({
operation: "GetListItems",
async: false,
CAMLRowLimit: 20,
listName: "Announcements",
completefunc: FirstFunc
});
});
function FirstFunc(xData, Status) {
var index = 0;
$documentListtable = $("#tableFirstFunc");
// create a variable where to store the html code
var htmlData = "";
// go thru the data received by the query
$(xData.responseXML).find("z'':row, row").each(function () {
// we look at one row
var LOTOSPLink =$(this).attr("ows_LOTODocLink");
var _Title = $(this).attr("ows_Title");
var ahref = "<a href='" + LOTOSPLink + "'>";
var anchor = "</a>"
var Titles = ahref + _Title + anchor
//Start of AReview
var _AReview = $(this).attr("ows_AReview");
var astartDateTime = $(this).attr("ows_AReview");
var astartDate = $(this).attr("ows_AReview").split(" ")[0];
var astartTime = $(this).attr("ows_AReview").split(" ")[1];
var astartDateParts = astartDate.split("-");
var aSPDYear = astartDateParts[0];
var aSPDMonth = astartDateParts[1];
var aSPDDay = astartDateParts[2];
var aSPDJoin = aSPDMonth+'/'+aSPDDay+'/'+aSPDYear;
var astartTimeParts = astartTime.split(":");
var aSPTHour = astartTimeParts[0];
var aSPTMin = astartTimeParts[1];
var aSPTSec = astartTimeParts[2];
//Combine SharePoint Date & Time split parts back together in JS Date Object
//format and than convert to millisecons to compare dates
var aReviewGraphic = "";
// I don't understand why you use this variable that hasn't been initialized...
if (aSPDTValue === 18000000) {
aReviewGraphic='<img src="sites/Somesite/NAGlassyButton20.png>';
}
else if(aSPDTValue >= firstDCMonth && aSPDTValue < lastDCMonth) {
aReviewGraphic='<img src="/sites/Somesite/GreenButtNew20.png>';
}
else if(aSPDTValue >= firstDCYear && aSPDTValue < firstDCMonth) {
aReviewGraphic='<img src="/sites/Somesite//GreenButtOld20.png>';
}
else if(aSPDTValue < firstDCYear) {
aReviewGraphic='<img src="/sites/Somesite/RedButt20.png>';
}
htmlData += '<tr><td class="Titles">'+Titles+'</td><td class="aReviewGraphic">'+aReviewGraphic+'</td><td class="bReviewGraphic">'+bReviewGraphic+'</td><td class="cReviewGraphic">'+cReviewGraphic+'</td><td class="dReviewGraphic">'+dReviewGraphic+'</td><td class="NewModiDReviewGraphic">'+NewModiDReviewGraphic+'</td></tr>';
// you can increment your index if you want
index++;
});
alert("There are "+index+" rows");
// add the HTML data into the table
$documentListtable.append(htmlData);
}
- 如何将 Javascript 应用于分组的 SharePoint 列表
- 如何从sharepoint列表中使用javascript显式加载列
- 使用基于值的jQuery代码更改SharePoint列表字段颜色
- 将 SharePoint 列表数据转换为 HTML
- 提取 SharePoint 列表中的所有项目
- 使用 JavaScript 检索多个 SharePoint 列表
- 如何使用 javascript 在 SharePoint 列表中批量创建项目
- 从 SharePoint 列表列获取超链接
- 使用 Javascript 无限制地查询 SharePoint 列表项
- 使用 JavaScript 和 REST 从 SharePoint 列表中删除项目
- 将 Excel 数据导入到 SharePoint 列表中的查找列
- 如何将文本框追加到复选框列表项,该复选框列表项来自选择列表项的 SharePoint 列表
- 在 SharePoint 列表“新建项”页中创建动态查阅列
- 动态创建选择元素并从 SharePoint 列表填充选项
- 如何使用java脚本和jquery从sharepoint列表中获取数据
- 对Sharepoint列表列应用类型验证
- 使用Sharepoint列表值时未显示JS警报
- 需要帮助使用SharePoint列表和PSServices将其他系列添加到Highcharts图中
- 尝试使用SharePoint列表和Highcharts在图表上放置点
- 比较SharePoint列表中的两个int列