Support

Account

Home Forums Backend Issues (wp-admin) Select Field with advanced ‘logic’, groups and search

Helping

Select Field with advanced ‘logic’, groups and search

  • First i will try to explain my actual code situation and the resulting problem:

    In my application i use a group of three select fields for “text content”.

    field 1 for title
    field 2 for subtitle
    field 3 for details

    All fields use the same choices base. To avoid that the user males the same selection in two or more fields of the group i use the ACG JavaScript API with ‘select2_ajax_results’ and disable the option if it’s selected in another field of the group.

    This works like a charm.

    else if(instance.data.field.data.key === 'field_dpf_settings_presentation_label_subtitle_content') {
    var pf_options = json.results;
    $(pf_options).each(function() {
    if($.inArray(this.id.toString(), dpf_title.val()) !== -1) {
    this.disabled = true;
    this.text = this.text + ' (' + acf.__('used in title') + ')';
    }
    else if($.inArray(this.id.toString(), dpf_details.val()) !== -1) {
    this.disabled = true;
    this.text = this.text + ' (' + acf.__('Used in details') + ')';
    }
    else {
    this.disabled = false;
    }
    });
    }

    Now i tried to get “optgroups” working and all hacks and tips here in the forum doesn’t work because it depends and static values and not possible with AJAX.

    So i decided to extend my JS code and “rebuild” the json.result array to include the optgroups. Also this i could get working.

    var pf_options = json.results;
    var neu = [];
    var counter = -1;
    $(pf_options).each(function() {
    var element = this.id;
    if ( element.search('#') !== -1 ) {
    element = element.replace("#", "");
    neu.push( { 'text' : this.text, 'children' : [] } );
    counter++;
    } else {
    id = this.id;
    text = this.text;
    neu[counter]['children'].push( { 'id' : id, 'text' : text } );
    }
    });

    The result are optgroups – OK – Now i tried to serach for an element and now i get an JS error in translation and no result and only no result in untranslated variant. Always the “children” element gets a reading error and value to undefined.

    Could someone help?

    And the end i have to say that it was more than good if an optgroup would be possible by default after more than ten years of questions about this here. But for me it would work with a hacking tip also.

    Regards André

  • I will post the code today later again. sorry for that.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.