SAPUI5 DataBinding for sap.m.DatePicker - data-binding

How can I bind a DataSet to sap.m.DatePicker?
I would like to show a default Date in my DatePicker. The value is coming from a backend oDataService.
I am familiar with binding DataSets to ComboBox items etc, but on Input Fields or a Date Picker this is not working.
I also tried to attach a dataReceived event in the onInit function, but this event is never called.
Does anybody have a working example for me?

Here my exemple:
In the view.xml
<DatePicker id="startDateEvent" displayFormat="short"/>
In the controller.js :
var startDate = this.byId("startDateEvent");
startDate.setDateValue(<data date from backend>);

You need to be very careful with the Date and DateTime types. The snippet below works with the dateTime variables in Northwind like this one for example
<DatePicker value="{path:'yourPathToTheODataProperty', type: 'sap.ui.model.type.Date', formatOptions: { style: 'full'}}"></DatePicker>
You can change the format options for a predefined one as explained here or set your own LDML pattern, as described here

Related

How to correctly bind a form element to newly created oData Entity in SAPUI5?

I have implemented an oData Service into my SAPUI5 application.
On pressing a button the oData Model createEntry() is triggered which returns a Context with the newly created entity.
As per the SAPUI5 documentation - Documentation (Creating Entities) it should be enough to call setBindingContext(oContext) on my form to bind my new entity to my form.
But no matter how I try, I can't seem to get the binding working. The input fields remain empty (although the entity has set properties).
Do I need to keep a special syntax in mind when trying to do this kind of binding with an oData Model?
My form:
<f:SimpleForm id="form" editable="true" layout="ResponsiveGridLayout" title="Address" labelSpanXL="3" labelSpanL="3" labelSpanM="3"
labelSpanS="12" adjustLabelSpan="false" emptySpanXL="4" emptySpanL="4" emptySpanM="4" emptySpanS="0" columnsXL="1" columnsL="1" columnsM="1"
singleContainerFullSize="false">
<f:content>
<Label text="Recno"/>
<sf:SmartField id="recno" value="{recno}"/>
</f:content>
</f:SimpleForm>
My method that opens my dialog containing the form:
openCreateDialog: function() {
this.getOwnerComponent().getModel("oDataModel").refreshSecurityToken(function() {
var oContext = this.getOwnerComponent().getModel("oDataModel").createEntry("/head", {
properties: {
recno: "100"
}
});
this.byId("form").setBindingContext(oContext);
}.bind(this), function(data) {
console.log(data);
}, true);
this.byId("CreateHeadDialog").open();
},
EDIT
I already tried {oDataModel>xxx} but this doesnt work.
On the other hand, after I made my oDataModel a nameless model it seems to work fine. The smartfield correctly shows the right value, but changes I make in the UI are not applied to the property my smartfield is bound to.
When I submit pending changes of my oDataModel, all properties of my entity remain undefined, although I filled the smartfields with correct values.
You are using a named model: your model name is "oDataModel" evidenced by this in your code:
.getModel("oDataModel")
When using binding syntax in your XML View you need to prefix the field with the named model name as below:
<sf:SmartField id="recno" value="{oDataModel>(your-path-to-object)recno}"/>
If it's not in a list then you would need to reference the field as below (or navigate according to the position of recno in your json hierarchy:
<sf:SmartField id="recno" value="{oDataModel>/recno}"/>

Getting value of checkbox from dialog

I am fairly new to Day Cq5.I am having a checkbox in my dialog and i want to retrieve its boolean value in my jsp when user selects or deselects it .Kindly help
This works for me
Properties for dialog field:
name: ./checkbox1
type: checkbox
xtype: selection
code:
boolean isChecked1 = properties.get("checkbox1", false);
If you want to retrieve the value from your component's JSP, then do this:
boolean foobar = properties.get("nameOfYourCheckbox", true);
You can specify the default value by using true/false as the second argument.
Hope that helps.
Every widget you add to your components dialog is stored in CRX as a property to the cq:Component node. All this properties can be accessed in the jsp by typing properties. in EL brackets (like this: ${properties.<name_of_property>}). Don't forget to include the /libs/foundation/global.jsp file to your jsp.

Range validation

Suppose I have a table like:
create table
{
id numeric(5,3),
code varchar(10)
}
I have two text boxes in my form for the two fields.
Suppose if I type 1234578 in the first text box the error has been thrown in ASP.NET because I crossed the limit.
How can I validate in JavaScript or some other way for that particular range validation?
Let's take one textbox only. Attach an 'onchange' event handler to your textbox like this:
<input type="text" onchange="handleChange(this);" />
Then declare a script for validation like this:
<script>
function handleChange(input) {
if (input.value > ..your_value_here..) alert ("Invalid input");
}
</script>
Please note that the alert pop-up used here should not be actually used. Use a more subtle reminder at a more appropriate moment. The alert here is only to make things simple.

Databinding in AS3 to a slider

I am creating a datagrid in AS3, and want to pass the value of a slider to an item renderer
var column : MyDataGridColumn = new MyDataGridColumn();
column.sliderValue = mySlider.value;
...
This works fine initially, but when I move the slider, I would like the datagrid to assign the new value to the column.
This worked fine when I created the datagrid in MXML and bound the slider value like this
<local:MyDataGridColumn
sliderValue="{mySlider.value}"/>
This gets re-evaluated and re-assigned everytime the slider changes. The AS3 version not.
I tried it with a bindable function, and assign the function result to the column, but it did not work. Any ideas?
Thx,
Martin
You will need to set a change event handler on the slider and manually update the column.sliderValue when the change event is dispatched. Something like:
mySlider.addEventListener(SliderEvent.CHANGE, function(event:SliderEvent):void {
column.sliderValue = mySlider.value;
});

Flex ComboBox, default value and dataproviders

I have a Flex ComboBox that gets populated by a dataprovider all is well...
I would now like to add a default " -- select a item --" option at the 0 index, how can I do this and still use a dataprovider? I have not seen any examples of such, but I can't imagine this being hard...
If you don't need the default item to be selectable you can use the prompt property of ComboBox and set the selectedIndex to -1. That will show the string you set propmt to as the selected value until the user chooses another. It will not appear in the list of options, however.
I came across this problem today and wanted to share my solution.
I have a ComboBox that has an ArrayCollection containing Objects as it's dataprovider. When the application runs, it uses a RemoteObject to go out and get the ArrayCollection/Objects. In my event handler for that call I just have it append another object to the beginning of the ArrayCollection and select it:
var defaultOption:Object = {MyLabelField: "Select One"};
myDataProvider.addItemAt(defaultOption, 0);
myComboBox.selectedIndex = 0;
This is what my ComboBox looks like for reference:
<mx:ComboBox id="myComboBox" dataProvider="{myDataProvider}" labelField="MyLabelField" />
The way I've dealt with this in the past is to create a new collection to serve as the data provider for the combobox, and then I listen for changes to the original source (using an mx.BindingUtils.ChangeWatcher). When I get such a notification, I recreate my custom data provider.
I wish I knew a better way to approach this; I'll monitor this question just in case.
This can be used following code for selected default value of combobox
var index:String = "foo";
for(var objIndex:int = 0; objIndex < comboBox.dataProvider.length; objIndex++) {
if(comboBox.dataProvider[objIndex].label == index)
{
comboBox.selectedIndex = objIndex;
break;
}
}
<mx:ComboBox id="comboBox" dataProvider="{_pageIndexArray}" />

Resources