简体   繁体   English

我们如何在钛合金网格视图项目中实现Onclick功能

[英]How we can implement the Onclick functionality in titanium grid view item

I have developed the gridview application using the widget (from this code) 我已经使用小部件开发了gridview应用程序(来自此代码)

https://github.com/pablorr18/TiDynamicGrid https://github.com/pablorr18/TiDynamicGrid

i have modified the code as per my client requirements. 我已经按照我的客户要求修改了代码。 Now if i like to click the view cart button on the list item it will alert the message like "Am clicked". 现在,如果我想单击列表项上的“查看购物车”按钮,它将提醒该消息,例如“我被点击”。 But i tried in various ways.but i can't find the solutions. 但是我尝试了各种方法。但是找不到解决方案。 Can anyone please explain me how we are write the code for this. 谁能解释一下我们如何为此编写代码。

I have follows below code in my application: 我在我的应用程序中遵循以下代码:

var items = [];
var showGridItemInfo = function(e){
    alert("Onclick the row");
}; 

var delay = (OS_ANDROID) ? 1000:500;

$.tdg.init({
    columns:3,
    space:5,
    delayTime:delay,
    gridBackgroundColor:'#e1e1e1',
    itemBackgroundColor:'#fff',
    itemBorderColor:'transparent',
    itemBorderWidth:0,
    itemBorderRadius:5,
    onItemClick: showGridItemInfo
});

function createSampleData(){
    var sendit = Ti.Network.createHTTPClient({ 
        onerror: function(e){ 
            Ti.API.debug(e.error); 
            alert('There was an error during the connection'); 
        }, 
        timeout:10000, 
    });           
    sendit.open('GET', url+'android_livedev/client/test.php?action=listitems&categoryid='+subcategorylist_category_id+'&productmin=0&productmax=50');  

    sendit.send(); 
    sendit.onload = function(){ 
        var response = JSON.parse(this.responseText); 
        if(response[0].success == 0){
            alert("No Products Found");
        }
        else {
            items = [];   
            for (var x=0;x<response[0].data.length;x++){
                var view = Alloy.createController('item_layout',{
                    image:imageurl+response[0].data[x].thumb_image,
                    product:response[0].data[x].product,
                    productprice:"$"+" "+response[0].data[x].price,
                    onItemClick: addcart,
                }).getView();
                var values = {
                    product: response[0].data[x].product,
                    image: response[0].data[x].thumb_image,
                    productid : response[0].data[x].productid,
                };
                items.push({
                    view: view,
                    data: values
                });
            };
            $.tdg.addGridItems(items);
            reateSampleData();

            $.tdg.clearGrid();
            $.tdg.init({
                columns:nColumn,
                space:nSpace,
                delayTime:delay,
                gridBackgroundColor:'#e1e1e1',
                itemHeightDelta: 0,
                itemBackgroundColor:'#fff',
                itemBorderColor:'transparent',
                itemBorderWidth:0,
                itemBorderRadius:5,
                onItemClick: showGridItemInfo
            });
            createSampleData();

        });

        $.win.open();

The item_layout.xml code is looking like : item_layout.xml代码如下所示:

<Alloy> 
    <View id="mainView"> 
        <ImageView id="thumb"/> 
        <Label id="product"></Label> 
        <Label id="productprice"></Label> 
        <Button id="addcart" onClick="additemtocart"/> 
    </View>
</Alloy>

EDIT: 编辑:

now am getting the view and the button id from that specified view. 现在从指定的视图获取视图和按钮ID。 But if am trying to click the button means can't able to do. 但是,如果试图单击该按钮,则意味着无法执行此操作。 can you check my code and give a solution. 您可以检查我的代码并提供解决方案吗?

i have added the below code : 我添加了以下代码:

var view = Alloy.createController('item_layout',{
        image:imageurl+response[0].data[x].thumb_image,
        product:response[0].data[x].product,
        productprice:"$"+" "+response[0].data[x].price
    }).getView();
    var controller = Alloy.createController('item_layout'); 
     var button = controller.getView('addcart');
    button.addEventListener('click', function (e){
    alert("click");
     Ti.API.info('click');
    });

First switch the ids to classes because they're non-unique. 首先将ID切换为类,因为它们不唯一。 Only use ID's if you must. 仅在必要时使用ID。 Second, try something like this: 其次,尝试这样的事情:

Try something like this: 尝试这样的事情:

$('.addCart').click(function() {
    var item = $(this).silblings('.product').text;
    addItemToCart(item);
});

Even better, add a data-productId attribute to the shopping cart button and you could do something like this: 更好的是,将data-productId属性添加到购物车按钮,您可以执行以下操作:

$('.addCart').click(function() {
    var itemId = $(this).attr('data-productId');
    addItemToCart(itemId);
});

This is better since you're only requiring the shopping cart button to be on the screen. 这样做会更好,因为您只需要在屏幕上显示购物车按钮即可。

All this said, you also probably need to include a fallback that adds the item to the cart when javascript isn't available on the page. 综上所述,当页面上没有javascript时,您可能还需要添加一个后备项,将其添加到购物车中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM