Lazy loading SELECT element parameters with jquerymobile, C # and asp.net

I have a SELECT element on one of my jQuery mobile pages that have many possible values. Obviously, loading all page load options increases performance issues on mobile phones. What is a good way to download on demand products?

An example of what I need is how the Android market loads application lists: first, the number of items is loaded, and then more items are added as soon as you scroll the bottom of the options, then another x ... and so on).

I am using C # / ASP.NET (Razor syntax) to implement jQuery Mobile.

+3
source share
1 answer

. , .

<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 -->

""

<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 :

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

Source: https://habr.com/ru/post/1796120/


All Articles