简体   繁体   English

sapui5 sap.ui.table如何以编程方式取消选中表行的复选框

[英]sapui5 sap.ui.table How to uncheck the checkbox of table row programatically

Im facing a small issue in sap.ui.table . 我在sap.ui.table中面临一个小问题。 I want to uncheck the checkbox of the table if i delete the record. 如果要删除记录,我想取消选中表的复选框。 Here im able to delete the record, But the selection of checkbox was not getting cleared and it is appending for the below row. 在这里我无法删除记录,但是未清除复选框的选择,而是将其追加到下面的行中。 在此处输入图片说明

In the above image if you see, if i delete "Rani Test" , Rani Test row will get deleted but checkbox will get selected to "Surya Test" . 在上图中,如果您看到的话,如果我删除“ Rani Test” ,Rani Test行将被删除,但“ Surya Test”复选框将被选中。 Please check the below code.. 请检查以下代码。

onPressDelete: function(evt) {
            var deleteRecord = evt.getSource().getBindingContext('familyDetailsModel').getObject();
            var tableId = this.getView().byId("familyDetailsModel");
            var index = tableId.getSelectedIndex();          
            //this.getView().byId("familyDetailsModel").removeSelectionInterval(index,1);

                for (var i = 0; i < this.getView().getModel('familyDetailsModel').getData().length; i++) {
                    if (this.getView().getModel('familyDetailsModel').getData()[i] == deleteRecord) {
                        this.getView().byId("familyDetailsModel").removeSelectionInterval(i, i);
                        this.getView().getModel('familyDetailsModel').getData().splice(i, 1);
                        this.getView().getModel('familyDetailsModel').refresh();
                        break;
                    }


            }
        },

In the above code, splice() method im using to delete the row which is working perfectly. 在上面的代码中, splice()方法用于删除运行良好的行。 But checkbox is not getting deselected. 但是不会取消选中该复选框。 To uncheck the checkbox im trying with removeSelectionInterval() method. 要取消选中复选框,尝试使用removeSelectionInterval()方法。 But it is not behaving as expected. 但是,它的行为不符合预期。

Can someone please help me to sort this issue 有人可以帮我解决这个问题吗

Thanks in advance 提前致谢

This line var index = tableId.getSelectedIndex(); 这行var index = tableId.getSelectedIndex(); returns -1 in your scenario. 在您的方案中返回-1。 Furthermore, to delete one line you need to specify removeSelectionInterval(index,index); 此外,要删除一行,您需要指定removeSelectionInterval(index,index); The second parameter is not the number of positions to delete. 第二个参数不是要删除的位置数。 Is the indexTo , so you want to remove from the selected row, to the selected row. indexTo ,因此您要从选定的行中删除到选定的行。

Getting the row index from the event will work better for you. 从事件中获取行索引将更适合您。 Try this: 尝试这个:

var iIndex = oEvent.getSource().getParent().getIndex();
var oTable = this.getView().byId("__table0");
oTable.removeSelectionInterval(iIndex, iIndex);

Here the snippet: https://plnkr.co/edit/wkMc4LcjYYS3K73ClYUc?p=preview 这里是代码段: https : //plnkr.co/edit/wkMc4LcjYYS3K73ClYUc? p =preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM