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 #.
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.