简体   繁体   English

将默认焦点设置为网格列表中的第一项

[英]Set default focus on first item in grid list

Once a grid is rendered how can I set focus to the first item. 呈现网格后,如何将焦点设置到第一项。 I am running into a problem where when the grid is updated (collection changes) then focus is lost for the entire application . 我遇到了一个问题,当网格更新(集合更改)时,整个应用程序失去焦点。

I am using the moonstone library. 我正在使用月光石图书馆。

 { 
    kind: "ameba.DataGridList", name: "gridList", fit: true, spacing: 20, minWidth: 300, minHeight: 270, spotlight : 'container',
    scrollerOptions: 
    { 
       kind: "moon.Scroller", vertical:"scroll", horizontal: "hidden", spotlightPagingControls: true
    }, 
    components: [
       {kind : "custom.GridItemControl", spotlight: true}
   ]
 }

hightlight() is a private method for Spotlight which only adds the spotlight class but does not do the rest of the Spotlight plumbing. hightlight()是Spotlight的私有方法,它仅添加spotlight类,而不执行其余的Spotlight管道。 spot() is the method you should be using which calls highlight() internally. spot()是您应该使用的内部调用highlight()

@ruben-ray-vreeken is correct that DataList defers rendering. @ ruben-ray-vreeken是正确的,因为DataList可以延迟渲染。 The easiest way to spot the first control is to set initialFocusIndex provided by moon.DataListSpotlightSupport. 发现第一个控件的最简单方法是设置initialFocusIndex提供的initialFocusIndex。

http://jsfiddle.net/dcw7rr7r/1/ http://jsfiddle.net/dcw7rr7r/1/

enyo.kind({
    name: 'ex.App',
    classes: 'moon',
    bindings: [
        {from: ".collection", to: ".$.gridList.collection"}
    ],
    components: [
        {name: 'gridList', kind:"moon.DataGridList", classes: 'enyo-fit', initialFocusIndex: 0, components: [
            {kind:"moon.CheckboxItem", bindings: [
                {from:".model.text", to:".content"},
                {from:".model.selected", to: ".checked", oneWay: false}
            ]}
        ]}
    ],
    create: enyo.inherit(function (sup) {
        return function () {
            sup.apply(this, arguments);

            // here, at least, the app starts in pointer mode so spotting the first control
            // isn't apparent (though it would resume from that control upon 5-way action).
            // Turning off pointer mode does the trick.
            enyo.Spotlight.setPointerMode(false);
            this.set("collection", new enyo.Collection(this.generateRecords()));
        };
    }),
    generateRecords: function () {
        var records = [],
            idx     = this.modelIndex || 0;
        for (; records.length < 20; ++idx) {
            var title = (idx % 8 === 0) ? " with long title" : "";
            var subTitle = (idx % 8 === 0) ? "Lorem ipsum dolor sit amet" : "Subtitle";
            records.push({
                selected: false,
                text: "Item " + idx + title,
                subText: subTitle,
                url: "http://placehold.it/300x300/" + Math.floor(Math.random()*0x1000000).toString(16) + "/ffffff&text=Image " + idx
            });
        }
        // update our internal index so it will always generate unique values
        this.modelIndex = idx;
        return records;
    },
});

new ex.App().renderInto(document.body);

Just guessing here as I don't use Moonstone, but the plain enyo.Datalist doesn't actually render its list content when the render method is called. 只是在这里猜测,因为我不使用Moonstone,但是当调用render方法时,普通的enyo.Datalist实际上并不呈现其列表内容。 Instead the actual rendering task is deferred and executed at a later point by the gridList's delegate. 相反,实际的渲染任务由gridList的委托推迟并在以后执行。

You might want to dive into the code of the gridList's delegate and check out how it works. 您可能想深入了解gridList的委托的代码,并查看其工作方式。 You could probably create a custom delegate (by extending the original) and highlight the first child in the reset and/or refresh methods: 您可能可以创建一个自定义委托(通过扩展原始委托),并在reset和/或refresh方法中突出显示第一个孩子:

enyo.Spotlight.highlight(this.$.gridList.childForIndex(0));

Ruben's answer adds on to my answer that I posted in the Enyo forums, which I'm including here for the sake of completeness: 鲁宾的答案是我在Enyo论坛上发布的答案的补充,为了完整起见,我将其包括在此处:

Try using 尝试使用

enyo.Spotlight.highlight(this.$.gridList.childForIndex(0));

I added a rendered() function to the Moonstone DataGridList sample in the Sampler and I found that this works: 我在Sampler的Moonstone DataGridList示例中添加了render()函数,发现这可行:

rendered: function() {
    this.inherited(arguments);
    this.startJob("waitforit", function() {
        enyo.Spotlight.highlight(this.$.gridList.childForIndex(0));
    }, 400);
},

It didn't work without the delay. 没有拖延,它是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM