I have 5 Questions in database table
1 textbox
2 dropdown
3 dropdown
4 textbox
5 textbox
but when I finish transactions it shows with invalid order like
1 textbox
4 textbox
5 textbox
then
2 dropdown
3 dropdown
it should be shown as above with correct order
in getDropdown function to get all options item
what's i'm doing wrong, Anyone can help? please,
//main function initially call to get all question
function showQuestions(){
db.transaction(function(tx) {
tx.executeSql(selectAllStatement, [], function(tx, result) {
dataset = result.rows;
for (var i = 0, item = null; i < dataset.length; i++) {
item = dataset.item(i);
if(item['ANS_TYPE'] == "dropdown"){
getDropdown(item['QU_MESSAGE'],item['QU_ID']);
}
if(item['ANS_TYPE'] == "textbox"){
getTextbox(item['QU_MESSAGE']);
}
}//for loop
});//execute
});//transaction
}//end function
function getTextbox(question){
$('#elements').append('<div data-role="fieldcontain">'
+ '<label for="textinput1">'
+ question
+ '</label><br /><br />'
+ '<input name="" id="textinput1" placeholder="" value="" type="text">'
+ '<br /><br /><hr></div>').trigger('create');
}
function getDropdown(question,qu_id){
var options = ''; option = '';
options += '<div data-role="fieldcontain">'
+ '<label for="selectmenu1">'
+ question
+ '</label><br /><br />';
db.transaction(function(tx) {
tx.executeSql(selectChoiceStatement, [qu_id], function(tx, result) {
dataset = result.rows;
option = '';
for (var i = 0, item = null; i < dataset.length; i++) {
item = dataset.item(i);
var data = item['CH_MSG'];
option += '<option>'+data+'</option>';
}
options += '<select id="selectmenu1" name="">'+option+'</select>'
+ '<br /><br /><hr></div>';
$('#elements').append(options).trigger('create');
});
});
}
The callback function of executeSql is called asynchronously.
You should probably create the <select> immediately and add the <option>s later.
Related
Here is how I am adding fields.
$scope.addEmailField = function () { //Function to add new email field.
if (valid <= 1 && checkToDelete == 0) {
var mailTxtId = 'mail' + valid;
var mailModel = 'Contact_Email' + valid;
var hide = 'hide' + valid;
hide = false;
console.log(mailTxtId);
var emailDiv = angular.element(document.querySelector('#emailDiv'));
var element = $compile('<div id="' + mailTxtId + '" style="margin-left: -60px; width:200px; margin-top:15px"><input id= "' + mailModel + '" type = "text" class="form-control" ng-model="' + mailModel + '" ng-blur="validateEmailDynamic(' + valid + ')">' +
'<input id="' + valid + '" class="form-control" style="margin-left: 206px; width:54px; margin-top:-34px" type="button" value="-" ng-click="deleteField(' + valid + ')"><span ng-show ="' + hide + '" style="color:red">' +
'Invalid email</span></div>')($scope);
emailDiv.append(element);
valid = valid + 1;
}
};
But not getting the value of ng-model.
Store your input box in a directive's template. Then add ng-class that would determine whether it should show or not.
app.directive('inputBox', function(){
template:'<input ng-model="item">'
});
Usage in the html:
<div ng-class="{ input-box : triggerInputBox }"></div>
Controller:
$scope.triggerInputBox = true;
This is just one of many ways to accomplish this. But directives are very useful for dynamically showing templates.
I'm working for iPhone application using phonegap. In the application there are few dropdowns whose values are more the 10000. Now we are trying to replace the dropdown with Autocomplete.
We are maintaining those 10000 records in SQLite DB and fetch the records from the DB as user enters the string.
CODE:
<input type ="text" class="inputFormText" ng-model="Location.location" id="location" list ="locValues" placeholder="{{'lSummary_Location_text'|translate}}" autocomplete = "on" maxlength="80" ng-keyup="populateLocations($event)"/>
<div class="aListCon">
<datalist id="locValues">
</datalist>
</div>
$scope.populateLocations = function($event){
if ($event.target.value.length > 2) {
try
{
db.transaction(function(tx){
tx.executeSql("SELECT ID, VALUE FROM tbl_Location WHERE VALUE LIKE '%" + $event.target.value + "%'", [],
function(tx, res){
if(res.rows.length > 0)
{
var template = "";
for (var i = 0; i < res.rows.length; i++) {
var id = res.rows.item(i).value + ' / ID: ' + res.rows.item(i).id;
template += '<option class="aCon" id="' + id + '" value="' + res.rows.item(i).value + '"></option>';
};
document.getElementById('locValues').innerHTML = template;
}
else
console.log("No records found")
},
function(ex){
console.log("Populate Location Error: " + ex.message);
});
});
}
catch(ex)
{
console.log("Populate Location Error: " + ex.message);
}
};
};
I was able to fetch the records form the SQLite and append to the datalist, but Autocomplete is not displayed in the UI.
Any idea where I'm going wrong?
Thanks in advance
This code should be working:
var divtoappend=angular.element( document.querySelector( '#locValues' ) );
divtoappend.append("<option class="aCon" id="' + id + '" value="' + res.rows.item(i).value + '"></option>");
In this first code when I change the anoini, the gerar() function show the old value.
But, when I remove <div ng-if.... works fine.
do you knows what's wrong ?
Tks
// JavaScript Document
var app = angular.module('dadosHist', []);
app.controller('dadosHistCtrl', function($scope) {
$scope.mesini = 1; $scope.anoini = 2011;
$scope.mesfim = 7; $scope.anofim = 2015;
$scope.log = "";
$scope.escolherperiodo = true;
$scope.gerar = function() {
this.log = this.anoini;
meses = ((this.anofim - this.anoini) * 12) + (12 - this.mesini) + this.mesfim;
qtdLoop = arrEstacoes.length * meses;
tempoEstimadoMinutos = Math.round((qtdLoop * 20) / 60 );
this.log = 'Tempo Estimado: ' + tempoEstimadoMinutos + ' min.' ;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="dadosHist" ng-controller="dadosHistCtrl">
<input type="checkbox" ng-model="escolherperiodo">Escolher PerĂodo<br>
<div ng-if="escolherperiodo">
<input type="text" ng-model="mesini" placeholder="Mes">/<input type="text" ng-model="anoini" placeholder="Ano"><br>
<input type="text" ng-model="mesfim" placeholder="Mes">/<input type="text" ng-model="anofim" placeholder="Ano"><br>
</div>
<button ng-click="gerar()">Gerar</button> <br>
{{log}}
</div>
Always use a dot in ng-model ! . In other words use objects not primitives.
ng-if creates a child scope and since you are using primitives in ng-model you are losing 2 way binding with scope from this child scope.
var myModel ={
mesini : 1,
anoini : 2011,
mesfim : 7,
anofim : 2015
};
$scope.myModel = myModel;
HTML
<input type="text" ng-model="myModel.mesini">
Then in function:
$scope.gerar = function() {
$scope.log = myModel.anoini;
var meses = ((myModel.anofim - myModel.anoini)......
.....
}
Understanding scope nesting in angular is the most important thing to learn when using the framework
You should not assign value to this, but to $scope inside gerar function:
$scope.gerar = function() {
$scope.log = $scope.anoini;
meses = (($scope.anofim - $scope.anoini) * 12) + (12 - $scope.mesini) + $scope.mesfim;
qtdLoop = arrEstacoes.length * meses;
tempoEstimadoMinutos = Math.round((qtdLoop * 20) / 60 );
$scope.log = 'Tempo Estimado: ' + tempoEstimadoMinutos + ' min.' ;
}
I am Bootstrap Select user in IE11.
When I fill data with ajax and open it then it automatically move down wards and when I select any value then it's not selected. How can I remove this error?
jQuery.ajax({
url: base_url + "UserBusinesses/ajaxState/" + countryId,
dataType: 'json',
beforeSend: function () {
$('#UserBusinessStateId').html('<option value="">Loding... </option>');
$('#UserBusinessStateId').selectpicker('refresh');
$('#UserBusinessCityId').html('<option value="">Select city</option>');
$('#UserBusinessCityId').selectpicker('refresh');
},
success: function (obj) {
var addHtml = '';
addHtml += '<option value="">Select state</option>';
$.each(obj, function (key, value) {
var selected = "";
if (selectedState == key) {
var selected = "selected='selected'";
}
addHtml += "<option value='" + key + "' " + selected + ">" + value + "</option>";
});
jQuery("#UserBusinessStateId").html(addHtml);
$('#UserBusinessStateId').selectpicker('refresh');
}
});
I am facing this issue In IE and I have find the solution
$('#UserBusinessStateId').selectpicker('refresh');
remove this and add this code
$('#UserBusinessStateId').selectpicker('render').selectpicker('refresh');
I am working on quiz app. I have add the time out to every question even though user attempts the question or not.
my code in view:
<p ng-repeat="opt in question.answer">
<label ng-show="opt._correct" for="">
<input type="radio" name="questionAnswer" id="opt.id" value=""
ng-click="checkAnswer(1)" />
{{opt.__text}}
</label>
<label ng-hide="opt._correct" for="">
<input type="radio" name="questionAnswer" id="opt.id" value=""
ng-click="checkAnswer(0)" />
{{opt}}
</label>
</p>
my code in controller:
$scope.randomQuestions = function(questionslist){
$scope.randomQuestion = questionslist
[Math.floor(Math.random() * questionslist.
length)];
console.log($scope.quiz);
var item = $scope.quiz.splice($scope.randomQuestion,1)[0];
$scope.questions = new Array();
$scope.questions.push($scope.randomQuestion);
$scope.counter = $scope.counter + 1;
return $scope.questions;
}
$scope.checkAnswer = function(option){
console.log('check answer');
if(option == 1){
console.log($scope.score);
$scope.score = $scope.score + parseInt($scope.level._points_per_question);
console.log($scope.score);
} else{
}
console.log($scope.counter);
if ($scope.counter < parseInt($scope.level._total_questions + 1)){
$scope.randomQuestions($scope.quiz);
} else {
console.log('5 questions');
}
}
$scope.nextLevel = function(){
$scope.total_score = $scope.total_score + $scope.score;
$scope.score = 0;
$scope.counter = 0;
if($scope.level_count == 1){
$scope.level_count = $scope.level_count + 1;
$scope.quiz = $scope.quiz2.question;
$scope.level = $scope.quiz2;
$scope.randomQuestions($scope.quiz);
} else if($scope.level_count == 2){
$scope.quiz = $scope.quiz3.question;
$scope.level = $scope.quiz3;
$scope.randomQuestions($scope.quiz);
$scope.level_count = $scope.level_count + 1;
} else {
$scope.level_count = $scope.level_count + 1;
$scope.result_text();
}
}
$scope.result_text = function(){
$scope.result = parseInt(($scope.total_score * 100) / 400);
for(var k=0; k < $scope.score_rules.length; k++){
if($scope.result >= $scope.score_rules[k]._min_percent){
$scope.score_status = $scope.score_rules[k].__text;
console.log($scope.score_rules[k].__text);
console.log($scope.score_rules[k]);
}
}
}
Can any one suggest how to call time out from view?
Please find below the code
function controller($scope)
{
$scope.showflag = true;
setTimeout(function ()
{
$scope.$apply(function()
{
$scope.showflag = false;
});
}, 1000);
}
You could use CSS transitions or animation to do the actual transition and use ngAnimate to trigger the transitions.
There are a few examples for ngAnimate on this page.
You can try this approach:
Modify your checkAnswer() code and call another function which sets
the timeout/sleep. When the call returns from the timeout function, set a
variable (eg. isVisible) to false and use this variable in ng-show.
Something like this:
So, new ng-show would look like ng-show=" isVisible && opt._correct"