How to bind the data in to label in Titanium alloy? - javascript

How can I bind data from controller to xml, My code is as follows,
View:
<Collection src="respondentAge"/>
<Label id="question"></Label>
Styles
".question":{
font:{
fontSize:18,
fontWeight:'normal'
},
color:"#000",
left:10,
height:Ti.UI.SIZE
}
Controller
var agenames = Alloy.Collections.respondentAge;
agenames.on("reset", function() {
var agenamesLength = agenames.length;
var question;
for (var i = 0; i < agenamesLength; i++) {
question = agenames.at(i).get("quesion");
// I need to bind the 'agenames.at(i).get("quesion")' value in to label in
}
});
agenames.fetch({
query:"SELECT * FROM respondentAge WHERE languageID ='1';"
});
The question text is coming from the database, So for for question I have added the label and I'm retrieving the value from database and I need to set the label value as retrieving value.
How can I do that

I propose you use the setText(text) property of Label. You can read more about it here: Label docs
agenames.on("reset", function() {
var agenamesLength = agenames.length;
var question;
for (var i = 0; i < agenamesLength; i++) {
question = agenames.at(i).get("quesion");
$.question.setText(question);
}
});

Related

Datalist unexpected return from eventlistener

I'm trying to get a return from the datalist selection to print it to the #body div and use it to call other function as well, but unfortunately for some reason I get a return of "undefined" printed instead and I can't control the selection to be just from the provided options of the datalist as well.
Any advice to fix both problems would so much appreciated!
//Html snippet
<input id="dirSearch" list="dirList" type="text"/>
<datalist id="dirList"></datalist>
// Array
const db = [
1,
2
];
//Functions
function autocomplete () {
let options = '';
for(let i = 0; i < db.length; i++)
options += '<option value="'+db[i]+'">'+db[i]+'</option>';
document.getElementById('dirList').innerHTML = options;
}
autocomplete();
function searchSelection () {
let dirSearch = document.getElementById('dirSearch');
let dirList = document.getElementById('dirList');
dirSearch.addEventListener ('input', function () {
document.getElementById('body').innerHTML = "<h1>" +dirList.option+ "</h1>";
}, false);
}
searchSelection();
getElementById("dirSearch").value instead of dirList.option
and moreover it is dirList.options -> which returns available options in the datalist.

why not assign var a value into id field?

$.getJSON('getwarehousename',function(data){
for(var i=0;i<data.length;i++){
$("#plantname").val(data[0][0]);
var a = JSON.stringify(data[0][1]);
alert("PlantWH...."+a)
$("#plantwhname").val(a);
alert("wname.."+$("#plantwhname").val())
}
});
In this variable a value is plantwarehouse01, but this value can not be assigned to plantwhname id field?
$(document).ready(function() {
var html2="<label><select id='plantwhname' class='wname' name='store1'>";
var ht2="<option selected='selected'>Select</option>";
$("#plantware").html(html2);
$.getJSON('findPlantwarehouseForMultiple',function(data){
if(data!=0){
for(i=0;i<data.length;i++){
ht2+="<option>"+data[i].plantwarehousecode+"</option>";
}
ht2+="</select></label>";
$("#plantwhname").html(ht2);
$("#plantwhname").attr("Style","width:220px;");
}
});
});
i think your select option list and your new add option not same so new option not select so first add this option
$('#selectID').append($('<option>',
{
value: value_variable,
text : text_variable
}));
$.getJSON('getwarehousename',function(data){
for(var i=0;i<data.length;i++){
$("#plantname").val(data[0][0]);
var a = JSON.stringify(data[0][1]);
alert("PlantWH...."+a);
$('#plantwhname').append($('<option>',
{
value: a,
text : a
}));
$("#plantwhname").val(a);
alert("wname.."+$("#plantwhname").val())
}
});
then select it using this code. hope this will help you

Angularjs checkbox value on edit profile page

