选择2对象的奇怪行为

时间:2018-04-19 14:19:41

标签: javascript jquery-select2

我正在尝试使用select2进行辅助多选。 当我使用javascript函数object.assign合并我的对象时,会发生一些奇怪的事情。

我有一个multiselect select2输入的集合: Select2 inputs

合并对象响应

[…]
​
0: Object { id: 1, text: 1 }
​
1: Object { id: 2, text: 2 }
​
2: Object { id: 3, text: 3 }
​
3: Object { id: 4, text: 4 }

length: 4
​
__proto__: Array []

屏幕转导 Onscreen traduction

Plot 18选择的合并对象

[…]
​
0: Object { id: 1, text: 1 }
​
1: Object { id: 2, text: 2 }
​
2: Object { id: 3, text: 3 }
​
3: Object { id: 4, text: 4 }
​
4: Object { id: 5, text: 5 }
​
5: Object { id: 6, text: 6 }
​
6: Object { id: 7, text: 7 }
​
7: Object { id: 8, text: 8 }
​
8: Object { id: 9, text: 9 }
​
9: Object { id: 10, text: 10 }
​
length: 10
​
__proto__: Array []

屏幕图18选择

Plot 18 Onscreen traduction

Plot 1&的合并对象18选择

{…}
​
0: Object { id: 1, text: 1 }
​
1: Object { id: 2, text: 2 }
​
2: Object { id: 3, text: 3 }
​
3: Object { id: 4, text: 4 }
​
8: Object { id: 5, text: 5 }
​
9: Object { id: 6, text: 6 }
​
10: Object { id: 7, text: 7 }
​
11: Object { id: 8, text: 8 }
​
12: Object { id: 9, text: 9 }
​
13: Object { id: 10, text: 10 }
​
__proto__: Object { … }

屏幕图1& 18选择 Onscreen Plot 1&18 selection

源代码:

        $.ajax({
                  url: "<?php echo base_url()?>main/api_filters",
                  data: {PlotsSelected : PlotsSelected, CensusYearsSelected : CensusYearsSelected, VernNamesSelected : VernNamesSelected, FamiliesSelected : FamiliesSelected, GenusSelected : GenusSelected, SpeciesSelected : SpeciesSelected },
                  datatype: 'json',
                  async: true
          }).done(function(dataajax){
                  dataajax = JSON.parse(dataajax);

                  var VernNamesObj = $("#VernName").select2('data');
                  var FamiliesObj = $("#Family").select2('data');
                  var GenusObj = $("#Genus").select2('data');
                  var SpeciesObj = $("#Species").select2('data');
                  var PlotObj = $("#Plot").select2('data');
                  var SubPlotObj = $("#SubPlot").select2('data');
                  var CensusYearObj = $("#CensusYear").select2('data');

                  // Merge Selected options with received data to avoid unselection
                  let mergedVernNames = Object.assign(dataajax.VernName, VernNamesObj);
                  let mergedFamily = Object.assign(dataajax.Family, FamiliesObj);
                  let mergedGenus = Object.assign(dataajax.Genus, GenusObj);
                  let mergedSpecies = Object.assign(dataajax.Species, SpeciesObj);
                  let mergedSubPlot = Object.assign(dataajax.SubPlot, SubPlotObj);

                  console.log(mergedSubPlot);

                  if (dataajax) {
                      $("#VernName").html("");
                      $('#VernName').select2({
                          closeOnSelect: false,
                          data : mergedVernNames
                      });
                      $("#Family").html("");
                      $('#Family').select2({
                          closeOnSelect: false,
                          data : mergedFamily
                      });
                      $("#Genus").html("");
                      $('#Genus').select2({
                          closeOnSelect: false,
                          data : mergedGenus
                      });
                      $("#Species").html("");
                      $('#Species').select2({
                          closeOnSelect: false,
                          data : mergedSpecies
                      });
                      $("#SubPlot").html("");
                      $('#SubPlot').select2({
                          closeOnSelect: false,
                          data : mergedSubPlot
                      });

分类学输入是函数,只有#SubPlot字段有问题

1 个答案:

答案 0 :(得分:0)

当定义了所有参数时,Object.assign返回一个对象对象,Select2似乎使用数组而不是对象。

我将mergedSubPlot转换为数组并且有效

                  // Merge Selected options with received data to avoid unselection
                  let mergedVernNames = Object.assign(dataajax.VernName, VernNamesObj);
                  let mergedFamily = Object.assign(dataajax.Family, FamiliesObj);
                  let mergedGenus = Object.assign(dataajax.Genus, GenusObj);
                  let mergedSpecies = Object.assign(dataajax.Species, SpeciesObj);
                  //let mergedPlot = Object.assign(dataajax.Plot, PlotObj);
                  let mergedSubPlot = Object.assign(dataajax.SubPlot, SubPlotObj);
                  //let mergedCensusYear = Object.assign(dataajax.CensusYear, CensusYearObj);

                  mergedSubPlot = Object.keys(mergedSubPlot).map(function(key) {
                    return mergedSubPlot[key];
                  });


                  console.log(mergedSubPlot);

                  if (dataajax) {
                      $("#VernName").html("");
...