我有一个TableView,其中包含来自我的成员的消息。 我添加了一个标签来显示每个帖子上的Like数量。
如何点击标签而不是行?
listeclouds.js
var args = arguments[0] || {};
var dataArray2 = [];
$.tableView.addEventListener("click",function(e) {
console.log(e.source);
if (e.source.id == "likeCloud") {
Ti.API.info("Click like");
}
});
var sendit = Ti.Network.createHTTPClient({
onload: function(){
var json = JSON.parse(this.responseText);
//var json = json.todo;
if(json.length == 0){
$.tableView.headerTitle = "The database row is empty";
}
dataArray2 = [];
for( var i=0; i<json.length; i++){
dataArray2.push((Alloy.createController('listecloudsrow', {
username: json[i].username,
content: json[i].content,
filename: json[i].filename,
jaimes: json[i].jaimes,
coms: json[i].coms,
idpost: json[i].id
}).getView()));
};
$.tableView.setData(dataArray2);
}
});
sendit.open('GET', 'http://www.****');
sendit.send();
listeclouds.xml
<Alloy>
<Window id="listeclouds" title="Clouds">
<TableView id="tableView"/>
</Window>
</Alloy>
listecloudsrow.js
var args = arguments[0] || {};
var clouds = args.content;
$.usernameList.text = args.username;
$.content.text = clouds.replace(/\r\n|\n|\r/gm," ");
$.like.text = args.jaimes;
$.coms.text = args.coms;
if(args.filename)
{
$.avatar.image = "http://www.********/"+ args.filename;
}else{
$.avatar.image = "http://www.********/";
}
function montrerCloud(e) {
var payload = {
idpost: args.idpost,
};
Alloy.createController('ViewCloud',payload).getView().open();
}
listecloudsrow.xml
<Alloy>
<TableViewRow class="RowClouds" layout="horizontal" onClick="montrerCloud">
<View class="GaucheListe" width="10%">
<ImageView id="avatar"/>
<Label id="usernameList"/>
</View>
<View class="MilieuListe" width="80%">
<Label id="content"/>
</View>
<View class="DroiteListe" width="10%" layout="vertical" id="likeCloud">
<View layout="horizontal" height="25dp" width="100%">
<Label id="like" />
<Label icon="fa-thumbs-up" />
</View>
<View layout="horizontal" width="100%">
<Label id="coms" />
<Label icon="fa-comments" />
</View>
<Widget id="fa" src="com.mattmcfarland.fontawesome"/>
</View>
<View height="2dp" width="100%" backgroundColor="#000"></View>
</TableViewRow>
</Alloy>
答案 0 :(得分:1)
如果您将点击监听器添加到该行,您将获得事件源中元素的名称:
tableview.addEventListener("click",function(e) {
console.log(e.source);
if (e.source.id == "like") {
// this is the like label
}
});
并且您将看到哪个元素收到了点击。