简体   繁体   English

如何异步填充选择控件?

[英]How can I asynchronously populate a select control?

I'm working on a project that involves having a select which has a static option at the top, some options pulled from a database, and a static option at the bottom. 我正在开发一个项目,其中涉及一个选择,该选择的顶部具有一个静态选项,一些选项是从数据库中拉出的,而底部具有一个静态选项。 When I try to implement this, I instead end up having the two static options at the top, and then all of the database options below them. 当我尝试实现此功能时,我最终会在顶部拥有两个静态选项,然后在其下方拥有所有数据库选项。

I have taken a look through Stack Overflow, and it looks like I should be using a Promise. 我已经看过Stack Overflow,看来我应该使用Promise。 I'm not familiar with the concept of a Promise, so I've read through various examples and taken a look at a number of websites documenting promises, what they are and how to implement them, and have tried to adapt my project accordingly, however I don't seem to be able to get the structure I want. 我对Promise的概念并不熟悉,因此我通读了多个示例,并查看了许多记录承诺的网站,承诺的内容和实现方式,并尝试相应地调整我的项目,但是我似乎无法获得我想要的结构。

To take a really simplified approach, I set up a really basic web page consisting of an empty select along with a button who's onclick action is to populate the select with three options, who's text values are 1, 2 and 3. If my populate() method simply creates three options and adds them in the correct order, the select list displays them in the correct order. 为了采取一种真正简化的方法,我建立了一个真正的基本网页,其中包含一个空选择以及一个按钮,其onclick动作是使用三个选项填充该选择,其文本值为1、2和3。如果我的populate( )方法仅创建三个选项并以正确的顺序添加它们,选择列表以正确的顺序显示它们。 If I use setTimeout to cause a delay on adding '2', then as expected, the order is 1, 3, 2. I've attempted to use a Promise on '2', however the select is still populated in the order of 1, 3, 2. 如果我使用setTimeout导致添加'2'时出现延迟,则按预期顺序为1、3、2。我试图在'2'上使用Promise,但是仍然按以下顺序填充选择: 1 3 2

Javascript: Javascript:

var select = document.getElementById('numbers');

function populate() {
    putOne();
    putTwo().then(putThree);
}

function putOne() {
    var one = document.createElement('option');
    one.text = 1;
    select.add(one);
}

function putTwo() {
    return new Promise((resolve, reject) => {
        setTimeout(function() {
            var two = document.createElement('option');
            two.text = 2;
            select.add(two);
        },1000)
        resolve();
    }) 
}

function putThree() {
    var three = document.createElement('option');
    three.text = 3;
    select.add(three);
}

HTML 的HTML

<select id="numbers"></select>
<button onclick="populate();">Populate</button>

<script src="promise.js"></script>

I expect the resulting select control to display the options 1, 2 and 3 in that order. 我希望结果选择控件以该顺序显示选项1、2和3。

In putTwo() you are resolving before the time out. putTwo()您要在超时之前进行解析。
If you move the resolve() into the setTimeout callback the options will be ordered as you expect. 如果将resolve()移到setTimeout回调中,则这些选项将按预期顺序排列。

function putTwo() {
    return new Promise((resolve, reject) => {
        setTimeout(function() {
            var two = document.createElement('option');
            two.text = 2;
            select.add(two);
            resolve();
        },1000)
    }) 
}

You should be resolving within your setTimeout 您应该在setTimeout中解决

function putTwo() {
    return new Promise((resolve, reject) => {
        setTimeout(function() {
            var two = document.createElement('option');
            two.text = 2;
            select.add(two);
            resolve();
        },1000)
    }) 
}

Right now, the promise is resolving being the setTimeout in finished. 现在,诺言正在解决setTimeout完成。

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

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