Control group loses control after dynamic add of radio button - jQuery Mobile - jquery-mobile

My control group (jquery mobile 1.4) renders perfectly when dynamically creating the radio group initially, but when I add another radio to it, the buttons are styled, but separated. When I reload the app, they are together again.
function showPlaces() {
$('#radio-group').empty();
for(var i = 0; i < connections.length; i++) {
$('#radio-group').append('<label><input type="radio" name="places" id="' + i + '" />' + place + '</label>');
}
$('input[type=radio]').checkboxradio().trigger('create');
$('#radio-group').controlgroup().trigger('create');
}
// The function is called when app starts, and again after I add another place
Some other things I've tried:
$("#radio-group").controlgroup("refresh");
$('#radio-group').controlgroup('refresh');
$("[data-role=controlgroup]").controlgroup("refresh");
$('input[type=radio]').checkboxradio('refresh');
Also tried .controlgroup('container'), before the .append, but get "cannot call methods on controlgroup prior to initialization".
Here's the html:
<form>
<fieldset data-role="controlgroup" id="radio-group">
<!-- Dynamically injected radio buttons go here -->
</fieldset>
</form>

You should .append() items into .controlgroup("container") inside for loop, and then enhance radio buttons .checkboxradio().
for(var i = 0; i < connections.length; i++) {
$('#radio-group')
.controlgroup("container")
.append('<label><input type="radio" name="places" id="+i+" />Place</label>');
}
$("#radio-group")
.enhanceWithin()
.controlgroup("refresh");
You dont even need to use .checkboxradio(), instead use .enhanceWithin() on parent container.
Demo

Related

How to display progress percentage in UI in Ruby on Rails?

