There are no secrets to success. It is the result of preparation, hard work, and learning from failure.

Friday, 13 May 2016

View Binding of list with JSON

Click here and follow my SAP UI5/FIORI snippets and information Page
This is view List binding with json, below I have shown application structure,view part,controller part, json part and index part of the application.Go through it and please let me know in case of any issue/doubt.

Application Structure


    View Part

<core:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m"
controllerName="view.s1" xmlns:html="http://www.w3.org/1999/xhtml">
<Page title="List with Json" id="pageid">
<content>
<List items="{/Records}"  mode="Delete" delete="handleDelete">
<StandardListItem
title="{fname}" counter="{mob}">
</StandardListItem>
</List>
</content>
</Page>
</core:View>

Controller Part

sap.ui.controller("view.s1", {
onInit:function()
{ var json=new sap.ui.model.json.JSONModel("model/data.json");
this.getView().setModel(json);
},
// When you click on delete Icon then this will fire....
handleDelete:function(EvT)
{ var list_ID=sap.ui.getCore().byId(EvT.getSource().sId);
  var Data=list_ID.getModel();
    var current_Item=EvT.getParameter("listItem");
//First Way- It will delete item from UI, not from model.....
  current_Item .destroy();
                                          OR
  // Second way
               var Index=list_ID.indexOfItem(current_Item);
          Data.getData().Records.splice(Index,1);
          Data.updateBindings();
                                                 OR
                   // Third way
          var path=current_Item.getBindingContext().sPath; 
          var spltarr=path.split("/");
         var InDeX=spltarr[spltarr.length-1];
         Data.getData().Records.splice(InDeX,1);
          Data.updateBindings();}
});

data.json

{ "Records":[{ 
"fname":"vikas",
"mob":1111111111
},
"fname":"raj",
"mob":2222222222
},
"fname":"Rahul",
"mob":3333333333
},
"fname":"Nikhil",
"mob":4444444444
}
]}

index.html

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/>
<script src="resources/sap-ui-core.js"
id="sap-ui-bootstrap"
data-sap-ui-libs="sap.m"
data-sap-ui-theme="sap_bluecrystal">
</script>
<!-- only load the mobile lib "sap.m" and the "sap_bluecrystal" theme -->
<script>
sap.ui.localResources("view");
var app = new sap.m.App({initialPage:"idview1"});
var page = sap.ui.view({id:"idview1", viewName:"view.s1",type:sap.ui.core.mvc.ViewType.XML});
app.addPage(page);
app.placeAt("content");
</script>
       </head>
<body class="sapUiBody" role="application">
<div id="content"></div>
</body>
</html>

Result



No comments:

Post a Comment