根据输入搜索SAPUI5的值检查表行

时间:2016-09-25 16:52:09

标签: sapui5

我有一个带有搜索字段的表格(多选模式)。有没有办法,当我搜索时,搜索结果的值将自动检查表中的相应行。最初我虽然我可以只搜索,然后如果结果长度是1,在表上执行getItems()并在第一行执行setSelected = true。该行被选中,但是当我退出搜索时,该行被取消选择。

        var oSerialTable = new sap.m.Table({
            mode: sap.m.ListMode.MultiSelect,
            columns: [
                ...
            ],
            items : {
                    path : "/results",
                    template: new sap.m.ColumnListItem({
                        cells: [
                        new sap.m.Text({ text: "{Sernr}" }),
                        new sap.m.Text({ text: "{Equnr}" }),
                        new sap.m.Text({ text: "{Lbbsa}" })
                        ]
                      })
            },
            select : function(evt){

            },
            updateFinished: function(){
                var aItems = oTable.getItems(); 
                console.log(aItems);
                if (aItems.length == 1){
                console.log(aItems[0]);
                aItems[0].setSelected(true)
                }

            }
        });

        oSerialTable.setModel(ocheckSerialBatchJsonModel);


        var oSerialTableSearch = new sap.m.SearchField({

            search: function(oEvent){

            var filterValue = oEvent.getSource().getValue();
            var aFilter = new sap.ui.model.Filter("Sernr", sap.ui.model.FilterOperator.EQ, filterValue);
            var oItemTemplate = new sap.m.ColumnListItem({
                cells: [
                    new sap.m.Text({ text: "{Sernr}" }),
                        new sap.m.Text({ text: "{Equnr}" }),
                        new sap.m.Text({ text: "{Lbbsa}" })


                ]
            });

                oSerialTableSearch.bindItems({path:"/SerialSet", template:oItemTemplate, filters: [aFilter]});


            }

        });

1 个答案:

答案 0 :(得分:0)

如果将ColumnListItem的{​​{3}}绑定到模型,这会变得更容易。 然后,您可以对模型数据执行过滤和选择:

    ...      
                 template: new sap.m.ColumnListItem({
                    cells: [
                    new sap.m.Text({ text: "{Sernr}" }),
                    new sap.m.Text({ text: "{Equnr}" }),
                    new sap.m.Text({ text: "{Lbbsa}" })
                    ],
                    selected: "{selected}" //Bind selection to model
                  })

    ...

    var oSerialTableSearch = new sap.m.SearchField({
       search: function(oEvent){
           var filterValue = oEvent.getSource().getValue();
           var array = ocheckSerialBatchJsonModel.getProperty("/results");
           array.forEach(function(item){
              //update selected state of each item
              item.selected = (item.Sernr == filterValue || item.Equnr == filterValue || item. Lbbsa === filterValue);
           });
           ocheckSerialBatchJsonModel.setProperty("/results",array); //Write back to Model to update bindings
        }
    });

您在selected property上的代码示例。