Calin M
Calin M

Reputation: 45

issues with dynamically generated options for select

So I have a simple page (html) where using jquery I load 's into a select using a php file. The code is as follows: The HTML:

<div data-role="page" id="pg_invoicedtl">
  <header data-role="header" data-theme="b">
    <a href="javascript:history.go(0)" data-icon="refresh" class="ui-btn-right" data-theme="e">Refresh</a>
    <h1>MyApp</h1>
  </header>

    <div align="center" data-role="content">
        <ul data-role="listview" data-theme="c" data-divider-theme="e" data-count-theme="b" data-inset="true">
            <li data-role="list-divider">
                <h3 class="ui-li-heading">Select product</h3>
            </li>            

            <li>
            <div data-role="fieldcontain">
                <label for="prodselect" class="select">Choose the product:</label>
                <select name="prodselect" id="prodselect">
                </select>
            </div>            
            </li>
        </ul>
    </div>
</div>

The script (located of course in the HEAD section) that I use to populate the select is:

<script type="text/javascript">
        $(document).ready(function() {
            $.ajaxSetup ({
                cache: false
            });

            var ajaxLoader = '<img src="images/ajax-loader.gif" alt="loading.." />'; 
            var loadUrl = "loadproducts.php";  


            $('#prodselect').toggle('fast', function() {
                $(this).html(ajaxLoader);
                $(this).toggle('fast', function() {
                    $.get(loadUrl, function(data){
                        $('#prodselect').html(data);
                    },'html');                  
                });
            });
            $('#prodselect').selectmenu('refresh');
        });        
</script>    

And now the php file (loadproducts.php) that the script is calling:

<?php
session_start();
include "/tmp/conexiune.php";

$pql = mysql_query("select * from products order by name");
$isfirst = 0;
while ($prow = mysql_fetch_row($pql)){
    if ( $isfirst == 0){
        echo '<option id="'.$prow['idproducts'].'"value="'.$prow['name'].'" selected="selected">'.$prow['name'].' - '.$prow['pu'].' EUR</option>';
    }
    else
    {
        echo '<option id="'.$prow['idproducts'].'"value="'.$prow['name'].'">'.$prow['name'].' - '.$prow['pu'].' EUR</option>';
    }
    $isfirst++;
}    
?>

First I must tell you that the above code works. With one problem though... My problem...:

Upvotes: 1

Views: 1571

Answers (2)

Kiran Ruth R
Kiran Ruth R

Reputation: 1012

Try this :

 $('#prodselect').selectmenu('refresh', true);

After loading dynamic content you need to force rebuild the select as mentioned in the document : http://api.jquerymobile.com/selectmenu/#method-refresh

Upvotes: 0

Rwd
Rwd

Reputation: 35200

Try using the === (note 3) rather than ==.

If this does not work then change:

$isfirst = 0;

to

$isfirst = 1;

As 0 can evaluate as false in cases.

Hope this helps!

Upvotes: 0

Related Questions