Let's follow up this discussion at the Neptune Community

(this forum will be kept in read-only mode)

Tables: Adding New Rows

Hi there,


I have created a table which i would like to have the functionality of adding and deleting rows. I have managed to build in logic to delete selected rows:


var sItems = oTable.getSelectedItems();

var model = oTable.getModel();

var data = model.getData();


if (sItems.length === 0) {


    // Use MessageToast

    jQuery.sap.require("sap.m.MessageToast");

    sap.m.MessageToast.show("Please select row to delete");


    return;

} else {

    // for ( var i = 0; i < sItems.length; i++) {

    for (var i = sItems.length - 1; i >= 0; i--) {


        path = sItems[i].getBindingContext().getPath();


        var idx = parseInt(path.substring(path.lastIndexOf('/') + 1));


        data.splice(idx, 1);

    }

    model.setData(data);

}

oTable.removeSelections();


Is there a way/method to add records to my table from the App?


Thank you.


Hi Shiren


My suggestion is using ModelData object so you can more easy Update/Add/Delete records from the models. All objects with binded data will have the modelobject like model<objectName>.


   

// Delete Selected Rows
// Use your Key Field instead of CONNID
var oSelectedItems = oTable.getSelectedItems();
$.each(oSelectedItems, function(i, data) {
    var context = data.getBindingContext();
    var value = context.getProperty("CONNID");
    ModelData.Delete(oTable, "CONNID", value);
});

// Add New Row
var newRec = {};
newRec.CONNID = '0020';
ModelData.Add(oTable, "CONNID", newRec);

   


Regards

Ole Andre 

Thank you Ole-André,


When updating fields in a table, does it follow a similar principal?


var newRec = {};

newRec.VAT = val;


ModelData.UpdateField(oTable, "VAT", newRec);


Thank you for your assistance.

Hi Shireen


When updating single fields in a table you need to use the following syntax - and only updating for existing records in the table, not adding new record.


  

// obj = FieldName of you object with binded model (in the designer)
// keyField = FieldName of your key in the model
// keyValue = FieldValue of your key in the model
// updField = FieldName of your update field in the model
// updValue = FieldValue of your update field in the model

// Add Data
ModelData.Update(obj,"keyField","keyValue","updField","updValue");

// Store to Cache if needed
// Use Neptune function setCache<objectName>
setCacheobj();

 

This help is from our Javascript Helpers. In the javascript editor, rightclick and SAPUI5->Offline and select it.


Regards

Ole Andre 



Login or Signup to post a comment