Hopefully this is easy but that sometimes means its impossible in flex and I have searched quite a bit to no avail.
希望这很容易,但有时候这意味着它在弹性方面是不可能的,而且我已经搜索了很多但无济于事。
Say I have a list (LIST#1) of artists:
说我有一个艺术家名单(列表#1):
2Pac Adele Amerie Beyonce Jason Aldean Shakira The Trews
2Pac Adele Amerie Beyonce Jason Aldean Shakira The Trews
I also have a list (LIST#2) that has the values #,A-Z - how would I create an alphabet jump?
我还有一个列表(列表#2),其值为#,A-Z - 我将如何创建字母跳转?
So If a user clicked on "A" in LIST#2 that would automatically scroll to "Adele" at the top of LIST#1 - not filter so he/she could scroll up to view 2Pac or down to view The Tews if they were not in the view yet.
因此,如果用户点击列表#2中的“A”,它将自动滚动到列表#1顶部的“阿黛尔” - 不过滤,这样他/她可以向上滚动查看2Pac或向下查看The Tews是否是还没在视图中。
Its a standard Flex Spark List with an ArrayCollection as the dataProvider - the artist field is called: "title" along with a unique id field that is not visible to the user.
它是一个标准的Flex Spark List,ArrayCollection作为dataProvider - 艺术家字段被称为:“title”以及用户不可见的唯一id字段。
Thanks!
谢谢!
Please see comments on marker answer for discussion on Dictionary that may be faster in some cases. See below for code (HAVE NOT CONFIRMED ITS FASTER! PLEASE TEST):
有关字典的讨论,请参阅有关标记答案的评论,在某些情况下可能会更快。请参阅下面的代码(尚未确认其更快!请测试):
private function alphabet_listChange(evt:IndexChangeEvent) : void {
var letter:String;
letter = evt.currentTarget.selectedItems[0].toString();
trace(currentDictionary[letter]);
ui_lstLibraryList.ensureIndexIsVisible(currentDictionary[letter]);
}
public function createAlphabetJumpDictionary() : Dictionary {
//alphabetArray is a class level array containing, A-Z;
//alphabetDictionary is a class level dictionary that indexes A-z so alphabetDictionary["A"] = 0 and ["X"] = 25
var currentIndexDict:Dictionary = new Dictionary; //Dictionary is like an array - just indexed for quick searches - limited to key & element
var searchArray:Array = new Array;
searchArray = currentArrayCollection.source; //currentArrayCollection is the main array of objects that contains the titles.
var currentIndex:Number; //Current index of interation
var currentAlphabetIndex:Number = 0; //Current index of alphabet
for (currentIndex = 0; currentIndex < searchArray.length; currentIndex++) {
var titleFirstLetter:String = searchArray[currentIndex].title.toString().toUpperCase().charAt(0);
if (titleFirstLetter == alphabetArray[currentAlphabetIndex]) {
currentIndexDict[titleFirstLetter] = currentIndex;
trace(titleFirstLetter + " - " + currentIndex);
currentAlphabetIndex++;
} else if (alphabetDictionary[titleFirstLetter] > alphabetDictionary[alphabetArray[currentAlphabetIndex]]) {
trace(titleFirstLetter + " - " + currentIndex);
currentIndexDict[titleFirstLetter] = currentIndex;
currentAlphabetIndex = Number(alphabetDictionary[titleFirstLetter] + 1);
}
}
return currentIndexDict;
}
private function build_alphabeticalArray() : Array {
var alphabetList:String;
alphabetList = "A.B.C.D.E.F.G.H.I.J.K.L.M.N.O.P.Q.R.S.T.U.V.W.X.Y.Z";
alphabetArray = new Array;
alphabetArray = alphabetList.split(".");
return alphabetArray;
}
private function build_alphabetDictionary() : Dictionary {
var tmpAlphabetDictionary:Dictionary = new Dictionary;
for (var i:int=0; i < alphabetArray.length; i++) {
tmpAlphabetDictionary[alphabetArray[i]] = i;
trace(alphabetArray[i] + " - " + i);
}
return tmpAlphabetDictionary;
}
private function buildCurrentDictionary() : void {
trace("Collection Changed");
currentDictionary = new Dictionary;
currentDictionary = createAlphabetJumpDictionary();
}
2 个解决方案
#1
1
The Flex Spark list has a very convenient method called ensureIndexIsVisible(index)
. Check the Flex reference documentation. All you have to do is to find the index of the first artist for the corresponding selected alphabet letter:
Flex Spark列表有一个非常方便的方法,名为ensureIndexIsVisible(index)。查看Flex参考文档。您所要做的就是找到相应选定字母的第一位艺术家的索引:
public function findAlphabetJumpIndex():Number
{
var jumpToIndex:Number;
var selectedLetter:String = alphabethList.selectedItem;
for (var i:int=0; i < artists.length; i++)
{
var artistName:String = artists.getItemAt(i);
var artistFirstLetter:String = artistName.toUpperCase().charAt(0);
if (artistFirstLetter == selectedLetter)
{
jumpToIndex = i;
break;
}
}
return jumpToIndex;
}
#2
0
You can iterate your artist list data provider and check if artist name starts with selected alphabet from list two. When corresponding artist is found, set artist list selected index a value what you get from iterating data.
您可以迭代您的艺术家列表数据提供者,并检查艺术家姓名是否以列表二中的选定字母开头。找到相应的艺术家后,设置艺术家列表选择索引的值是从迭代数据中获得的值。
#1
1
The Flex Spark list has a very convenient method called ensureIndexIsVisible(index)
. Check the Flex reference documentation. All you have to do is to find the index of the first artist for the corresponding selected alphabet letter:
Flex Spark列表有一个非常方便的方法,名为ensureIndexIsVisible(index)。查看Flex参考文档。您所要做的就是找到相应选定字母的第一位艺术家的索引:
public function findAlphabetJumpIndex():Number
{
var jumpToIndex:Number;
var selectedLetter:String = alphabethList.selectedItem;
for (var i:int=0; i < artists.length; i++)
{
var artistName:String = artists.getItemAt(i);
var artistFirstLetter:String = artistName.toUpperCase().charAt(0);
if (artistFirstLetter == selectedLetter)
{
jumpToIndex = i;
break;
}
}
return jumpToIndex;
}
#2
0
You can iterate your artist list data provider and check if artist name starts with selected alphabet from list two. When corresponding artist is found, set artist list selected index a value what you get from iterating data.
您可以迭代您的艺术家列表数据提供者,并检查艺术家姓名是否以列表二中的选定字母开头。找到相应的艺术家后,设置艺术家列表选择索引的值是从迭代数据中获得的值。