简体   繁体   English

Vue多重选择元素在错误的元素上触发事件

[英]Vue multiselect element triggers event on wrong element

I am using this Vue Multiselect component . 我正在使用Vue Multiselect组件

I have 8 dropdown multi-select elements on my form. 我的表单上有8个下拉的多选元素。

When selecting an option from the second dropdown, the onSearchResellerCompanies method gets triggered, instead of the onSearchAgencyCompanies method. 从第二个下拉菜单中选择一个选项时,将触发onSearchResellerCompanies方法,而不是onSearchAgencyCompanies方法。

Another thing ... 另一件事 ...

On my form, there is a dropdown element with countries. 在我的表单上,国家/地区包含下拉菜单元素。

If I select a reseller company, and if I then select a country and then select an agency company, the onSearchEnumCountries gets triggered, instead of the onSearchAgencyCompanies method. 如果选择代理商公司,然后选择国家/地区然后选择代理公司, onSearchEnumCountries触发onSearchEnumCountries ,而不是onSearchAgencyCompanies方法。

So, in all cases, the @search-change event from the last touched dropdown element gets triggered, instead of the onSearchAgencyCompanies method. 因此,在所有情况下,都会触发最后触摸的下拉元素中的@search-change事件,而不是onSearchAgencyCompanies方法。

This is the html code: 这是html代码:

This is the resellect companies dropdown element: 这是resellect公司下拉菜单元素:

            <multiselect 
            id="multiselect_drop_down_reseller_companies" 
            v-model="drop_down_reseller_companies_selected" 
            track-by="id" 
            label="name" 
            :multiple="false" 
            :options="reseller_companies" 
            :searchable="true" 
            :loading="drop_down_reseller_companies_selectize_isLoading" 
            :placeholder="drop_down_reseller_companies_selectize_placeholder" 
            @select="drop_down_reseller_companies_at_select" 
            :preselectFirst="true" 
            :allowEmpty="false" 
            deselectLabel="Selected" 
            :clearOnSelect="true" 
            @search-change="onSearchResellerCompanies">

                <span slot="noResult">custom no result reseller companies</span>
                <span slot="noOptions">custom no options reseller companies</span>

            </multiselect>    

This is the agency companies dropdown element: 这是代理公司下拉菜单元素:

            <multiselect 
            id="multiselect_drop_down_agency_companies" 
            v-model="drop_down_agency_companies_selected" 
            track-by="id" 
            label="name" 
            :multiple="false" 
            :options="agency_companies" 
            :searchable="true" 
            :loading="drop_down_agency_companies_selectize_isLoading" 
            :placeholder="drop_down_agency_companies_selectize_placeholder" 
            @select="drop_down_agency_companies_at_select" 
            :preselectFirst="true" 
            :allowEmpty="false" 
            deselectLabel="Selected" 
            :clearOnSelect="true" 
            @search-change="onSearchAgencyCompanies">

                <span slot="noResult">custom no result agency companies</span>
                <span slot="noOptions">custom no options agency companies</span>

            </multiselect>

This is the JS code: 这是JS代码:

        /*
        * reseller companies settings
        * start
        */

        drop_down_reseller_companies_selectize_no_result : 'no result',
        drop_down_reseller_companies_selectize_isLoading: false,
        drop_down_reseller_companies_selectize_placeholder : 'type the name of a reseller company ...',
        drop_down_reseller_companies_selected_default : { id: null, name : 'type the name of a reseller company ...' },
        drop_down_reseller_companies_selected : { id: null, name : 'type the name of a reseller company ...' },

        reseller_companies : [],

        /*
        * reseller companies
        * stop
        */

        /*
        * agency companies settings
        * start
        */

        drop_down_agency_companies_selectize_no_result : 'no result',
        drop_down_agency_companies_selectize_isLoading: false,
        drop_down_agency_companies_selectize_placeholder : 'type the name of a agency company ...',
        drop_down_agency_companies_selected_default : { id: null, name : 'type the name of a agency company ...' },
        drop_down_agency_companies_selected : { id: null, name : 'type the name of a agency company ...' },

        agency_companies : [],

        /*
        * agency companies
        * stop
        */

