简体   繁体   English

延迟加载SELECT元素选项w / jquerymobile,c#和asp.net

[英]Lazy loading SELECT element options w/ jquerymobile, c# and asp.net

I have a SELECT element on one of my jQuery Mobile pages that has a lot of possible values. 我的一个jQuery Mobile页面上有一个SELECT元素,它有很多可能的值。 Obviously loading all of the options on page load raises performance issues on mobile handsets. 显然,在页面加载时加载所有选项会增加移动手机的性能问题。 What is a good way to load items "on demand"? 什么是“按需”加载项目的好方法?

An example of what I need is how the Android market loads app lists: x number of items load initially, then x more items load once you've scrolled to the bottom of the options, then x more...and so on). 我需要的一个例子是Android市场如何加载应用程序列表:最初加载x个项目,然后在滚动到选项底部后再加载x个项目,然后x更多......依此类推)。

I'm using C#/ASP.NET (Razor syntax) to implement jQuery Mobile. 我正在使用C#/ ASP.NET(Razor语法)来实现jQuery Mobile。

Here's my solution. 这是我的解决方案。 The idea is to implement a kind of Twitter-like pagination and that you should render some choices from the beginning. 这个想法是实现一种类似Twitter的分页,你应该从一开始就做出一些选择。

<div data-role="page">

    <div data-role="header">
        <h1>Page Title</h1>
    </div><!-- /header -->

    <div data-role="content">   
        <p>Page content goes here.</p>

        <div data-role="fieldcontain">
            <label for="select-choice-1" class="select">Choose shipping method:</label>
            <select name="select-choice-1" id="select-choice-1">
                <option value="standard">Standard: 7 day</option>
                <option value="rush">Rush: 3 days</option>
                <option value="express">Express: next day</option>
                <option value="overnight">Overnight</option>
                <option value="-1">More...</option>
            </select>
        </div>

        </div><!-- /content -->

    <div data-role="footer">
        <h4>Page Footer</h4>
    </div><!-- /footer -->
</div><!-- /page -->

Then hook some handlers to the More option 然后将一些处理程序挂钩到More选项

<script type="text/javascript">
    $(document).bind("pageshow", function(){
        bindMore();
    });

    function bindMore(){
        // The rendered select menu will add "-menu" to the select id
        $("#select-choice-1-menu li").last().click(function(e){handleMore(this, e)});
    }

    function handleMore(source, e){

        e.stopPropagation();

        var $this = $(source);

        $this.unbind();

        $this.find("a").text("Loading...");

        // Get more results
        $.ajax({
            url: "test.js",
            dataType: "script",
            success: function(data){
                $(eval(data)).each(function(){

                    // Add options to underlaying select
                    $("#select-choice-1 option").last()
                        .before($("<option></option>")
                        .attr("value", this.value)
                        .text(this.text)); 

                });

                // Refresh the selectmenu
                $('#select-choice-1').selectmenu('refresh');

                // Rebind the More button
                bindMore();

            }
        });
    }
</script>

Test.js contains this: Test.js包含这个:

[
        {"value": "1", "text": "1"},
        {"value": "2", "text": "2"},
        {"value": "3", "text": "3"}
]

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

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