Kirix Support Forums

sort list in choicebox

Please post any help questions, requests or other feedback here.

sort list in choicebox

Postby abenitez77 on Fri Aug 10, 2012 6:48 am

How can i sort my list in the choicebox below?

// Check to make sure Table Exists in the DB.
var TblExists = db4.execute("SELECT Table_Name FROM information_schema.tables Where Table_Name = '" + items[i].getColumnText(2) + "'");
if (TblExists.hasNext())
{
//Table Exists
info = db4.describeTable(items[i].getColumnText(2));
//Create a variable with all the field names for the dynamic SQL query.
for (var e = 0; e < info.length; ++e)
{
name = info[e].name;
choiceboxFieldNames.addItem(name.toUpperCase());
}
}
else
{
//Table Does Not Exists, Loop to the next Table Name in the list.
alert("Table: " + items[i].getColumnText(2) + " does not exist in " + items[i].getColumnText(0) + "/" + items[i].getColumnText(1));
continue;
}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: sort list in choicebox

Postby Ben on Fri Aug 10, 2012 2:20 pm

Use an ORDER BY clause in your SQL query. You can also use standard JavaScript functions to sort your result array. The SQL way is probably the best in this case.
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: sort list in choicebox

Postby abenitez77 on Fri Aug 10, 2012 3:26 pm

Sorting the SQL query won't have any affect in my results. I am just using that to see if it exists. See I am using "info = db4.describeTable(items[i].getColumnText(2));" to get the field names. how do i sort that?
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: sort list in choicebox

Postby Ben on Sat Aug 11, 2012 5:01 pm

Put the table names into an array and then call Array.sort()
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: sort list in choicebox

Postby abenitez77 on Mon Aug 13, 2012 9:20 am

Tried that ...didn't work...see my code below.

function onButton5Clicked()
{
var db4
var i
var info = new Array;
//var info
var name

choiceboxFieldNames.clear();

// Multicolumn list selections
// Create Array of selected items in list.
var items = m_list.getSelectedItems();

for (var i in items)
{
// Create the connection
db4 = new DbConnection("mssql://" + items[i].getColumnText(0) + ":1434/" + items[i].getColumnText(1));
if (db4.isConnected())
{
//print("Connection Successful on - mssql://" + items[i].getColumnText(0) + ":1434/" + items[i].getColumnText(1));
//break;
}
else
{
print("Connection failed on - mssql://" + items[i].getColumnText(0) + ":1434/" + items[i].getColumnText(1));
continue;
}

// Check to make sure Table Exists in the DB.
var TblExists = db4.execute("SELECT Table_Name FROM information_schema.tables Where Table_Name = '" + items[i].getColumnText(2) + "'");
if (TblExists.hasNext())
{
//Table Exists
info = db4.describeTable(items[i].getColumnText(2));
info.sort();
//Create a variable with all the field names for the dynamic SQL query.
for (var e = 0; e < info.length; ++e)
{
name = info[e].name;
choiceboxFieldNames.addItem(name.toUpperCase());
}
}
else
{
//Table Does Not Exists, Loop to the next Table Name in the list.
alert("Table: " + items[i].getColumnText(2) + " does not exist in " + items[i].getColumnText(0) + "/" + items[i].getColumnText(1));
continue;
}

}
HostApp.refresh();

}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: sort list in choicebox

Postby abenitez77 on Mon Aug 13, 2012 9:36 am

Ok, I think i figured it out. This is working for me.

// Check to make sure Table Exists in the DB.
var TblExists = db4.execute("SELECT Table_Name FROM information_schema.tables Where Table_Name = '" + items[i].getColumnText(2) + "'");
if (TblExists.hasNext())
{
// Get the Field Names from a Table.
var flds = db4.describeTable(items[i].getColumnText(2));

// Put the field names into an Array.
for (var a = 0; a < flds.length; ++a)
{
ArrFlds[a] = flds[a].name;
}
// Sort the Array.
ArrFlds.sort();

//Put the field names from the Array into the choicebox for FieldNames to select from.
for (var e = 0; e < ArrFlds.length; ++e)
{
name = ArrFlds[e];
choiceboxFieldNames.addItem(name.toUpperCase());
}
}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: sort list in choicebox

Postby Ben on Mon Aug 13, 2012 6:16 pm

Yes.
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: sort list in choicebox

Postby abenitez77 on Mon Dec 03, 2012 2:53 pm

I am able to add field names to a choicebox from a listbox. Now, I want to be able to select 2 different tables in my listbox and get only the fieldnames added to the choicebox that exists in both tables. How can I edit my code to make this work?

code:
//function to connect to server and retrieve Field Names for dropdown.
function onButton5Clicked()
{
var db4
var i
var ArrFlds = new Array;
//var info
var name

choiceboxFieldNames.clear();

// Multicolumn list selections
// Create Array of selected items in list.
var items = m_list.getSelectedItems();

for (var i in items)
{
// Create the connection
db4 = new DbConnection("mssql://" + items[i].getColumnText(0) + ":1434/" + items[i].getColumnText(1));
if (db4.isConnected())
{
//print("Connection Successful on - mssql://" + items[i].getColumnText(0) + ":1434/" + items[i].getColumnText(1));
//break;
}
else
{
print("Connection failed on - mssql://" + items[i].getColumnText(0) + ":1434/" + items[i].getColumnText(1));
continue;
}

// Check to make sure Table Exists in the DB.
var TblExists = db4.execute("SELECT Table_Name FROM information_schema.tables Where Table_Name = '" + items[i].getColumnText(2) + "'");
if (TblExists.hasNext())
{
// Get the Field Names from a Table.
var flds = db4.describeTable(items[i].getColumnText(2));

// Put the field names into an Array.
for (var a = 0; a < flds.length; ++a)
{
ArrFlds[a] = flds[a].name;
}
// Sort the Array.
ArrFlds.sort();

//Put the field names from the Array into the choicebox for FieldNames to select from.
for (var e = 0; e < ArrFlds.length; ++e)
{
name = ArrFlds[e];
choiceboxFieldNames.addItem(name.toUpperCase());
}
}
else
{
//Table Does Not Exists, Loop to the next Table Name in the list.
alert("Table: " + items[i].getColumnText(2) + " does not exist in " + items[i].getColumnText(0) + "/" + items[i].getColumnText(1));
continue;
}

}
HostApp.refresh();

}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Return to Strata Help & Feedback