These are the methods : 这些是方法:

This method gets trggered when selecting a reseller company: 选择代理商公司时,此方法会触发:

drop_down_reseller_companies_at_select({id, name}){

    consoleService.log('drop_down_reseller_companies_at_select', true);

    consoleService.log('id', true);
    consoleService.log(id, true);

    consoleService.log('name', true);
    consoleService.log(name, true);

    this.m_record.reseller_id = id;
    // optional setting
    // this.drop_down_reseller_companies_selectize_placeholder = name;
    this.drop_down_reseller_companies_selected = { id: id, name : name };

    this.campaigns = [];
    //this.m_record.campaign_id = this.m_record_default.campaign_id;
    this.m_record.campaign_id = null;

    this.drop_down_client_companies_selected = this.drop_down_client_companies_selected_default;
    this.client_companies = [];
    //this.m_record.client_id = this.m_record_default.client_id;
    this.m_record.client_id = null;

    this.drop_down_agency_companies_selected = this.drop_down_agency_companies_selected_default;
    this.agency_companies = [];
    //this.m_record.agency_id = this.m_record_default.agency_id;
    this.m_record.agency_id = null;

    //consoleService.log('m record', true);
    //consoleService.log(this.m_record, true);        

    this.dynamic_drop_downs_class();

    this.populateDropDownAgencyCompanies(id);

},

This method gets triggered when selecting a agency company: 选择代理公司时会触发此方法:

drop_down_agency_companies_at_select({id, name}){

    consoleService.log('drop_down_agency_companies_at_select', true);

    consoleService.log('id', true);
    consoleService.log(id, true);

    consoleService.log('name', true);
    consoleService.log(name, true);

    this.m_record.agency_id = id;
    // optional setting
    // this.agency_company_selectize_placeholder = name;
    this.drop_down_agency_companies_selected = { id: id, name : name };

    this.campaigns = [];
    //this.m_record.campaign_id = this.m_record_default.campaign_id;
    this.m_record.campaign_id = null;

    this.drop_down_client_companies_selected = this.drop_down_client_companies_selected_default;
    this.client_companies = [];
    //this.m_record.client_id = this.m_record_default.client_id;
    this.m_record.client_id = null;

    //consoleService.log('m record', true);
    //consoleService.log(this.m_record, true);        

    //this.dynamic_drop_downs_class();

    //this.populateDropDownClientCompanies(id);               

},

This method should get triggered only when searching within the reseller dropdown element: 仅当在经销商下拉列表元素中进行搜索时,才应触发此方法:

onSearchResellerCompanies(search, elId){

    consoleService.log('onSearchResellerCompanies', true);

    consoleService.log('element Id', true);
    consoleService.log(elId, true);

    consoleService.log('search', true);
    consoleService.log(search, true);

    if(this.selectize_timestamp){
       clearTimeout(this.selectize_timestamp);
    }

    if(
        search.length >= 2 &&
        search != this.drop_down_reseller_companies_selected.name
    ){

        consoleService.log('onSearchResellerCompanies q >= 2', true);

        this.selectize_timestamp = setTimeout(() => {

            this.reseller_companies_selectize_isLoading = true;

            //axios start
            axios.post(
                apiService.API_URL + '/resellerCompaniesListSelectize',
                {
                    token : this.$store.getters.token,
                    q : search,
                }
            ).then(
                (response) => {

                    this.reseller_companies_selectize_isLoading = false;

                    consoleService.log('administrator form selectize search axios /onSearchResellerCompanies response success', true);
                    consoleService.log(response.data, true);

                    this.reseller_companies = response.data.data;

                }
            ).catch(
                (error) => {

                    // nothing to do here

                    //consoleService.log('administrator form selectize search axios /onSearchResellerCompanies response error', true);
                    //consoleService.log(error, true);

                }
            );
            //axios stop

        }, 500);

    }else{

        consoleService.log('onSearchResellerCompanies NO', true);

    }

},

