【问题标题】:Sort and remove duplicate items from array - REST API SharePoint lists对数组中的重复项进行排序和删除 - REST API SharePoint 列表
【发布时间】:2020-01-23 10:48:02
【问题描述】:

我已使用 REST API 在网格中成功整理了来自 SharePoint Online 的多个列表的结果。

但是,整理后的结果集数组不按顺序排列并且有重复的元素。我需要订购网格,以便它根据创建的日期 desc 进行排序,并且只有唯一的项目。

我使用了下面的代码和 RemoveDuplicatesItemsenter image description here 函数。代码执行得很好并删除了重复项,但是一旦我删除了重复项,网格就会将所有列显示为未定义。如果我不执行重复功能,网格会显示数据,但有重复。

任何技术建议都会对我有很大帮助。期待着听到您的意见。谢谢大家。

<script src="https://dev.sharepoint.com/sites/dev/SiteAssets/jquery-1.10.2.js"></script>
<script src="https://dev.sharepoint.com/sites/dev/SiteAssets/jquery.min.js"></script>
<script>
var ListDataCollection = {
    Lists:[{
        Url:"https://dev.sharepoint.com/sites/dev/sites1/_api/web/lists/getbytitle('Employee')/items?$top=5&$orderby=Created desc&$select=Title,Description,Action_x0020_By_x003a_,RequestFrom_x003a_,Created&$filter=Category eq 'Key Events and Decision'",
        Completed:false
    },{
        Url:"https://dev.sharepoint.com/sites/dev/sites2/_api/web/lists/getbytitle('Employee')/items?$top=5&$orderby=Created desc&$select=Title,Description,Action_x0020_By_x003a_,RequestFrom_x003a_,Created&$filter=Category eq 'Key Events and Decision'",
        Completed:false
    },{
        Url:"https://dev.sharepoint.com/sites/dev/sites3/_api/web/lists/getbytitle('Employee')/items?$top=5&$orderby=Created desc&$select=Title,Description,Action_x0020_By_x003a_,RequestFrom_x003a_,Created&$filter=Category eq 'Key Events and Decision'",
        Completed:false
    }]
};
var tableContent = '<table id="tableEmployee" style="width:100%" border="1 px"><thead><tr><td><b>No</b></td>' + 

'<td><b>Description</b></td>' + '<td><b>Action By</b></td>' + '<td><b>Request From</b></td>' + '<td><b>Created</b></td>'+ 

'</tr></thead><tbody>';

function groupBy(items,propertyName)
{
    var result = [];
    $.each(items, function(index, item) {
       if ($.inArray(item[propertyName], result)==-1) {
          result.push(item[propertyName]);
       }
    });
    return result;
}
function RemoveDuplicateItems(items, propertyName) {
    var result = [];
        $.each(items, function (index, item) {
            if ($.inArray(item[propertyName], result) == -1) {
                result.push(item[propertyName]);
            }
        });
    return result;
}
function ifAllCompleted() {
    for (var i=0;i<ListDataCollection.Lists.length;i++) {
					
        if (!ListDataCollection.Lists[i].Completed) {
            return false;
        }			
    }
	console.log('Completed');   
    var arrayOfAllData = ListDataCollection.Lists[0].Data.d.results;
    arrayOfAllData = arrayOfAllData.concat(ListDataCollection.Lists[1].Data.d.results);
    arrayOfAllData = arrayOfAllData.concat(ListDataCollection.Lists[2].Data.d.results);
	console.log('Total elements : ' + arrayOfAllData.length);
	arrayOfAllData=RemoveDuplicateItems(arrayOfAllData,'Description')
	for( var i=0; i<arrayOfAllData.length;i++)
	{
					tableContent += '<tr>';
                    tableContent += '<td>' + arrayOfAllData[i].Title+ '</td>';
                    tableContent += '<td>' + arrayOfAllData[i].Description+ '</td>';
                    tableContent += '<td>' + arrayOfAllData[i].Action_x0020_By_x003a_+ '</td>';
                    tableContent += '<td>' + arrayOfAllData[i].RequestFrom_x003a_+ '</td>';
					tableContent += '<td>' + arrayOfAllData[i].Created+ '</td>';
                    tableContent += '</tr>';
	}
	$('#employeeGrid').append(tableContent);
	
	
}

$(document).ready(function(){
    for (var x=0;x<ListDataCollection.Lists.length;x++) {
        $.ajax({
            url:ListDataCollection.Lists[x].Url,
            indexValue:x,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data, status, xhr) {
                ListDataCollection.Lists[this.indexValue].Data = data;
                ListDataCollection.Lists[this.indexValue].Completed = true;
                ifAllCompleted();
            },
            error: function (xhr, status, error) {
                console.log('error');
            }
        });     
    }
});
</script>
 <div id="CustomerPanel">
        <table id="tableEmployee" border="1 px" style="width: 100%;">
            <tbody>
                <tr>
                    <td>
                        <div id="employeeGrid" style="width: 100%;"><br /><br /><br /><br /><br /></div>
                    </td>
                </tr>
            </tbody>
        </table>
    </div> 

