Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find index number of selected item in Tlist

Status
Not open for further replies.

newbill1

Programmer
Oct 7, 2003
1
CA
I'm trying to delete an item from a Tlist when a user clicks on it. Is there a way to find out what the list index is for the currectly selected item in the list. I want to use DELETE_LIST_ELEMENT but need to know the index #.

Thx,

Bill
 
If the elements in your tlist item are not ordered alphabetically or numerically, you will need to start at 1 and loop round every element in the list until you find the one you want. Note that p_item is the name of the list item in 'block.name' format, and p_value is the element label (or value) you are looking for.

[tt]FUNCTION f_get_index (p_item IN VARCHAR2,
p_value IN VARCHAR2)
RETURN INTEGER IS
l_index INTEGER;
BEGIN
FOR i IN 1..To_Number(Get_List_Element_Count(p_item))
LOOP
IF Get_List_Element_Label(p_item,i) = p_value
THEN
Return i;
END IF;
END LOOP;
--
Return 0;
END;[/tt]

However, if your items are ordered, you can use a much faster binary search like this

[tt]FUNCTION F_Get_Index (p_item IN VARCHAR2,
p_value IN VARCHAR2)
RETURN INTEGER IS
l_top_last INTEGER := Get_List_Element_Count(p_item);
l_index INTEGER := l_top_last / 2;
l_bottom_last INTEGER := 0;
BEGIN
IF l_top_last = 0
THEN
Return 0;
END IF;
--
WHILE Get_List_Element_Label(p_item,l_index) != p_value
AND l_index != l_top_last
AND l_index != l_bottom_last
LOOP
IF p_value < Get_List_Element_Label(p_item,l_index)
THEN
l_top_last := l_index;
l_index := (l_index - l_bottom_last) / 2;
ELSE
l_bottom_last := l_index;
l_index := l_index + ((l_top_last - l_index) / 2);
END IF;
END LOOP;
--
IF Get_List_Element_Label(p_item,l_index) = p_value
THEN
Return l_index;
ELSE
Return 0;
END IF;
END;[/tt]

In both of these cases, the functions will return 0 if they do not find a match for the label value you have entered. The binary search is more powerful if your list item has a large number of rows in it.

If you want to search for the value of the items in the list rather than the labels, use GET_LIST_ELEMENT_VALUE instaed of GET_LIST_ELEMENT_LABEL in the example code.


Let us know how you get on with it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top