This method shoud get triggered only when searching within the agency dropdown element: 仅当在代理商下拉列表元素中进行搜索时,才应触发此方法:

onSearchAgencyCompanies(search, elId){

    consoleService.log('element Id', true);
    consoleService.log(elId, true);

    consoleService.log('onSearchAgencyCompanies', true);

    consoleService.log('search', true);
    consoleService.log(search, true);

    if(this.selectize_timestamp){
       clearTimeout(this.selectize_timestamp);
    }

    if(
        search.length >= 2 &&
        search != this.drop_down_agency_companies_selected.name
        ){

        this.selectize_timestamp = setTimeout(() => {

            this.agency_companies_selectize_isLoading = true;

            //axios start
            axios.post(
                apiService.API_URL + '/agencyCompaniesListSelectize',
                {
                    token : this.$store.getters.token,
                    q : search,
                }
            ).then(
                (response) => {

                    this.agency_companies_selectize_isLoading = false;

                    consoleService.log('administrator form selectize search axios /onSearchAgencyCompanies response success', true);
                    consoleService.log(response.data, true);

                    this.agency_companies = response.data.data;

                }
            ).catch(
                (error) => {

                    // nothing to do here

                    //consoleService.log('administrator form selectize search axios /onSearchAgencyCompanies response error', true);
                    //consoleService.log(error, true);

                }
            );
            //axios stop

        }, 500);

    }

},

I have been trying to find a solution to this problem for days now. 几天来,我一直在努力寻找解决此问题的方法。

After trying all kind of tricks and running all kind of tests, I found out that this form component was working fine on the create scenario and failing on the edit scenario. 在尝试了各种技巧并运行了各种测试之后,我发现此表单组件在create方案中运行正常,而在edit方案中运行失败。

A couple of hours ago, I found the BUG. 几个小时前,我发现了错误。

Make sure that you do not use v-if directives ! 确保您不使用v-if指令!

Always try and use v-show directives, in order to avoid strange behaviour. 始终尝试使用v-show指令,以避免奇怪的行为。

<!-- row [ dropdowns companies ] / start -->
<div class="row">

    <!-- dropdown resellers companies / start -->
    <div v-show="show_drop_down('const_access_level_reseller')" 
    v-bind:class="drop_down_number_of_columns">

        <div class="form-group">

            <label>{{ translations.reseller_optional | filter_translation_default('reseller_optional') }}</label>

            <multiselect 
            id="multiselect_drop_down_reseller_companies" 
            name="multiselect_drop_down_reseller_companies" 
            autocomplete="off" 
            :autocomplete="off" 
            v-model="drop_down_reseller_companies_selected" 
            track-by="id" 
            label="name" 
            :multiple="false" 
            :options="reseller_companies" 
            :searchable="true" 
            :loading="drop_down_reseller_companies_selectize_isLoading" 
            :placeholder="drop_down_reseller_companies_selectize_placeholder" 
            @select="drop_down_reseller_companies_at_select" 
            :preselectFirst="true" 
            :allowEmpty="false" 
            deselectLabel="Selected" 
            :clearOnSelect="true" 
            :clear-on-select="true" 
            @search-change="searchForResellerCompany">

                <span slot="noResult">custom no result reseller companies</span>
                <span slot="noOptions">custom no options reseller companies</span>

            </multiselect>                

            <span class="text-danger" 
                v-show="validationErrorsFormAdministrator.reseller_id" 
                v-text="validationErrorsFormAdministrator.reseller_id"></span>                                

        </div>

    </div>
    <!-- dropdown resellers companies / stop -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM