What is the best way to add a button to every row in Titanium Studio?

Is there a way to add a different button inside each row (createTableViewRow)? I have created five buttons using Titanium.UI.createButton, but I'm struggling to figure out how to place all five buttons in every row. Can someone provide some guidance on how to achieve this?


function sendAjax() {

    var xhr = Titanium.Network.createHTTPClient();

    xhr.onerror = function(e){
        var error = e.error;
        alert(error);               
    };
    xhr.open('GET', 'http://xxxxxxxxxxxxxx');
    xhr.send();

    var tv = Titanium.UI.createTableView({
        height: Titanium.UI.SIZE,
        width: Titanium.UI.FILL
    });
    win2.add(tv);

    xhr.onload = function() {

        var data = [];
        var schools = JSON.parse(this.responseText);

        for (s in schools) {
            data.push(Titanium.UI.createTableViewRow({
                title: schools[s].Name
            }));
        }
        tv.data = data;
    };
}

Answer №1

Instead of adding listener events to each button inside the row, it is recommended to add the listener event on the tableView itself:

tableView.addEventListener("click", function (event) {
    if (event.source.buttonid) {
       //handle button click
    }
});

Answer №2

Give this a try:

var win = Ti.UI.createWindow({
backgroundColor : '#fff'
});

var tableData = [];

for(var i = 0; i < 10; i++) {
var row = Ti.UI.createTableViewRow();
var button = Ti.UI.createButton({
    title : 'Button ' + i,
    width : 100,
    height : 40,
    buttonid : i    //our custom button property
});
row.add(button);
tableData.push(row);

button.addEventListener('click', function(e) {
    Ti.API.info('button ' + e.source.buttonid + ' clicked.');
    alert('Button ' + e.source.buttonid);
});
}

var tableView = Ti.UI.createTableView({
data : tableData
});

win.add(tableView);

win.open();

This solution was found on this page

UPDATE :

function sendAjax() {

var xhr = Titanium.Network.createHTTPClient();

xhr.onerror = function(e){
    var error = e.error;
    alert(error);               
};
xhr.open('GET', 'http://xxxxxxxxxxxxxx');
xhr.send();

var tv = Titanium.UI.createTableView({
    height: Titanium.UI.SIZE,
    width: Titanium.UI.FILL
});
win2.add(tv);

xhr.onload = function() {

    var data = [];
    var schools = JSON.parse(this.responseText);
    for (s in schools) {
    var row = Ti.UI.createTableViewRow();
    var rowItem = Ti.UI.createView({
       height : 40,
       width : Ti.UI.FILL,
       layout : 'horizontal'
    });
    row.add(rowItem);
    var title = Ti.UI.createLabel({
       height : 40,
       text : schools[s].Name,
       width : Ti.UI.SIZE
    });

    rowItem.add(title);
    var button = Ti.UI.createButton({
        title : 'click ',
        width : 100,
        height : 40,
        buttonid : s    //our custom button property
    });
    rowItem.add(button);
    data.push(row);

    button.addEventListener('click', function(e) {
        Ti.API.info('button ' + JSON.stringify(e.source.buttonid) + ' clicked.');

    });
    };

    tv.data = data;
};
};

Remember to mark this as the correct answer if it helped you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The perplexing problem of scrolling in Dojox/mobile/scrollableview

I've noticed some scroll issues with the dojox/mobile/ScrollableView (version 1.10+) in my cordova application, specifically on Android phones. This problem seems to be affecting other widget-based architectures and even some store apps as well. I wou ...

Troubleshooting Puppeteer compatibility issues when using TypeScript and esModuleInterop

When attempting to use puppeteer with TypeScript and setting esModuleInterop=true in tsconfig.json, an error occurs stating puppeteer.launch is not a function If I try to import puppeteer using import * as puppeteer from "puppeteer" My questi ...

What could be causing the issue with uglify not functioning properly with AngularJS content?

I've created some gulp tasks to assist in building my web project. One of the tasks involves minifying js files. Here is the task code snippet: gulp.task('minify' , function() { console.log('Copy minified js '); return gulp ...

The ajax success response transforms when using @html.raw

