Titanium Alloy get all label text value from xml - titanium

I want to console log all label text value displayed from my xml, And this is how I was doing to log the label Ti.API.info($.label.getText()); , but this code doesn't seem to work since this only works for only a single value from a variable. How I'm going to do this? Sorry,Just so noob enough. Thanks!
<TableView id="table" dataCollection="person">
<TableViewRow id="row">
<Label id="label" text="{name}"></Label>
</TableViewRow>
</TableView>

from the Appcelerator documentation http://docs.appcelerator.com/titanium/latest/#!/guide/Alloy_Data_Binding
dataTransform: specifies an optional callback to use to format model attributes. The passed argument is a model and the return value is a modified model as a JSON object.
<TableView id="table" dataCollection="person" dataTransform="dumpText" >
<TableViewRow id="row">
<Label id="label" text="{name}"></Label>
</TableViewRow>
</TableView>
So we can use this method to dump whatever is being added to the list
function dumpText(model) {
// model to a JSON object
var o = model.toJSON();
Ti.API.info(o.name);
return o;
}

Related

Is it possible to render elements conditionally in NativeScript-Vue?

I hope you can help me to solve my problem.
I am working on a Nativescript-Vue Application and I want to render this conditionally:
<Label if="isRendering" text="This is a text"></Label>
But this does not work.
I already tried it with <v-template if="..."></v-template> but this also does not work.
If i am not mistaken, in Vue.js it is possible to render conditionally with v-if and i am searching for a possibility to make it in Nativescript-Vue.
Thank you
It's the same like in Vue.js.
Instead of using if you should be using v-if.
Playground example
Example:
<Label v-if="isRendering" text="This is a text"></Label>
But if you want to use an if in a ListView it's different.
Example:
<ListView for="item in listOfItems" #itemTap="onItemTap">
<v-template>
<Label :text="item.text" />
</v-template>
<v-template if="$odd">
<!-- For items with an odd index, shows the label in red. -->
<Label :text="item.text" color="red" />
</v-template>
</ListView>
But more info about that in the docs

Titanium appcelerator how to update view inside listview?

I have an Appcelerator Titanium app to do, i have a Ti.UI.ListView with a view inside (view_one.xml):
...
<ListView onItemclick="getEvaluation" id="myLisview" defaultItemTemplate="templateLm" bottom="0" top="10" onItemclick="changePage" backgroundColor="white">
<Templates>
<Require id="lm_items" src="common/templateLm"/>
</Templates>
</ListView>
...
in common/templateLm :
<Alloy>
<ItemTemplate name="templatePageMission">
<View bindId="evaluate" height="10dp">
<Label text="evaluate"></Label>
</View>
<View bindId="stars" height="15dp"/>
</ItemTemplate>
</Alloy>
in view_one.js :
function getEvaluation(e){
switch(e.bindId){
case 'evaluate':
var item = e.section.getItemAt(e.itemIndex);
console.log(item) // <= this is empty ????
item.stars.backgroundColor = "red";
break;
}
}
and when click on the evaluate view I finally got:
undefined is not an object (evaluating item.stars.backgroundColor)
If anyone can help, this is great, anyway thanks for this great community.
You have assigned the itemclick event twice:
onItemclick="changePage"
and
onItemclick="getEvaluation"
So if you just use the latter one, it should catch the controller function correctly and the item should be updated correctly, since your controller code looks valid.

Appcelerator Titanium Alloy | How to access element of other class

In Alloy Titanium, I can access XML elements with their id $.element_id but how can I get elements of other class? or xml?
EDIT
I have two files.
1. file1.js, file1xml.xml
2. file2.js, file2xml.xml
In File1.js i want to access the variable of file2xml.xml. how can i achieve this?
Anything with an id can be accessed:
file1.xml
<Alloy>
<View id="myView" />
</Alloy>
file2.js
var ctrl1 = Alloy.createController('file1');
ctrl1.myView.backgroundColor = 'red';
if you have required file2.xml in file1.xml like
<Require src="common/viewToolBar" id="viewToolBar"/>
then you can get the element with the id in file1.js like
$. viewToolBar.getView('viewSynch').visible = false;
link for more details
You can use Require tag in alloy with an Id and you can access its elements as below.
**File1.xml**
<Alloy>
<View>
<Label id="labelId">Middle</Label>
</View></Alloy>
**File2.xml**
<Window>
<Require src="File1" id="File1View" type="View"/>
<View id="header"><Label id="headerText">Header</Label></View>
<View id="nav">
<Label class="navButton" onClick="openTab" controllerName="home">Home</Label>
<Label class="navButton" onClick="openTab" controllerName="news">News</Label>
<Label class="navButton" onClick="openTab" controllerName="info">Info</Label>
<Label class="navButton" onClick="openTab" controllerName="more">More</Label>
</View>
</Window>
**File2.js**
$.File1View.labelId.text = "hi";

Get the row id in TableViewRow

The following code to print the "local" model in tableview, its working, now my problem is to fetch the row id or row detail by clicking the each row. I have attached the showDetail function on each row to get the row detail.But unable to get the row attch data. How can i achieve this please help me, I really appreciate your answers.
<TableView dataCollection="local">
<TableViewRow onClick="showDetail">
<View layout="horizontal">
<ImageView image="{image}" width="50" height="50"/>
<Label id="name" text="{title}"></Label>
</View>
</TableViewRow>
</TableView>
function showDetail(e){
console.log(e.index);
}
Have a look at the TableView API click Event:
http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.TableView-event-click
There are all properties: e.g. index=the number of the row
And have a look at row and rowData

dynamic data for each TableViewRow

I'm using alloy (markup and models) to construct a tableview, Everything works perfect
<Collection src="spot" />
<Window>
<TableView id="spotTableView" dataCollection="spot">
<TableViewRow title="{name}">
<View class="header-view">
<Label class="title-header" text="{name}"/>
<Label class="desc-header" text="{desc}"/>
</View>
<View class="fixed-view">
<ImageView class="image" image="{logo}"/>
<View class="info-view">
<Label class="green-label" text="{cost}"/>
<Label text="{minimum}"/>
</View>
</View>
</TableViewRow>
</TableView>
</Window>
But I have a question: How I can modify dynamic data form for each tableviewrow? I need to change the colors of the labels depending on the data that brings each model in the collection. For example, if the cost is 0 that the label "cost" is green, but if the cost is 100 then the color of the label is red.
I guess this must realizarce on the controller, but I would not like create the tableviewrow out of view, because they do not want to miss the advantages of data bindings
Add a field containing the color to the model, and bind the label's color to it.