0

I have an app that has a listview with checkboxes on it. Here is the method that actually draws the listview. (panelListId is the ID of the UL tag that contains the listview.)

/**
 * @param panelListId
 * @param panelPersons
 */
function _buildConfirmAttendanceEntries(panelListId, panelPersons){
    //list the persons on that case
    panelPersons.sort(function(left, right){
        var leftSeqnum = left.originals.panelSequenceNumber;
        var rightSeqnum = right.originals.panelSequenceNumber;

        return leftSeqnum - rightSeqnum;
    });

    var from = -19;
    var to = 0;
    var result = "";
    panelPersons.forEach(function(panelPerson){
        var seqNum = panelPerson.originals.panelSequenceNumber;
        var needsDivider = false;
        while(to < seqNum ){
          from += 20;
          to += 20;
          needsDivider = true;
        }

        if(needsDivider){
          result += "<li data-role=list-divider>" + from + "-" + to + "</li>";
        }

        var listItem = "<li style='min-height: 32px; max-height: 90px; padding-left: 40px;' class='attendee'>";

        var personId = panelPerson.originals.personId;
        var nameId = seqNum + ". ";
        nameId += _formatPersonName(panelPerson);
        nameId += "<input type='hidden' value='" + personId + "'/>";
        nameId += " (" + personId + ")";

        listItem += _buildListContent(panelListId, personId, nameId, panelPerson.originals.awol);
        listItem += "<\li>";

        result += listItem + "\n";
    });

    return result;
}

On the first draw, I call it this way and everything works fine:

    result += "  <ul id='" + panelListId + "' data-role='listview' data-theme='e' data-inset='true' data-filter='true'>\n"

    result += _buildConfirmAttendanceEntries(panelListId, panel.originals.panelPersons);
    result += "  </ul>\n"

When someone clicks on the listview, I remove all of the entries in the list and add all of the entries for the sub-list. When they click on the sub-list, I re-create the original list using this code:

/**
 * @param panelListId
 */
function _refreshPanel(panelListId){
  var parts = panelListId.split("-");
  var panelId = parts[1];
  var doneCallback = function(jsonText){
    var panelList = jQuery.parseJSON(jsonText);
    panelList.forEach(function(panel){
      if(panelId == panel.originals.panelId){
        var listContents = _buildConfirmAttendanceEntries(panelListId, panel.originals.panelPersons);
        jQuery("#" + panelListId).empty().append(listContents).listview( "refresh" );
      }
    });
    var checkboxes = jQuery(".attendCheckbox");
    checkboxes.checkboxradio( "refresh" );
  };

  var failCallback = function(){
    console.log( "error:" + getUnseatedPanelsUrl);
  };

  _loadPanels(doneCallback, failCallback);
}

But this time, checkboxes don't get rendered as jquery-mobile components, but as regular old HTML checkboxes.

Can someone tell me why?

Thom
  • 14,013
  • 25
  • 105
  • 185
  • can the behavior be shown in a jsfiddle at all? – gloomy.penguin Aug 29 '13 at 18:31
  • @gloomy.penguin I don't think so. I'd have to load all of the data out of our web service here at the office. :( – Thom Aug 29 '13 at 18:33
  • when you do `jQuery("#" + panelListId).empty()...` in the refresh code, is that just removing the checkbox items or is it clearing out the whole thing, including the `
  • ` that contained the checkboxes and hidden person info input? (I keep editing this comment, sorry)... I'm just wondering if some styling or css class was removed and not included during the refresh...
  • – gloomy.penguin Aug 29 '13 at 18:33
  • @gloomy.penguin It is removing everything including the
  • entries that contained the checkbox.
  • – Thom Aug 29 '13 at 18:35
  • I feel like this is getting left out somewhere for the refresh: `data-role='listview' data-theme='e'` or maybe a fieldset or something of that nature. I haven't worked with the jquery mobile before, though... so... But if the checkboxes are there correctly and the only thing wrong is they're not "styled" right, then the jquery isn't noticing them appropriately. try [link 1](http://stackoverflow.com/questions/15383531/jquery-mobile-doesnt-apply-style-on-dynamic-checkboxes-and-cant-clicked) or [link 2](http://stackoverflow.com/questions/5663033/jquery-mobile-dynamic-checkboxes-lose-style?rq=1) – gloomy.penguin Aug 29 '13 at 18:39
  • @gloomy.penguin That does make sense, but I can't understand how. I'm using the same ul tag, I'm simply removing the stuff in it and starting over again. – Thom Aug 29 '13 at 18:42
  • If you append new elements, try `.checkboxradio().trigger('create');`. – Omar Aug 30 '13 at 03:06