In my Razor viewpage, I have the following jQuery code: $(document).ready(function () { var listValues = @Html.Raw(Json.Encode(Session["list"])); $("#nsline").click(function () { alert(listValues) $.ajax({ type: "P ...

Is it possible to incorporate HTML and CSS into a npm package?

I've been searching extensively for an answer to this question, but haven't found a clear answer. I recently began using the node package manager and I'm wondering if it's possible to publish a package that includes HTML and CSS, or if ...

Displaying the currently logged in user's name with NodeJS/ExpressJS/Passport

In my quest to showcase the username for logged-in users (function 3), I encountered a dilemma. Initially, only function 1 existed in my codebase. To address this issue, I made modifications and introduced function 2 to facilitate displaying usernames of a ...

Drawing graphics on an HTML5 Canvas with the power of React Native

How should html5 canvas be utilized in react native? Is it necessary to utilize webview, a plugin, or can plain html canvas be used in react native code? Appreciate your help! ...

Confirm the email address using the autocomplete feature in the field

I'm currently utilizing Material UI to design an autocomplete field with multiple inputs that gives users the option to either choose from existing email addresses or input their own. An example of what I'm trying to achieve can be seen here: ht ...

The error message popping up reads as follows: "TypeError: _this2.setState cannot

I am encountering an error that I don't quite understand. How can I resolve this issue and why is it occurring in the first place? Although I am receiving the correct response from the API, I am also getting an error immediately after making a call. ...

Issue with karma-ng-html2js-preprocessor failing to generate modules

Struggling to configure the karma-ng-html2js-preprocessor. While Karma has been successfully detecting all my JavaScript files, it's having trouble generating a module from the HTML preprocessor. Take a look at my options object below. I've spec ...

The error message displayed in Andriod Studio states that there is an issue converting the value "<br" of type java.lang.String to a JSONObject,

I am currently in the process of developing an android app using Android Studio that involves sending data from a User Registration activity to a database. The goal is for the user to input all the required information, click the register button, and have ...

What is the solution to setting true and false equal to true in an AngularJS expression?

How can I determine if an input field has been touched and is empty, without using the built-in functions in Angular? <form-input is-invalid-on="isTouched && !gs.data.name || gs.data.name.length > 50"></form-input> If isTouched &am ...

When the table is clicked, a dynamic accordion table should appear

My current code displays a table inside another table using a PHP for loop. While I can retrieve the data successfully, I'm facing issues with the UI layout and accordion functionality. The inner table is displaying beside the outer table instead of b ...

Splitting Code in React Components using Webpack within a Ruby on Rails Application

I'm currently integrating ReactJS components into my Rails application using the Webpack gem. However, I am facing an issue where the React components are only being loaded in specific areas within the layout of the Rails application. This results in ...

Real Android Integration tests without mocking

When working with iOS, I can create an integration test in the following way: // Setting up expectation to ensure all async tasks finish before ending the test. let expectation = expectationWithDescription("Sign in"); // Calling API method for signing in ...

What could be causing my .vue component to be undefined?

I've encountered an issue while using parcel-plugin-vue to compile a basic .vue component from a file. The compilation process seems to be running smoothly with no errors, but when I try to view the page in my browser, it appears blank without any con ...

What is the solution to resolving the problem indicated by the error message "at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"?

I encountered an issue while attempting to send a token to the user for password reset. The error message I received is as follows: Error: there was an error sending the email, try again later at exports.forgotPassword (C:\\Users\\Abdur ...

After receiving the go-ahead from JavaScript, I am planning on integrating PHP into my project. I have come across some recommendations

Looking for assistance with AJAX functionality on a website. Specifically, users should be prompted to confirm a purchase before completing it. If they confirm, the purchase goes through; if they decline, it does not. Originally considered using PHP within ...

Retrieve your Docusign Access Code using the typescript httpClient.post method

I am currently struggling to obtain the access token through the Docusign API. My code is producing an error in Xcode and I am unable to make it work on my native device or browser, despite successfully testing it via Postman. When attempting to run it in ...

Traversing a two-dimensional array backwards in JavaScript

I am working with an array that contains different teams: The structure looks like this: leagues = new Array( Array('Juventus'), Array('Milan'), Array('Inter')); My goal is to iterate through the array and generat ...