Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use input field as custom entry in <md-radio-group>?

I'm trying to create a form using Angular Material which allows user to select number of items per page (loaded from DB, or whatever). I want to have most popular choices (e.g. 10, 20, All) always visible and accessible with a single mouse click, so no drop-down list or slider. However I also want the user to have the freedom to specify custom value through an input field (see image below).

I created following JSFiddle: http://jsfiddle.net/p55sx0zy/

However there is a problem. Input field cannot be focused with mouse, instead radio button is selected and I need to press TAB to focus the input field. When I do that and change the value radio button gets unselected. Then when I press this 4th radio button again the proper value is reported.

I'd like this input to be focusable with mouse and the radio button to remain selected while input is being modified. Is it possible to achieve such design at all with Angular Material?

Select list with custom input field

like image 990
Bartosz Moczulski Avatar asked Oct 22 '15 18:10

Bartosz Moczulski


People also ask

How do I know if my input radio is selected?

Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.

Which attribute can you use to preselect an item in a radio button list?

How do I preselect a radio button in HTML? The radio class is a simple wrapper around the <input type="radio"> HTML elements. You can check a radio button by default by adding the checked HTML attribute to the <input> element.

How do I group radio buttons in HTML?

Defining Radio Group in HTML We can define a group for radio buttons by giving each radio button the same name. As and when the user selects a particular option from this group, other options are deselected. Following is an example of radio buttons with different names within a form.


1 Answers

CSS is preventing the input from being interacted with.

md-radio-button .md-label, .md-switch-thumb .md-label {
    ...
    pointer-events: none;
}

You can just override the style:

HTML:

<md-radio-button ng-if="option.input" value="{{ctrl.perPageCustom}}" class="md-primary md-radio-interactive">
    <input ng-model="ctrl.perPageCustom" /> (Custom)
</md-radio-button>

CSS:

.md-radio-interactive input {
    pointer-events: all;
}

Modified fiddle: http://jsfiddle.net/langdonx/p55sx0zy/1/

You'll have to change you tack on how you're storing the custom value so the radio stays selected though.

like image 141
Langdon Avatar answered Nov 14 '22 22:11

Langdon