I have a button in my Rails application. When it is clicked, around 5 stored procedures run (sequentially) in the background. It takes around 60 seconds for all of them to complete.
Until then, I would like to display some kind of a progress bar (or a spinner that shows percentage of progress) in the UI so that the user does not get a feeling that the application just hangs.
So, if 1 out of 5 stored procedures are complete, then we could should show the progress as 20%; if 2 stored procedures are complete, then show the progress as 40% and so on.
What is the best way to do this?
(Ajax spinners don't seem to have a provision to display % progress)
you can use bootstrap progress bar and update the values from js or jquery. Here are the examples.
Js :
HTML :
var i = 0;
fill_bar();
function fill_bar(){
setInterval(function() {
var art = i;
doSomethingWith(art)
if(i >= 5) {return false;}
}, 5000);
}
valuer = 0;
function doSomethingWith(art){
valuer = valuer + 20;
$('.progress-bar').css('width', valuer+'%').attr('aria-valuenow', valuer);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="progress progress-striped active">
<div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
You can call the method
fill_bar()
from controller or after clicking the button.

Hide the iOS soft keyboard AngularJS

I need to close soft keyboard on iOS simply clicking outside of input field in Angular app.
I found part of solution:
$scope.hideKeyboard = function() {
document.activeElement.blur();
var inputs = document.querySelectorAll('input');
for(var i=0; i < inputs.length; i++) {
inputs[i].blur();
}
};
<input type='text' ... ng-blur='hideKeyboard()' />
but ng-blur does not help me.
something like this doesn't work.
Found easy solution. On top level div (root of project) I added:
ng-click='angular.noop()'
no need for code above, or anything else.

jQuerymobile slider touch lags

I'm using a slider to change the min/max for chart I'm displaying with flot.
This is my slider HTML:
<div id=timerangeslider>
<form>
<div class="ui-grid-a" id="slider3">
<label for="slider-2">Anzeige-Zeitraum (in Stunden):</label>
<input type="range" name="slider-2" id="slider-2" data-highlight="true" min="1" max="48" value="24">
</div><!-- /ui-grid -->
</form></div>
And this is my jQuery:
function timerangeslider() {
$(document).ready(function(){
$("#slider-2").change("click",function() {
timerange = $("#slider-2").val();
energyPlot(d,timerange)
});
});
}
The function gets called on "pageinit".
It calls another function "energyPlot" which updates my chart (using flot)
Problem with this solution is, that is re-draws my chart on every change of the value of "#slider-2"
So this works, but looks like crap.
Another solution would be this:
function timerangeslider() {
$(document).ready(function(){
$(document).on("click","#timerangeslider",function(event) {
timerange = $("#slider-2").val();
energyPlot(d,timerange)
});
});
}
Problem here is, that on my device (with Phonegap) it does not recognize touches on the actual slider. You can click the number left to the slider and it get's the touch, but not if you drag the slider.
It works in browser though.
Any ideas how to react to touches on the actual slider (on device with phonegap)
Or any ideas for the first solution on how to re-draw the chart only when the slider stopped?
Just found the solution here jQueryMobile slider change event
It seems there is an event for a slider called "slidestop" and you can listen for it.
So I used this:
function timerangeslider() {
$("#slider-2").on("slidestop",function(event) {
timerange = $("#slider-2").val();
energyPlot(d,timerange);
});
}

Change text (label) with jquery ui button toggle

I have a jquery-ui button and I know how to change the styling between states with css, but I would like to know how to change the label text too. For example, an ON/OFF button like below -- when [checked="checked"] then the label text would = "ON" (and OFF when not checked).
<input type="checkbox" id="device_4_state" name="device_4_state" class="toggle-button on-off" checked="checked" />
<label for="device_4_state">ON</label>
Any help would be tremendous. Thanks.
here is the solution :
$('#device_4_state').change(function(){
if ($("#device_4_state").is(':checked'))
{
$("label[for='device_4_state']").text("ON");
}
else
{
$("label[for='device_4_state']").text("OFF");
}
});
If you wish to use pure CSS to set the label text, you can use CSS pseudo elements to automatically generate the content of the label based on the state of the jquery-ui button. To use this approach, leave the label empty (<label for="device_4_state"></label>) and include CSS like this:
.on-off + label > span:before {
content: "OFF";
}
.on-off + label.ui-state-active > span:before {
content: "ON";
}
As in this jsfiddle: http://jsfiddle.net/GjPXZ/1/

Get scroll position of panel using SplitView (JQuery Mobile)

I have implemented a WebApp using SplitView - http://asyraf9.github.com/jquery-mobile/ - (and that seems to use the ScrollView component) together with jQuery Mobile. Everything works fine ...
Within the panel I have got a list of elements that should dynamically add elements when scrolling reaches the end of the list. On the iPhone I do not use SplitView but iScroll - http://cubiq.org/iscroll - and the following code to achieve this (and it is working).
HTML:
<div data-role="panel" data-id="menu" data-hash="crumbs" style="z-index: 10000;" id="Panel">
<div data-role="page" id="Root" class="Login" onscroll="console.log('onscroll');">
<div data-role="content" data-theme="d" onscroll="console.log('onscroll');">
<div class="sub">
<ul data-role="listview" data-theme="d" data-dividertheme="a" class="picListview" id="PortfolioList">
<!-- Content added dynamically -->
</ul>
</div>
</div>
</div>
</div>
Javascript:
var defaultIScrollOptions = {
useTransition: true,
onScrollStart: function() {
this.refresh();
},
onScrollEnd: function() {
if (this.elem && this.id) {
possiblyDisplayNextDocuments(this.y, this.elem, this.id);
}
}
};
iScrolls.push(new iScroll(document.getElementById("searchResults").parentNode, defaultIScrollOptions));
But when using SplitView I do not really know which event and which element to bind the listener on or how to get the scroll position. I already tried several combinations, but did not achieve good results. The best one was the following:
$("#PortfolioList").scrollstop(function(event) {
console.log("scrollstop: "+$("#PortfolioList").scrollTop());
});
My question is: Am I using the right event listener (since it already fires althgough the scrolling animation is still in use) and how do I get the scroll position?
dont use the scrollview plugin. its buggy. Use iscroll for both iOS phonegap apps as well as android. It works fine on both.
For detecting the scroll and loading new elements into the list, listen to the the 'onScrollMove' event of iscroll.
In the iscroll-wrapper.js add this-
options.onScrollMove = function(){
that.triggerHandler('onScrollMove', [this]);
};
then in your code attach a event handler to the onScrollMove event and handle adding new rows in that. onScrollMove will fire whenever you scroll.
In the handler you can find how many rows are there in your list and that which row is on the top of your view port using something like
iscrollScrollEventHandler:function(event){
var contentDiv= $('div:jqmData(id="main") .ui-page-active div[data-role*="content"] ul li' );
var totalItemsonList = contentDiv.length;
var cont =$('div:jqmData(id="main") .ui-page-active div:jqmData(role="content")');
var itemToScrollOn = totalItemsonList - x; // x is the item no. from the top u want to scroll on. u need to keep updating i guess
var docViewBottom = $(cont).scrollTop() + $(cont).height();
var itemOffset = $(contentDiv[itemToScrollOn]).offset();
if(itemOffset){
var elemTop = itemOffset.top;
if (elemTop <= docViewBottom){
event.data.callback();
}
}
}
and in the callback add the code to add new rows. hope that helps.

Resources