I am new to angularjs and getting some problem on checkbox values on edit profile page.
I have an array for checkbox
$scope.checkBoxes = [
{id:'1', value:'Reading'},
{id:'2', value:'Cooking'},
{id:'3', value:'Dancing'},
{id:'4', value:'Singing'}
];
and I have used ng-repeat for showing checkbox
<div ng-repeat="checkBox in checkBoxes" style="display:inline;">
<input type="checkbox" ng-model="checkBox.selected" id={{checkBox.id}}' ng-checked="checkItem(checkBox.id)" ng-change="listActionsHandler(checkBoxes)">{{ checkBox.value }}
</div>
This is my ng-change function:
$scope.listActionsHandler = function(list) {
var has = [];
angular.forEach(list, function(item) {
if ( angular.isDefined(item.selected) && item.selected === true ) {
has.push(item.id);
}
});
formData['hobby'] = has;
}
and this is my ng-checked function that is used for showing checkbox checked according to database saved value.
var arr_checked_items = data.data.hobby;
var arraySize = arr_checked_items.length;
$scope.checkItem = function (id) {
var checked = false;
for(var i=0; i<= arraySize; i++) {
if(id == arr_checked_items[i]) {
checked = true;
}
}
return checked;
};
These function work properly on add and edit page, but problem is that when I doesn't apply any changes on checkbox and click on submit button it take blank value.
My question is that: if I does not apply any changes, it should take old value of checkbox and if I apply any changes, it should take new value of checkbox.
Please try with below code. I am not sure weather it is working or not.
$scope.checkItem = function (id) {
var checked = "";
for(var i=0; i<= arraySize; i++) {
if(id == arr_checked_items[i]) {
checked = "checked";
}
}
return checked;
};

Populate multiple fields with javascript

I am new to javascript and I can't populate many fields with one click.
<script>
function addTxt(txt, field)
{
var myTxt = txt;
var id = field;
document.getElementById(id).value = myTxt;
}
</script>
<input type="text" name="xx" id="info" autofocus="required">
<p>x</p>
I've got 3 more fields.
Thanks.
You can use
function addTxt(txt, ids)
{
for (var i=0, l=ids.length; i<l; ++i) {
document.getElementById(ids[i]).value = txt;
}
}
And call it like
addTxt('Some text', ['id1', 'id2', 'id3']);
You can populate multiple fields. I have shared a jsfiddle link. You can populate multiple fields using this code.
function addTxt(_val, _id,_no)
{
var _myTxt = _val;
var _id = _id;
for(var i=1;i<=_no;i++){
document.getElementById(_id+i).value = _myTxt;
}
}
Click here to see DEMO
I think you don't need a function to do this.
Just use
document.getElementById('id1').value
= document.getElementById('id2').value
= document.getElementById('id3').value
= 'Some text';
Or, if you think document.getElementById is too long, use a shortcut:
var get = document.getElementById;
/* ... */
get('id1').value = get('id2').value = get('id3').value = 'Some text';
Try getting the elements by tagName or by className instead of by id, then using a for loop to iterate through each one.

Multiple checkbox can't access from JavaScript function

I have dynamic multiple check boxes which is used to restore multiple files. It works perfectly when I have more than 1 check boxes. Here is my php code for check boxes:
<form name="RestoreFile">
<input type="checkbox" title="'.$FldDoc['FldDocumentName'].'" name="restore_checkbox" value="'.$FldDoc['FldDocumentID'].'" id="restore_'.$NodeId.'_'.$FldDoc['FldDocumentID'].'"/>
<input type="button" value="Restore" onclick="RestoreDocFile(\''.$NodeId.'\',this.form.restore_checkbox);" />
</form>
And the definition of function RestoreDocFile() is given below:
function getSelected(opt)
{
var selected = new Array();
var index = 0;
for (var intLoop = 0; intLoop < opt.length; intLoop++) {
if (opt[intLoop].checked)
{
index = selected.length;
selected[index] = new Object;
selected[index].value = opt[intLoop].value;
selected[index].index = intLoop;
}
}
return selected;
}
function RestoreDocFile(nodeid, opt)
{
var getSelectDocIds = getSelected(opt);
//alert(nodeid+','+getSelectDocIds);
var strSelectedDocIds = "";
var i=0;
for (var item in getSelectDocIds)
{
if(i!=0)
{
strSelectedDocIds+=":";
}
strSelectedDocIds += getSelectDocIds[item].value ;
i++;
}
}
The problem is that if there has 1 checkbox at the time of form load it doesn't work properly.
Try replacing
onclick="RestoreDocFile(\''.$NodeId.'\',this.form.restore_checkbox);"
with
onclick="RestoreDocFile(\''.$NodeId.'\',this.form.getElementsByName(\'restore_checkbox\'));"
This will ensure you get a NodeList regardless of how many checkboxes there are.

Categories

Resources