【问题讨论】:

    标签: sharepoint sharepoint-2010 sharepoint-2013 sharepoint-online sharepoint-designer


    【解决方案1】:

    您推送的是 result.push(item[propertyName]) 而不是项目。

    示例脚本:

    <script>
        var ListDataCollection = {
            Lists: [{
                Url: "url1",
                Completed: false
            }, {
                    Url: "url2",
                Completed: false
            }, {
                    Url: "url3",
                Completed: false
            }]
        };
        var tableContent = '<table id="tableEmployee" style="width:100%" border="1 px"><thead><tr><td><b>No</b></td>' +
    
            '<td><b>Description</b></td>' + '<td><b>Action By</b></td>' + '<td><b>Request From</b></td>' + '<td><b>Created</b></td>' +
    
            '</tr></thead><tbody>';
    
        function groupBy(items, propertyName) {
            var result = [];
            $.each(items, function (index, item) {
                if ($.inArray(item[propertyName], result) == -1) {
                    result.push(item[propertyName]);
                }
            });
            return result;
        }
        function checkDuplicateInObject(propertyName,value, inputArray) {
            var seenDuplicate = false;            
            inputArray.map(function (item) {
                var itemPropertyName = item[propertyName];
                if (itemPropertyName == value) {                    
                    seenDuplicate = true;
                    return seenDuplicate;
                }
            });
    
            return seenDuplicate;
        }
        function RemoveDuplicateItems(items, propertyName) {
            var result = [];
            $.each(items, function (index, item) {
                if (!checkDuplicateInObject(propertyName, item[propertyName], result)) {
                    result.push(item);
                }
            });
            return result;
        }
        function ifAllCompleted() {
            for (var i = 0; i < ListDataCollection.Lists.length; i++) {
    
                if (!ListDataCollection.Lists[i].Completed) {
                    return false;
                }
            }
            console.log('Completed');
            var arrayOfAllData = ListDataCollection.Lists[0].Data.d.results;
            arrayOfAllData = arrayOfAllData.concat(ListDataCollection.Lists[1].Data.d.results);
            arrayOfAllData = arrayOfAllData.concat(ListDataCollection.Lists[2].Data.d.results);
            console.log('Total elements : ' + arrayOfAllData.length);
            arrayOfAllData = RemoveDuplicateItems(arrayOfAllData, 'Description')
            for (var i = 0; i < arrayOfAllData.length; i++) {
                tableContent += '<tr>';
                tableContent += '<td>' + arrayOfAllData[i].Title + '</td>';
                tableContent += '<td>' + arrayOfAllData[i].Description + '</td>';
                tableContent += '<td>' + arrayOfAllData[i].Action_x0020_By_x003a_ + '</td>';
                tableContent += '<td>' + arrayOfAllData[i].RequestFrom_x003a_ + '</td>';
                tableContent += '<td>' + arrayOfAllData[i].Created + '</td>';
                tableContent += '</tr>';
            }
            $('#employeeGrid').append(tableContent);
    
    
        }
    
        $(document).ready(function () {
            for (var x = 0; x < ListDataCollection.Lists.length; x++) {
                $.ajax({
                    url: ListDataCollection.Lists[x].Url,
                    indexValue: x,
                    method: "GET",
                    headers: { "Accept": "application/json; odata=verbose" },
                    success: function (data, status, xhr) {
                        ListDataCollection.Lists[this.indexValue].Data = data;
                        ListDataCollection.Lists[this.indexValue].Completed = true;
                        ifAllCompleted();
                    },
                    error: function (xhr, status, error) {
                        console.log('error');
                    }
                });
            }
        });
    </script>
    <div id="CustomerPanel">
        <table id="tableEmployee" border="1 px" style="width: 100%;">
            <tbody>
                <tr>
                    <td>
                        <div id="employeeGrid" style="width: 100%;"><br /><br /><br /><br /><br /></div>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    

    【讨论】:

    • 感谢@Lee_MSFT 帮助我纠正错误。现在效果很好。我正在尝试根据“已创建”字段降序对检索到的结果集进行排序。我使用 JQuery 排序功能根据日期对数组进行排序,但无法成功。任何有关日期排序的输入都会有很大帮助。再次感谢。
    • 我使用 lodash _SortBy 函数根据 Created 对日期进行排序。
    猜你喜欢
    • 2013-12-03
    • 1970-01-01
    • 2017-10-09
    • 2014-04-29
    • 1970-01-01
    • 2016-03-12
    • 2019-07-01
    • 1970-01-01
    • 2021-12-18
    相关资源
    最近更新 更多