cancel
Showing results for 
Search instead for 
Did you mean: 

Can we get old date value from DataPicker on change event .??

nandishm
Participant
0 Kudos

Hi

I have used one Datepicker in the view, initially i have set some date value to Date picker.

On change of Date, change function() will trigger, in that function i need to compare old Date and new date. So is there any way to get old Date value from Datepicker.

Thanks and Regards

Nandish

Accepted Solutions (0)

Answers (1)

Answers (1)

santhu_gowdaz
Active Contributor
0 Kudos

Create 1 global variable and save your initial date in that.

through out your controller you will get that date so using that compare with your new date.

nandishm
Participant
0 Kudos

Hi Santosh

  

Every time i need to compare old date with new changed date, so is there any other way to get old values from event parameters .

santhu_gowdaz
Active Contributor
0 Kudos

same global variable, save the initial setting value to that global variable. so 1st time compare with that date. after comparing save current value in that global variable. so every time while entering into that method you are getting old value in that global variable. issue solved:)

nandishm
Participant
0 Kudos

Datepicker is in Table column, for first time data is coming from backend, we are binding data to the table. So through json model , while changing date method is triggering i Need old value since i have more than 10 records so I cant save date of every records  in a separate  varible.

Is there any way to fetch date value from method only.

Ex:-

For first let Date be 21/03/2014    I changed 25/03/2014

Next time I changed  25/03/2014  to 27/03/2014.

How I can get old values i.e 25/03/2014

santhu_gowdaz
Active Contributor
0 Kudos

store in global array variable. do as same as previous comment.

Qualiture
Active Contributor
0 Kudos

Please, please, please! Do not use global variables... Why are global variables considered bad practice? (Javascript) - Stack Overflow

Better use a temporary JSONModel which holds your initial values. It makes comparing also a LOT easier between models

santhu_gowdaz
Active Contributor
0 Kudos

i'm declaring a variable in global and store in a model. using that model reference i'm doing binding in my view. is this bad??

var headerModel = {};

sap.ui.controller("sfs", {

    onInit: function() {

var oModel = new sap.ui.model.json.JSONModel(headerModel);

        this.getView().setModel(oModel, "HeaderModel");

},

})

Qualiture
Active Contributor
0 Kudos

Yes, global variables are bad

I would not use global variables if you can avoid it.

In your case, why not have a model (or model node) with your initial values and a separate model (or model node) with the new/changed values?

Therefor you don't need any global variables, you can just copy the initial model property to the corresponding second model property.

You can even reset your screen's control to the initial state ('reset button' functionality) by copying the initial model to the second model if you want.

santhu_gowdaz
Active Contributor
0 Kudos

simple example,

like above in init() i'm initialize the model then,

in view.xml, i'm bind like below.

<Input id="AmountId" value="{HeaderModel>/Amount}" />

so what ever in this Amount input field that is available in my controller,

for example,

any method in controller,

var headerModel = {};

sap.ui.controller("sfs", {

    onInit: function() {

var oModel = new sap.ui.model.json.JSONModel(headerModel);

        this.getView().setModel(oModel, "HeaderModel");

},

onReview: function() {

alert(headerModel.Amount); //For validation purpose

},

onSave: function() {

alert(headerModel.Amount); //For Posting purpose

},

})

Like this through out the controller i can get. so any easy way is there to do like this?

Qualiture
Active Contributor
0 Kudos

Well, for starters, I would not load the model in the onInit since it will be loaded only once

No, what I mean is the following:

  1. Load your initial data in your view's model (OData or JSON)
  2. Copy this loaded data in a separate JSON model
  3. Since the model's data (and structure) are equal, you can simply compare / reset data based on their path.

Now, let's say you want have changed a values in a 'mydatevalue' field in a certain row in a table, and you want to compare it with the old value, you just simply compare the initial model's property to the table model:


doReview : function(oEvent) {

         var oModel        = this.getView().getModel("main");

         var oModelInitial = this.getView().getModel("initial");

         var sPath = oEvent.getSource().getBindingContext("main").getPath() + "/mydatevalue";

         sap.m.MessageBox.show(

                    "Old date: " + oModelInitial.getProperty(sPath) +

                    ", new date: " + oModel.getProperty(sPath)

         )

}

santhu_gowdaz
Active Contributor
0 Kudos

thank you somuch for clarifying my doubts.