Creating Dynamic Drop Down List in Talend

Creating Dynamic Drop Down List in Talend

Created a sample job to retrieve the records from a particular table only by selecting the table name from a drop down list.

First we have to create tables and load the data into the database. We have to get these tables in a particular database as drop down list. So that we can select a table name at run time.

Here is a point to note that whenever we are using tjava components it will execute first even in the sub-job. So I am using tprejob component to display the list of table names. Please find the screenshot of the entire job.

main_job

PreJob:

In my required database I have 3 tables. So I have to retrieve these table names as list. to store these names we require one context variable. So create a context variable as shown below:

context

I am using mysql database. To retrieve the tables name I have taken tmysqlInput component and configured database name, port,username, password . And in Query part write the below Query:

“select Table_name from information_schema.`TABLES` where Table_schema='<databasename>'”

table_names_qry

Take tJavaflex component and connect with tmysqlInput by using ‘row’. tJavaflex will execute the code by loop wise for each record of its input flow.

First we have to create an array list type variable in start code. And then need to add each row (table name) from input flow to this array list in the main code section. And in the End code section, convert the list to an array and then create a drop down list.

javaflex

End Code:

String [] TableNames=new String[tables_list.size()];
TableNames=tables_list.toArray(TableNames);
JFrame frame = new JFrame(“Input Dialog Example”);
String returnValue = (String) JOptionPane.showInputDialog(frame,
“Select the table name you want to query?”,”Tables Information”,JOptionPane.QUESTION_MESSAGE,null,TableNames,TableNames[0]);
context.tables=returnValue;
if(context.tables != null)
System.out.println(“selected table: “+context.tables);

 //on clicking cancel button in drop down box it will show this message
else System.out.println(“please select table name”);

In the Advanced settings of tjavaflex import required java libraries as shown below:

tjavaflex_import

Pre-job is completed.

Main Job:

After selecting the drop down list  based on the selected table name that particular sub-job condition will become true and it will retrieve the records from selected table.

In case, if we not select the table from drop down list on clicking cancel button, it will throw null pointer exception. To avoid that please enter the below code in all tjava components in the sub-jobs.

if(context.tables == null)
{
context.tables=”0″;
}

Connect tmysqlInput component to tjava by using Runif condition, please write the below code in the runif condition:

context.tables.contains(“categories”).Here Categories is one of my table name. If we select this table in drop down it will become true and execute otherwise it will become false.

Now configure the tmysqlInput component with port,user name,database name,password and table name.And now the table name will store in the context variable. so you can use the context variable in place of table name in the select query. Please find the below screen shot:

sql_input

And connect the tlogrow to display the result on console.

Configure the remaining sub-jobs also in the same way.

Now execute the job, it will display table names in the drop down list.

dd1

If you click on cancel, on the console it will display the message as “Please select the table name”

cancel

If we select any one of the table name from the list, that particular matched condition will become true and that sub-job will execute and display the records of that table on the console. Here I selected Customer table from the drop down so it will display the records from customer table:

selected table

Result:

result