Checkboxes sometimes not being checked after .update()

Checkboxes sometimes not being checked after .update()

dataBdataB Posts: 23Questions: 5Answers: 1

I'm running into a strange bug where checkboxes which should appear checked in the editor window do not. When I close the editor window (changing nothing) and simply click "Edit" again the checkbox is now checked. Anyone else seen this before?

Example below. Notice that in the first screen the second checkbox is unchecked and after closing the Editor window and reopening the checkbox is now checked.

Here is the javascript which updates the checkbox:

function setNewsletterAds (newDate) {
    var date = moment(new Date(newDate)).format('YYYY-MM-DD');
    $.get("/modules/api/getNewsletterAds.php", {sid:$("#sid").val(), date:date}, function(d) {
        if (d[0].result == "success") {
            //console.log(d)
            var options = [];
            for ( i=1; i <= d[0].numads; i++ ) {
                options.push( { value: d[i].id, label: d[i].image_url} );
            }
            newslettereditor.field( 'newsletter_ads_available[].id' ).update( options );
            $.each($(".adCheckboxes").find(":input").next("label"), function() {
                $(this).html('<img src="'+$(this).text()+'">')
            });
            
            if (admin_level < 99) { //disable checking off ads if user is not a super admin
                $('.adCheckboxes .DTE_Field_InputControl input[type="checkbox"]').prop("disabled", "disabled");
            }
        } else {
            alert("There was an error loading the ads for this publish date. Please refresh the page and try again.")
        }
    }, "json" )
}

newslettereditor.field( 'newsletters.publish_date' ).input().on( 'change', function () {
    setNewsletterAds ($(this).val())
})


It works fine other than this strange "checked" bug.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,175Questions: 1Answers: 10,409 Site admin
    Answer ✓

    I don't actually see where the checkbox is being marked as checked in that function. I suspect that the issue is that the old values aren't being cleared, but I'm not very sure to be honest. Can you link to the page showing the issue?

    Allan

  • dataBdataB Posts: 23Questions: 5Answers: 1

    Thanks for your reply Allan! I sent you an email with a login and more info for that page.

    I also suspected old values but couldn't find a way to clear old values. I tried the following but it didn't work (all checkboxes were unchecked):

    newslettereditor.field( 'newsletter_ads_available[].id' ).update( {} );
     for ( i=1; i <= d[0].numads; i++ ) {
        options.push( { value: d[i].id, label: d[i].image_url} );
    }
    newslettereditor.field( 'newsletter_ads_available[].id' ).update( options );
    
    

    As far as how the checkbox gets checked I'm not entirely sure how that works. :#
    I'm getting the checkbox label (the image URL) and displaying it using the following code:

    ->join(
          Mjoin::inst( 'newsletter_ads_available')
            ->link( 'newsletters.id', 'newsletter_ads_shown.newsletter_id' )
            ->link( 'newsletter_ads_available.id', 'newsletter_ads_shown.newsletter_ad_id' )
            ->fields(
                Field::inst( 'id' )
                    ->options( Options::inst()
                        ->table( 'newsletter_ads_available' )
                        ->value( 'id' )
                        ->label( 'image_url' )
                    ),
                Field::inst( 'image_url' )
            )
        )
    
    
    ------------
    
     {
                label: "Ads in Newsletter:",
                name: "newsletter_ads_available[].id",
                type: "checkbox",
                className: 'adCheckboxes',
            }
    
    
    

    Then I use this line of code to turn the label into an image (and CSS to hide the checkbox):

    $.each($(".adCheckboxes").find(":input").next("label"), function() {
                    $(this).html('<img src="'+$(this).text()+'">')
                });
    
This discussion has been closed.