Skip to Content
0

SAPUI5 Delete Multiple Rows (sap.m.table)

Jan 24 at 06:20 PM

265

avatar image

Dear All,

I am working on SAPUI5 Table, I have created a functionality to delete selected rows from the table, but when I select to delete more then one rows, it is deleting wrong rows, although first selected row is deleted correctly, nut others are not.
Please review my code and advise,

fDeleteRow: function(oEvent) {
                    // Here Calling the table from view
		var oTable = this.getView().byId("tbl");
                    // Getting the table Model
		var data = oTable.getModel();
                  // getting selected Row COunt,
	var selRowCount = oTable._oSelection.aSelectedIndices.length;
		var Flag = false;
		for (var i = 0; i < selRowCount; i++) {
		var rowNum = oTable._oSelection.aSelectedIndices[i];
		data.oData.splice(rowNum, 1);
		data.oData.refresh();
		Flag = true;
			}
			if (Flag) {
			var oModelJson = new sap.ui.model.json.JSONModel();
			oModelJson.setData(data.oData);
			oTable.unbindColumns();
			oTable.unbindRows();
			oTable.setModel(oModelJson);
			oTable.bindRows("/");
			
			}


		}

Thanks,

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Jun Wu Jan 24 at 06:38 PM
0

make sure the selected index are sorted, then delete from big one

example

1,3,5 are selected index, then you delete in this sequence 5,3,1

Show 3 Share
10 |10000 characters needed characters left characters exceeded

Dear Mr Jun,
Thanks for your response, I replaced my code after your advise, but issue is still the same,

	fDeleteRow: function(oEvent) {
			 // Here Calling the table from view
		var oTable = this.getView().byId("tbl");
                    // Getting the table Model
		var data = oTable.getModel();
                // data.oData.sort();
                  // getting selected Row COunt,
	               var selRowCount = oTable._oSelection.aSelectedIndices.length;
			var Flag = false;
			var i = 0;
			for (i = selRowCount; i >= 0; i--) {
				var rowNum = oTable._oSelection.aSelectedIndices[i];
				data.oData.sort();
				data.oData.splice(rowNum, 1);
				Flag = true;
				// }
			}
			if (Flag) {
				var oModelJson = new sap.ui.model.json.JSONModel();
				oModelJson.setData(data.oData);
				oTable.unbindColumns();
				oTable.unbindRows();
				oTable.setModel(oModelJson);
				oTable.bindRows("/");
			}


		},

0
Hamdan Khalid

I mean the selected index (aSelectedIndices) should be sorted


1

It worked.

Thanks.

0