【问题标题】:how to load array of objects data with select2如何使用 select2 加载对象数据数组
【发布时间】:2018-01-03 05:09:25
【问题描述】:

我的问题:

我已经创建了我的问题的完整场景。

我的 HTML:

<select data-placeholder = "Sending" id = "sender" data-allow-clear = true >
   <option ></option >
</select >    

<select data-placeholder = "Receiving" id = "receiving" data-allow-clear = true >
  <option ></option >
</select >

Corridor url 返回以下集合(我使用 laravel 作为后端):

[ {
    "id"                 : 1, "source_country_id": 1, "destination_country_id": 2,
    "source_country"     : { "id": 1, "name": "United Kingdom", "flag_icon": "flag-icon-gb" },
    "destination_country": { "id": 2, "name": "Pakistan", "flag_icon": "flag-icon-pk" }
}, {
    "id"                 : 2, "source_country_id": 1, "destination_country_id": 3,
    "source_country"     : { "id": 1, "name": "United Kingdom", "flag_icon": "flag-icon-gb" },
    "destination_country": { "id": 3, "name": "India", "flag_icon": "flag-icon-in" }
}, {
    "id"                 : 7, "source_country_id": 1, "destination_country_id": 4,
    "source_country"     : { "id": 1, "name": "United Kingdom", "flag_icon": "flag-icon-gb" },
    "destination_country": { "id": 4, "name": "Bangladesh", "flag_icon": "flag-icon-bd" }
} ]

我的 Vue 代码:在使用 axios 的 corridors 方法中,我正在检索数据(如上所示),之后我为 sendigCountriesrecevingCountries 创建了两个数组。两个数组都成功填充了它的数据(sendingCountries 使用重复数据创建),之后我想将该数据传递给 select2 但 select2 没有填充该数组。请帮我找出我犯错的地方。

  var app = new Vue({
    methods: {
         corridors : function () {
                   axios.post ( '/corridors' )
                   .then ( response => {
                         let sendingCountries   = [];
                         let receivingCountries = [];
                         _.forEach ( response.data, function (value, key) {
                                 sendingCountries.push ( {
                                   id: value.source_country.id,
                                   text: value.source_country.name,
                                   flag: value.source_country.flag_icon
                                 });

                                 receivingCountries.push ( {
                                   id  : value.destination_country.id,
                                   text: value.destination_country.name,
                                   flag: value.destination_country.flag_icon
                                 });
                    } );
                   $ ( "#sender" ).select2 ( {
                                      width: '100%',
                                      data : sendingCountries,
                                  } );
                    $ ( "#receiver" ).select2 ( {
                                      width: '100%',
                                      data : receivingCountries,
                                  } );
                   } )
                   .catch ( error => { } )
            }
     }
})

【问题讨论】:

    标签: javascript vuejs2 lodash jquery-select2


    【解决方案1】:

    数组格式不正确:

    var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];
    

    数组必须包含 id 和 text 键。

    更新

    HTML

    var sender=$ ("#sender").select2 ({
         width: '100%'
    	});
    	
     var receiver= $ ("#receiver").select2 ({
         width: '100%'
      });
     
    
     var app = new Vue({
        methods: {
             corridors : function () {
                       axios.post ( '/corridors' )
                       .then ( response => {
                             let sendingCountries   = [];
                             let receivingCountries = [];
                             _.forEach ( response.data, function (value, key) {
                                     sendingCountries.push ( {
                                       id: value.source_country.id,
                                       text: value.source_country.name,
                                       flag: value.source_country.flag_icon
                                     });
    
                                     receivingCountries.push ( {
                                       id  : value.destination_country.id,
                                       text: value.destination_country.name,
                                       flag: value.destination_country.flag_icon
                                     });
                        } );
    
                 //Assume this array coming from ajax
                 var sourceCountry = [{ id: "US", text: 'America' }, { id: "AF", text: 'Africa' }, { id: "Ind", text: 'India' }, { id: "UK", text: 'England' }, { id: "ITL", text: 'Itally' }];
    
                 var receivingCountries = [{ id: "US", text: 'America' }, { id: "AF", text: 'Africa' }, { id: "Ind", text: 'India' }, { id: "UK", text: 'England' }, { id: "ITL", text: 'Itally' }];
                      sender.select2({data:sourceCountry});
                      receiver.select2({data:receivingCountries});
                       } )
                       .catch ( error => { } )
                }
         }
    })
    <select class="select2" data-placeholder = "Sending" id = "sender" data-allow-clear = true >
       <option value="">Please Select</option >
    </select >    
    
    <select class="select2"  data-placeholder = "Receiving" id = "receiver" data-allow-clear = true >
      <option value="">Please Select</option >
    </select >

    参考Link

    【讨论】:

    • sendingCountriesreceivingCountries 在 ajax 成功响应中创建 idtext 并标记如果我从两个数组中删除 flag 而不是仍然没有填充相同的情况数据...
    • @WaqasMehmood 请将完整代码分享给 fiddle 或 jsbin
    • 是的 @sarath 它可以与简单的 JS 一起使用,但不能与 vuejs 一起使用。感谢您抽出宝贵时间解决了这个问题。
    • @WaqasMehmood 怎么样?
    • 在我的问题中,我使用了非常复杂的逻辑,但在 vue 中,通过在选择选项上使用 v-for 并填充该值非常简单。
    猜你喜欢
    • 2018-06-12
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 2015-05-12
    • 1970-01-01
    • 2014-02-15
    相关资源
    最近更新 更多