首页 > 解决方案 > 对数组中的重复项进行排序和删除 - REST API SharePoint 列表

问题描述

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

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

我使用了下面的代码和 RemoveDuplicatesItems在此处输入图像描述功能。代码执行得很好并删除了重复项,但是一旦我删除了重复项,网格就会将所有列显示为未定义。如果我不执行重复功能,网格显示数据,但有重复。

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

<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> 

标签: sharepointsharepoint-2010sharepoint-2013sharepoint-onlinesharepoint-designer

解决方案


你推的是一个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>

推荐阅读