Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

OptionsDialog only seems to associate with last TableRow

I have what I thought was a pretty simple requirement - file browsing the Data directory to either Upload or Delete the files.

However, the OptionDialog created will only associate itself with the last row in the table.

var dir = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory);
var listing = dir.getDirectoryListing();
Ti.API.info('directoryListing = ' + listing);
Ti.API.info('getParent = ' + dir.getParent());
Ti.API.info('spaceAvailable = ' + dir.spaceAvailable());

var data = [];
var z = 0;


    for (var i = 0; i < listing.length; i++) {
        Ti.API.info('Found file: ' + listing[i]);


        var row = Ti.UI.createTableViewRow({height:50,width:200, selectedBackgroundColor:'#777'});

        var rlabel = Ti.UI.createLabel({
        text:listing[i], left:5, top:5}
        );

        var dialog = Titanium.UI.createOptionDialog({
        options:['Upload', 'Delete', 'Cancel'],
        destructive:1,
        cancel:2,
        title:listing[i]
        });

        dialog.addEventListener('click',function(e)
        {
            Ti.API.info('Action: ' + e.index + ' and filename is ' + e.source.title);            
        });

        rlabel.addEventListener('click',function()
        {
            dialog.show({view:rlabel,animated:true});
        });

        row.add(rlabel); 
        data[z++] = row;

    }


var table = Titanium.UI.createTableView({
data:data, 
height: 500,
top:150
});

Upload.view.add(table);

Anyone know how to create the OptionDialogs in such as way as they are tied to the correct row?

— asked June 3rd 2010 by Ken Bantoft
  • ipad
  • iphone
  • optiondialog
0 Comments

1 Answer

  • Accepted Answer

    Yeah - this is a pretty basic JS issue, not specific to Titanium… There's a lot of ways to accomplish this. Probably the easiest is something like this…

    change your row to add the label as a data property - will be ignored by table but used below…

           var row = Ti.UI.createTableViewRow({
                 height:50, 
                 width:200, 
                 selectedBackgroundColor:'#777',
                 text:listing[i]
           });
    

    change your click handler to just fire to a generic function (defined below)

    row.addEventListener('click',rowClick);
    

    outside your loop define your row handler:

    function rowClick(e)
    {
       // source points to the person firing the event 
       // which is the row, get the title from the row
       var title = e.source.text;
    
       // now do your normal option dialog code
       var dialog = Titanium.UI.createOptionDialog({
            options:['Upload', 'Delete', 'Cancel'],
            destructive:1,
            cancel:2,
            title:title
            });
    
         // finish your dialog code as normal here
    }
    

    Since all events have a source property which is the object that fired the event - in this case the row object - you can now simply pull out a property from the object - in this case text.

    Now just build the option dialog on the fly with that data.

    — answered June 3rd 2010 by Jeff Haynie
    permalink
    1 Comment
    • Thanks for this - that solves the problem of passing along the right data to the OptionDialog!

      One thing was missing - actually showing the box. For anyone else who needs this, after the //finish your dialog, I added:

      dialog.show({view:e.source,animated:true});
      

      And now the OptionDialog anchors itself to the right row element. Slick!

      — commented June 4th 2010 by Ken Bantoft
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.