Tianium JS:是否可以使用数据绑定在ListView中按字母顺序对集合进行分组?

Tianium JS:是否可以使用数据绑定在ListView中按字母顺序对集合进行分组?,listview,titanium,titanium-mobile,titanium-alloy,Listview,Titanium,Titanium Mobile,Titanium Alloy,我正在从TableView迁移到Titanium中的ListView。新的要求之一是将列表按字母顺序分组到ListSection中。我可以想出一种方法,仅使用Javascript就可以做到这一点,方法是生成ListView数据,然后将每个数据作为ListSection中的字母组输出 然而,我正试图找到一种使用钛合金和数据绑定来实现这一点的方法,但我在网上找不到任何例子,甚至在钛合金厨房水槽中也找不到 以下是迄今为止我所掌握的,我可以在一个ListSection中获得数据输出: <L

我正在从TableView迁移到Titanium中的ListView。新的要求之一是将列表按字母顺序分组到ListSection中。我可以想出一种方法,仅使用Javascript就可以做到这一点,方法是生成ListView数据,然后将每个数据作为ListSection中的字母组输出

然而,我正试图找到一种使用钛合金和数据绑定来实现这一点的方法,但我在网上找不到任何例子,甚至在钛合金厨房水槽中也找不到

以下是迄今为止我所掌握的,我可以在一个ListSection中获得数据输出:

    <ListView id="brandList">

        <Templates>
            <ItemTemplate name="template" id="template">
                <View>
                   <ImageView bindId="image" class="programmeImage" />
                   <Label bindId="name" class="programmeTitle" />
                   <ImageView bindId="channel" class="channelBrand" />
                </View>
            </ItemTemplate>
        </Templates>

        <ListSection dataCollection="brands">
            <ListItem template="template" name:text="{name}" image:image="{cached_image}" channel:image="{channel}" />
        </ListSection>

    </ListView>

这输出的列表很好,数据绑定也很好,但我不知道如何为每个字母组创建一个列表部分,如下图所示。这甚至可以用合金吗

例如:


您可以使用Alloy进行此操作,但您必须在控制器中使用创建节和索引

<Alloy>
<Window>
    <ListView id="list" defaultItemTemplate="title">
        <Templates>
            <ItemTemplate name="title" height="50">
                <Label bindId="title" class="title"/>
            </ItemTemplate>
        </Templates>
    </ListView>
</Window>
</Alloy>
视图/index.xml

在XML中定义列表视图及其模板。我们在这里不进行数据绑定,因为我们需要在控制器中定义节

<Alloy>
<Window>
    <ListView id="list" defaultItemTemplate="title">
        <Templates>
            <ItemTemplate name="title" height="50">
                <Label bindId="title" class="title"/>
            </ItemTemplate>
        </Templates>
    </ListView>
</Window>
</Alloy>
控制器/index.js

下面是我如何创建功能的。我循环遍历一个字母数组,这将是我的索引,如果有以该字母开头的名称,则为每个字母创建节

var info = Alloy.createCollection('info');
info.fetch();

var sections = [];
var sectionIndexArray = [];
var letters = ['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' ];
_.each(letters, function(letter) {
    var data = info.filter(function(name) {
        var n = name.get('title');
        if(n.charAt(0).toLowerCase() === letter) {
            return name;
        }
    });
    if(data && data.length >0 ) {
        var section = Ti.UI.createListSection();
        var items = _.map(data, function(item) {
            return { title: {text: item.get('title')}};
        });

        section.setItems(items);
        sections.push(section);
        sectionIndexArray.push({index: (sections.length -1 ), title:     letter.toUpperCase() });
    }
});

$.list.sections = sections;
$.list.sectionIndexTitles = sectionIndexArray;

$.index.open();
最后,您需要填充您的集合。为了进行测试,我在alloy.js文件中添加了以下内容。它可能在index.js中

var firstLadies = [
    "Adams, Abigail",
    "Adams, Louisa Catherine",
    "Arthur, Ellen Herndon",
    "Bush, Barbara",
    "Bush, Laura",
    "Carter, Rosalynn",
    "Cleveland, Frances Folsom",
    "Clinton, Hillary Rodham",
    "Coolidge, Grace Goodhue",
    "Eisenhower, Mamie Doud",
    "Fillmore, Abigail Powers",
    "Ford, Betty",
    "Garfield, Lucretia Rudolph",
    "Grant, Julia Dent",
    "Harding, Florence Kling",
    "Harrison, Anna Tuthill Symmes",
    "Harrison, Caroline Lavinia Scott",
    "Harrison, Mary Lord",
    "Hayes, Lucy Webb",
    "Hoover, Lou Henry",
    "Jackson, Rachel",
    "Johnson, Eliza McCardle",
    "Johnson, Lady Bird",
    "Kennedy, Jacqueline",
    "Lincoln, Mary Todd",
    "Madison, Dolley",
    "McKinley, Ida Saxton",
    "Nixon, Pat",
    "Obama, Michelle",
    "Pierce, Jane Means",
    "Polk, Sarah Childress",
    "Reagan, Nancy",
    "Roosevelt, Edith Kermit Carow",
    "Roosevelt, Eleanor",
    "Taft, Helen Herron",
    "Truman, Bess Wallace",
    "Van Buren, Hannah Hoes",
    "Washington, Martha",
    "Wilson, Edith Bolling Galt",
    "Wilson, Ellen Axson"
];

if (!Ti.App.Properties.hasProperty('seeded')) {
    for(var i=0,j=firstLadies.length;i<j;i++) {
        Alloy.createModel('info', { title: firstLadies[i]}).save();
    }
    Ti.App.Properties.setString('seeded', 'yes');
}
var第一夫人=[
“亚当斯,阿比盖尔”,
“亚当斯,路易莎·凯瑟琳”,
“亚瑟,艾伦·赫恩登”,
“布什,芭芭拉”,
“布什,劳拉”,
“卡特,罗莎琳”,
“克利夫兰,弗朗西斯·福尔松”,
“克林顿,希拉里·罗德姆”,
“柯立芝,格雷斯·古德休”,
“艾森豪威尔,梅米·杜德”,
“菲尔莫,阿比盖尔·鲍尔斯”,
“福特,贝蒂”,
“加菲猫,鲁道夫”,
“格兰特,朱莉娅·登特”,
“哈丁,弗洛伦斯·克林”,
“哈里森,安娜·塔希尔·西姆斯”,
“哈里森,卡罗琳·拉维尼亚·斯科特”,
“哈里森,玛丽·洛德”,
“海斯,露西·韦伯”,
“胡佛,卢·亨利”,
“杰克逊,瑞秋”,
“约翰逊,伊丽莎·麦卡德尔”,
“约翰逊,小鸟女士”,
“肯尼迪,杰奎琳”,
“林肯,玛丽·托德”,
“麦迪逊,多利”,
“麦金利,艾达·萨克斯顿”,
“尼克松,帕特”,
“奥巴马,米歇尔”,
“皮尔斯,简的意思是”,
“波尔克,莎拉·奇德里斯”,
“里根,南希”,
“罗斯福,伊迪丝·克米特·卡洛”,
“罗斯福,埃莉诺”,
“塔夫脱,海伦·赫伦”,
“杜鲁门,贝丝·华莱士”,
“范布伦,汉娜·霍斯”,
“华盛顿,玛莎”,
“威尔逊,伊迪丝·博林·高尔特”,
“威尔逊,艾伦·阿克森”
];
如果(!Ti.App.Properties.hasProperty('seeded')){

for(var i=0,j=firstLadies.length;iGreat!这正是我需要的!!:)