Skip to Content

Get access to field in uxap-fragment from controller

Hi! I have view that contains fragment in uxap block. Fragment does not have id. This fragment contains few inputs (DatePickers). I want to get access from one input to another when onChange event is fired.

Part of view

<mvc:View
        controllerName="name_of_controller"
        xmlns="sap.m"
        xmlns:core="sap.ui.core"
        xmlns:mvc="sap.ui.core.mvc"
        xmlns:uxap="sap.uxap"
        xmlns:ino="sap.ino.controls"
        afterRendering="onAfterRendering"
        xmlns:app="http://schemas.sap.com/sapui5/extension/sap.ui.core.CustomData/1">
    <Page id="some_id"
    	...
		<uxap:ObjectPageLayout 
			id="objectpage" 
			useIconTabBar="true"
			enableLazyLoading="true"
			visible="{view>/objectExists}">
			<uxap:headerTitle>
				...
			</uxap:headerTitle>
			<uxap:sections>
				<uxap:ObjectPageSection id="sectionDetails" title="{i18n>MENU_MIT_DETAILS}">
					<uxap:subSections>
						<uxap:ObjectPageSubSection id="mainsubsection" title="{i18n>MENU_MIT_DETAILS}">
							<uxap:blocks>
								<core:Fragment fragmentName="fragment_with_datepickers" type="XML" />
							</uxap:blocks>
						</uxap:ObjectPageSubSection>
					</uxap:subSections>
				</uxap:ObjectPageSection>

Inputs are put in fragment dynamically.

If I write next code then returns undefined

oFieldControl.attachChange(function() {
	var oDatePicker = oController.byId("id_of_needed_control"); // Returns undefined
});

Use of sap.ui.core.Fragment.byId() also helpless because the page is not drawn completely, the page is not drawn completely because I add id for <core:Fragment fragmentName="fragment_with_datepickers" type="XML" />

oFieldControl.attachChange(function() {
	var fragmentId = oController.getView().createId("fragment_id");
	var oDatePicker = sap.ui.core.Fragment.byId(fragmentId, "id_of_needed_control");
	// or
	// var oDatePicker = oController.byId(sap.ui.core.Fragment.byId(fragmentId, "id_of_needed_control"));
});
Add comment
10|10000 characters needed characters exceeded

  • Hi Denis Kitrish,

    How you are putting inputs in the fragment dynamically?

    BR,
    Mahesh

  • I answered in Answer chapter (sorry, didn`t see "comment" link).

  • Fragment

    <core:FragmentDefinition
    	xmlns="sap.m"
    	xmlns:core="sap.ui.core"
    	xmlns:mvc="sap.ui.core.mvc"
    	xmlns:f="sap.ui.layout.form"
    	xmlns:l="sap.ui.layout"
    	xmlns:u="sap.ui.unified"
    	xmlns:ino="sap.ino.controls"
    	xmlns:app="http://schemas.sap.com/sapui5/extension/sap.ui.core.CustomData/1"><l:Grid><f:SimpleForm editable="true"layout="{view>/Layouts/SimpleFormLayout/layout}"
                          labelSpanL="{view>/Layouts/SimpleFormLayout/labelSpanL}"
                          labelSpanM="{view>/Layouts/SimpleFormLayout/labelSpanM}"
                          emptySpanL="{view>/Layouts/SimpleFormLayout/emptySpanL}"
                          emptySpanM="{view>/Layouts/SimpleFormLayout/emptySpanM}">//.... many other tags
    			<Label text="Additional Form Fields" visible = "false" /><VBox id="additionalFormFields" width="100%"><layoutData><l:GridData span="XL12 L12 M12 S12"/></layoutData></VBox></f:SimpleForm>

    Fields are put in VBox-GridData.

    In controller it is called method that add fields in onRouteMatched.

  • Get RSS Feed

2 Answers

  • Best Answer
    Feb 21 at 02:59 PM

    Hi Denis Kitrish,

    No probs, you can delete the answer and put it in the comment as well.

    I was asking you for the code where you add the date buttons. But I guess i understand your issue.

    1. If you are adding the controls in the controller by some hardcoded id then you need to access them via below:

    sap.ui.getCore().byId("yourCntrlId");

    2. If you don't want to do that, then you need to use createId function to generate the id

    			this.getView().byId("page").addContent(new sap.m.Button({
    				id: this.createId("test1"),
    				press: this.custBtnPress.bind(this)
    			}));
    
    
    // Now the code will work
    this.getView().byId("yourCntrlId");

    BR,

    Mahesh

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 21 at 03:21 PM

    Mahesh Kumar Palavalli Thank you very much. sap.ui.getCore().byId is helpfull

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Denis Kitrish

      But that is not a better way, instead you can use the createId() to generate the id for you which will be better as the ID wlll be specific to your view.

      You can actually click on comment and reply there :p

      Please mark the answer as answered and close the question

      BR,

      Mahesh