Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding elements to Listview in jqueryMobile

I have a listview. I need to add and remove from the list. On adding to the list, the jquery mobile styling does not get added to the new content.

<ul data-role="listview" id="contributionList">
   <li id="l1"><a>5.00</a><a data-icon="delete" data-role="button" id="1"></a></li>
   <li><a>10.00</a><a data-icon="delete" data-role="button"></a></li>
   <li><a>15.00</a><a data-icon="delete" data-role="button"></a></li>
   <li><a>20.00</a><a data-icon="delete" data-role="button"></a></li>
   <li><a>25.00</a><a data-icon="delete" data-role="button"></a></li>
   <li><a>50.00</a><a data-icon="delete" data-role="button"></a></li>
   <li><a>100.00</a><a data-icon="delete" data-role="button"></a></li> 
</ul>

I have a fieldset to add amounts to the list.

<fieldset class="ui-grid-a">
   <div class="ui-block-a">
      <input type="text" placeholder="Add new Amount" id="contributionAmount" />
   </div>
   <div class="ui-block-b">
     <input type="button" value="Add" id="addContribution"/>
   </div>
</fieldset>

I am using the append function to end other amounts that are added to the list. The amount gets added, but the styling (i.e. jquery mobile) classes do not get applied to the new added amount. Can someone tell me on how to overcome the problem.

like image 269
Hozefa Avatar asked Dec 15 '11 18:12

Hozefa


People also ask

What is the difference between jQuery and jQuery Mobile?

jQuery is a DOM manipulating/traversing and AJAX JavaScript framework. It abstracts out a lot of the complexity between the different browsers automatically. There are countless jQuery plugins that simplify many task. jQuery Mobile is a UI framework geared to mobile applications that is built on jQuery.

What is data role in jQuery Mobile?

Pages & DialogsA page in jQuery Mobile consists of an element with a data-role="page" attribute. Within the "page" container, any valid HTML markup can be used, but for typical pages in jQuery Mobile, the immediate children of a "page" are divs with data-role="header" , class="ui-content" , and data-role="footer" .

Is used to create a dropdown in jQuery Mobile?

The <select> element creates a drop-down list with several options.


2 Answers

Got it working:

  • http://jsfiddle.net/NXrRp/10/

JS

$('.deleteMe').live('click', function(){
    $(this).parent().remove();
    $('#contributionList').listview('refresh');
});

$('#addContribution').click(function() {
    var newAmount = $('#contributionAmount').val();

    if(newAmount != '') {
        $('#contributionList').append('<li><a>' + newAmount + '</a><a class="deleteMe"></a></li>').listview('refresh');
        $('#contributionAmount').val('');
    } else {
        alert('Nothing to add');   
    }
});

HTML

<div data-role="page" id="home">
    <div data-role="content">
        <ul data-role="listview" id="contributionList" data-split-icon="delete" data-split-theme="d">
           <li id="l1"><a>5.00</a><a id="1" class="deleteMe"></a></li>
           <li><a>10.00</a><a class="deleteMe"></a></li>
           <li><a>15.00</a><a class="deleteMe"></a></li>
           <li><a>20.00</a><a class="deleteMe"></a></li>
           <li><a>25.00</a><a class="deleteMe"></a></li>
           <li><a>50.00</a><a class="deleteMe"></a></li>
           <li><a>100.00</a><a class="deleteMe"></a></li> 
        </ul>
        <br />
        <fieldset class="ui-grid-a">
           <div class="ui-block-a">
              <input type="text" placeholder="Add new Amount" id="contributionAmount" />
           </div>
           <div class="ui-block-b">
             <input type="button" value="Add" id="addContribution"/>
           </div>
        </fieldset>

    </div>
</div>
like image 182
Phill Pafford Avatar answered Oct 31 '22 10:10

Phill Pafford


You have to refresh the listview for jQuery Mobile to add the correct classes to the correct elements in your listview:

$('#addContribution').on('click', function () {
    var amount_val = $('#contributionAmount').val();
    if (amount_val != '') {
        $('#the-listview').append('<li>' + amount_val + '</li>').listview('refresh');
    }
});

Here is a demo: http://jsfiddle.net/PQ39n/1/

Docs for jQuery Mobile listviews: http://jquerymobile.com/demos/1.0/docs/lists/docs-lists.html

EDIT

Phill Pafford brings-up a good point that .on() is new in jQuery 1.7 and the jQuery Mobile team suggest using jQuery 1.6.4 with jQuery Mobile 1.0. In this case .on() is the same as using .bind() so they can be interchanged without issue.

like image 4
Jasper Avatar answered Oct 31 '22 12:10

Jasper