Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a picture to each Radio Button in Radio Button Group

Tags:

sapui5

I want to implement a Radio Button Group where every Radio Button will have a picture next to it (on the left side of the radio button).

Is that possible? If so, how?

html:

<div class="form-group" show-errors>
    <label class="col-md-2 control-label metric-light-16-black pay-form-label">Payment Method</label>
    <div class="col-md-10" id="rdPaymentMethod">
        <label class="radio-group col-md-2">
            <span id="radio-group-1" style="margin-left: 0cm;"></span>
            <span class="radio" style="margin-left: 0cm;" />
            <img src="assets/images/card-mastercard.png" style="margin-left: 0cm;"/>

        </label>

        <label class="radio-group col-md-2">
            <span id="radio-group-2" style="margin-left: 0cm;"></span>
            <span class="radio"/>
            <img src="assets/images/card-viza.png"/>
        </label>

        <label class="radio-group col-md-2">
            <span id="radio-group-3" style="margin-left: 0cm;"></span>
            <span class="radio"/>
            <img src="assets/images/card-paypal.png"/>
        </label>
    </div>
</div>

javascript sap function:

var placeRadioButtonAt = function(radioButtonId, containingDivId, selected) {
    elements.push(radioButtonId);
    var oRB1 = new sap.ui.commons.RadioButton(radioButtonId, {
        selected : (selected==true),
        select : function() {}
    });

    oRB1.placeAt(containingDivId);
}

calling this function 3 time (for each radio button):

    placeRadioButtonAt("radio1","radio-group-1",true);
    placeRadioButtonAt("radio2","radio-group-2");
    placeRadioButtonAt("radio3","radio-group-3");

enter image description here

like image 580
Matoy Avatar asked Dec 12 '16 08:12

Matoy


People also ask

Is it possible to add an image on the radio button control?

To use a custom image in a radio button control or a checkbox control, ensure the Add display image check box is selected. If you right-click on the Display field, this will open a prompt allowing the user to select an image on either the Reporting Server or WebFOCUS Client.

How do I make an image slider work with radio buttons?

We will use radio buttons to select the image that we want to see from the image slider by giving an unique id to each radio button. Next, we will embed all the images one by one and create labels for radio button id's and will apply necessary CSS properties to achieve the desired output.

Can radio buttons have multiple selections?

Radio buttons allow a user to select a single option among multiple options. You can set the Choice Value of each option, for each button, as well as group these buttons by giving them the same Group Name.


1 Answers

I would extend the RadioButton control for allowing images to be added with each radio button, and also extend the RadioButtonGroup to allow adding the previous extended control as an aggregation.

OBS: css classes rendering are missing, more details on renderer implementation can be found here.

Assumptions:

  1. The namespace was defined as my.app inside the index.html file
  2. Inside your project folder (webapp or WebContents) you have created a folder for these custom controls, named controls

RadioButton extension

sap.ui.define(['jquery.sap.global', 'sap/m/Image', 'sap/m/RadioButton'

], function (jQuery, Image, RadioButton) {
    "use strict";

    var CustomRadioButton = RadioButton.extend("my.app.controls.RadioButtonImage", {
        metadata: {
            "properties": {
                "imageSrc": "string"
            },
        },
        renderer: function (oRm, oControl) {
            var oImg = new Image({ src: oControl.getProperty("imageSrc") })
            oRm.renderControl(oImg);
            sap.m.RadioButtonRenderer.render(oRm,oControl);
        }
    });

    return CustomRadioButton;
}, true);

RadioButtonGroup extension

sap.ui.define(['jquery.sap.global', 'sap/m/RadioButtonGroup'

], function (jQuery, RadioButtonGroup) {
    "use strict";

    var CustomRadioButtonGroup = RadioButtonGroup.extend("my.app.controls.RadioButtonGroup", {
        metadata: {
            aggregations: {
                buttons : {type : "my.app.controls.RadioButtonImage", multiple : true, singularName : "button", bindable : "bindable"}
            },
            defaultAggregation : "buttons",
        }
    });

    return CustomRadioButtonGroup;
}, true);
like image 166
Cassio Avatar answered Oct 01 '22 21:10

Cassio