Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript UI5删除重复的提要列表项_Javascript_Xml_Sapui5 - Fatal编程技术网

Javascript UI5删除重复的提要列表项

Javascript UI5删除重复的提要列表项,javascript,xml,sapui5,Javascript,Xml,Sapui5,我有一个提要列表,用于在UI5XML视图中发布评论 <layout:content> <m:FeedInput post="onFeedPost" class="sapUiSmallMarginTopBottom"/> <m:List id="feedList" showSeparators="Inner" items="{path: '/table', sorter: {path: 'DATE', descending: true}}">

我有一个提要列表,用于在UI5XML视图中发布评论

<layout:content>
    <m:FeedInput post="onFeedPost" class="sapUiSmallMarginTopBottom"/>
    <m:List id="feedList" showSeparators="Inner" items="{path: '/table', sorter: {path: 'DATE', descending: true}}">
        <m:FeedListItem sender="{MEMBERID}" timestamp="{DATE}" text="{COMMENT}" convertLinksToAnchorTags="All"/>
    </m:List>
</layout:content>

我不想显示具有相同文本和日期的重复注释,而是将它们保留在数据库中。我的想法是在控制器中对项进行迭代以完成此操作,但我不确定如何处理生成的数组

var results = [];
var comments = feed.getItems();

for (var n = 0; n < comments.length - 1; n++) {
    var contained = false; 

    for (var m = n + 1; m < comments.length; m++) {
        if (comments[n].getText() === comments[m].getText() && 
            comments[n].getDate() === comments[m].getDate()) {
            comments.pop(m);

            contained = true;

            if (!results.includes(comments[n])) {
                results.push(comments[n]);
            }
        }
    }

    if (!contained && !results.includes(comments[n])) {
        results.push(comments[n]);
    }
}

// replace list items with results array
var结果=[];
var comments=feed.getItems();
对于(var n=0;n

我不知道如何用新数组替换提要列表的项,因为有一个getItems函数,但没有一个setItems函数。我突然想到,可能有一种更简单、更惯用的UI5方法可以做到这一点,但我还没有找到它。

这里有一种方法:

不要直接将列表绑定到oData。 您可以创建一个JSON模型,该模型将在删除重复项后作为结果模型

将JSON模型绑定到列表,如下所示:

var oList=this.getView().byId(“feedList”);
bindAggregation(“items”,“pathToJsonArray”,模板)


(本例中的模板是feedlistitem)。

首先,处理这种情况的正确方法是使用OData服务。服务应在将数据发送到客户端之前删除重复项。但是,如果我们假设您不能在服务器端执行此操作,那么您有一些选择

1.)不要将列表项绑定到任何内容。相反,使用ODataModel读取数据,然后过滤掉重复项,创建一个新的列表项并将其添加到列表中

oModel.read("/EntitySet", {
    success: function(oResponse) {
        this._addCommentsToList(oResponse.results)
    }.bind(this)
})
var aDistinctComments = //use your logic to filter out duplicates
aDistinctComments.forEach(function(oComment) {
    //to set the binding context, you'll need the entity key/path
    var sCommentKey = oModel.createKey("/EntitySet", oComment)
    //create a new binding context
    var oContext = oModel.createBindingContext(sCommentKey)
    //create a new FeedListItem
    var oItem = new FeedListItem({
        sender: "{MemberId}",
        ...
    });
    //set the context of the item and add it to the list
    oItem.setBindingContext(oContext);
    oList.addItem(oItem);
})
使用ODataModel读取数据,然后将结果传递给一个方法,该方法将过滤并将它们添加到列表中

oModel.read("/EntitySet", {
    success: function(oResponse) {
        this._addCommentsToList(oResponse.results)
    }.bind(this)
})
var aDistinctComments = //use your logic to filter out duplicates
aDistinctComments.forEach(function(oComment) {
    //to set the binding context, you'll need the entity key/path
    var sCommentKey = oModel.createKey("/EntitySet", oComment)
    //create a new binding context
    var oContext = oModel.createBindingContext(sCommentKey)
    //create a new FeedListItem
    var oItem = new FeedListItem({
        sender: "{MemberId}",
        ...
    });
    //set the context of the item and add it to the list
    oItem.setBindingContext(oContext);
    oList.addItem(oItem);
})
在处理结果的方法中,需要做三件事——创建一个新的FeedListItem,设置列表项的绑定上下文,然后将列表项添加到列表中

oModel.read("/EntitySet", {
    success: function(oResponse) {
        this._addCommentsToList(oResponse.results)
    }.bind(this)
})
var aDistinctComments = //use your logic to filter out duplicates
aDistinctComments.forEach(function(oComment) {
    //to set the binding context, you'll need the entity key/path
    var sCommentKey = oModel.createKey("/EntitySet", oComment)
    //create a new binding context
    var oContext = oModel.createBindingContext(sCommentKey)
    //create a new FeedListItem
    var oItem = new FeedListItem({
        sender: "{MemberId}",
        ...
    });
    //set the context of the item and add it to the list
    oItem.setBindingContext(oContext);
    oList.addItem(oItem);
})
2.)将列表直接绑定到OData实体集,然后当列表接收到数据时,迭代项目并隐藏重复项

<List items="{/EntitySet}" updateFinished="onListUpdateFinished"....>

----- onListUpdateFinished ---
var aItems = oList.getItems();
for (var m = n + 1; m < aItems.length; m++) {
    //set a boolean, true if duplicate
    var bDuplicate = aItems[m].getText() ==== aItems[n].getText() &&
                     aItems[m].getDate() === aItems[n].getDate();
    //set the visibility of the item to true if it is not a duplicate
    aItems[m].setVisible(!bDuplicate)
}
您可以在JSON模型中隐藏一个对象数组,然后将表项绑定到该路径

 var aDistinctComments = // your logic to get distinct comments
 oJSONModel.setProperty("/comments", aDistinctComments)
 oList.setModel(oJSONModel);

 -----

 <List items="{/comments"}....>

我建议首先在OData服务中处理这个服务器端,如果这不是一个选项,那么使用上面的选项1。这将为您提供所需的结果,并维护列表项的绑定上下文。选项2和3将获得所需的结果,但根据您的应用情况,可能会使使用列表更加困难。

对模型数据执行上述操作。Do,
comments=model.getProperty('/table')
然后应用过滤器逻辑,最后将结果存储回数组:
model.setProperty('/table',results)
。这里,我假设模型是JSON模型。@RahulBhardwaj'comments=model.getProperty('/table');'for my table返回undefined,即使它显示在绑定中。我在后端使用odata服务,但创建json来执行请求(创建、删除)。不确定这是否使其成为json模型。如何获取提要列表项模板以及pathToJsonArray应该是什么?能否澄清pathToJsonArray应该如何指向我拥有的数组?这必须是数组的路径。根据上面的示例代码(XML),我可以说确切的语法应该是:oList.bindAggregation(“items”,“/table”,new sap.ui.core.FeedListItem({text:{COMMENT}”);提供的“COMMENT”是表数组的属性。我不明白。似乎在您尚未添加的步骤之间缺少了一些步骤。我有一个list.getItems()中的数组。据我所知,“/table”不会指向该数组。请填写缺少的部分。