页脚在不同的 select2 下拉菜单下方添加多次

问题描述 投票:0回答:1

我正在使用 select 2 jquery 插件创建多个多选下拉菜单,在某些情况下,页脚会在下拉菜单下多次附加。例如,如果您打开 drodpown 1 且未选择任何选项,则打开下拉菜单 2 会附加一个额外的下拉菜单,反之亦然。我试图解决这个问题,但无法理解这个问题。任何人都可以帮忙吗? 附上工作示例代码

var temporarySelection = [];
var temporarySelection_level1 = [];
var temporarySelection_level2 = [];
var temporarySelection_level3 = [];
var startDateText = '';
var endDateText = '';
var selectedTextAward = '';
var selectedTextOrg = '';
var selectedTextPillar = '';
var selectedTextLeader = '';
var selectedTextTeam = '';
var selectedDateOpt = '';
let filterRequest = '';
var pillars = [];
let badgifyData = '';
let pageHomeAward_filter = 1;
var shouldClose_myselect = false;
var shouldClose_level1 = false;
var shouldClose_level2 = false;
var shouldClose_level3 = false;
// let jsonData = '';
function formatState(state) {
  if (!state.id) {
    return state.text;
  }
  var selected = ($('#mySelect').val() || []).indexOf(state.id) !== -1;
  var $state = $(
    '<span><input type="checkbox" class="select2-checkbox"' + (selected ? ' checked' : '') + ' /><span class="select2-checkbox-label">' + state.text + '</span></span>'
  );
  return $state;
}

function formatState_Level1(state) {
  if (!state.id) {
    return state.text;
  }
  var selected = ($('#level1').val() || []).indexOf(state.id) !== -1;
  var $state = $(
    '<span><input type="checkbox" class="select2-checkbox"' + (selected ? ' checked' : '') + ' /><span class="select2-checkbox-label">' + state.text + '</span></span>'
  );
  return $state;
}

function formatState_Level2(state) {
  if (!state.id) {
    return state.text;
  }
  var selected = ($('#level2').val() || []).indexOf(state.id) !== -1;
  var $state = $(
    '<span><input type="checkbox" class="select2-checkbox"' + (selected ? ' checked' : '') + ' /><span class="select2-checkbox-label">' + state.text + '</span></span>'
  );
  return $state;
}

function formatState_Level3(state) {
  if (!state.id) {
    return state.text;
  }
  var selected = ($('#level3').val() || []).indexOf(state.id) !== -1;
  var $state = $(
    '<span><input type="checkbox" class="select2-checkbox"' + (selected ? ' checked' : '') + ' /><span class="select2-checkbox-label">' + state.text + '</span></span>'
  );
  return $state;
}


function formatSelection(selected) {
  return selected.text;
}

function populateLevel2() {
  var level1Dropdown = document.getElementById("level1");
  var level2Dropdown = document.getElementById("level2");
  var selectedOrganizations = [];

  // Get all selected options in level1 dropdown
  for (var i = 0; i < level1Dropdown.options.length; i++) {
    if (level1Dropdown.options[i].selected) {
      selectedOrganizations.push(level1Dropdown.options[i].text);
    }
  }

  // Initialize an empty array to store unique pillars
  var pillars = [];

  // Extract unique pillars corresponding to the selected organizations
  for (var i = 0; i < jsonData.data.length; i++) {
    if (selectedOrganizations.includes(jsonData.data[i].Organization)) {
      var pillar = jsonData.data[i].Pillar;
      if (!pillars.includes(pillar)) {
        pillars.push(pillar);
      }
    }
  }

  // Clear the existing options in level2 dropdown
  level2Dropdown.innerHTML = '';

  // Populate the level2 dropdown with options
  for (var j = 0; j < pillars.length; j++) {
    var option = document.createElement("option");
    option.text = pillars[j];
    level2Dropdown.add(option);
  }
}

function populateLevel3() {
  var level1Dropdown = document.getElementById("level1");
  var level2Dropdown = document.getElementById("level2");
  var level3Dropdown = document.getElementById("level3");
  var selectedOrganizations = [];
  var selectedPillars = [];

  // Get all selected options in level1 dropdown
  for (var i = 0; i < level1Dropdown.options.length; i++) {
    if (level1Dropdown.options[i].selected) {
      selectedOrganizations.push(level1Dropdown.options[i].text);
    }
  }

  // Get all selected options in level2 dropdown
  for (var i = 0; i < level2Dropdown.options.length; i++) {
    if (level2Dropdown.options[i].selected) {
      selectedPillars.push(level2Dropdown.options[i].text);
    }
  }

  // Initialize an empty array to store unique Cost Center Leaders
  var costCenterLeaders = [];

  // Extract unique Cost Center Leaders corresponding to the selected combination of organization and pillar
  for (var i = 0; i < jsonData.data.length; i++) {
    if (selectedOrganizations.includes(jsonData.data[i].Organization)) {
      if (selectedPillars.length === 0 || selectedPillars.includes(jsonData.data[i].Pillar)) {
        var costCenterLeader = jsonData.data[i]["Cost Center Leader"];
        if (!costCenterLeaders.includes(costCenterLeader)) {
          costCenterLeaders.push(costCenterLeader);
        }
      }
    }
  }

  // Clear the existing options in level3 dropdown
  level3Dropdown.innerHTML = '';

  // Populate the level3 dropdown with options
  for (var j = 0; j < costCenterLeaders.length; j++) {
    var option = document.createElement("option");
    option.text = costCenterLeaders[j];
    level3Dropdown.add(option);
  }
}



$(document).ready(function() {


  //get the csv for dropdown
  getDropData();
  const select2Input = document.querySelector('.select2-container .select2-search input[type="search"]');
  if (select2Input) {
    select2Input.style.caretColor = 'transparent'; // Set caret color to transparent
  }
  //myselect ---------------------------------------------------------
  $('#mySelect').select2({
    placeholder: "Select",
    templateResult: formatState,
    templateSelection: formatSelection,
    closeOnSelect: false,
    minimumResultsForSearch: -1,
    allowClear: false
  });



  $('#mySelect').on('select2:open', function(e) {
    if (!$('.apply-button').length) {
      $('.select2-dropdown').append(
        '<div class="select2-footer">' +
        '<button class="clear-button">Reset</button>' +
        '<button class="cancel-button">Cancel</button>' +
        '<button class="apply-button">Apply</button></div>'
      );
    }

    $('.apply-button').off('click').on('click', function() {
      shouldClose_myselect = true;
      $('#mySelect').select2('close');
      selectedTextAward = $('#mySelect').select2('data')
        .map(option => option.text.replace(/\s+/g, '_').toUpperCase())
        .join(', ');
      console.log("Applying selection: award ", selectedTextAward);

      getFilterDataAward();

    });

    $('.clear-button').off('click').on('click', function(e) {
      shouldClose_myselect = true;
      temporarySelection = [];
      $('.select2-checkbox').prop('checked', false);
      $('#selected-items').val('');
      $('#mySelect').val(null).trigger('change');
      $('#mySelect').select2('close');
      selectedTextAward = ''
      filterRequest.awardTypeList = null;
      getFilterDataAward();

    });
    $('.cancel-button').off('click').on('click', function(e) {
      shouldClose_myselect = true;
      temporarySelection = [];
      $('.select2-checkbox').prop('checked', false);
      $('#selected-items').val('');
      $('#mySelect').val(null).trigger('change');
      $('#mySelect').select2('close');
      selectedTextAward = ''
      filterRequest.awardTypeList = null;
    });

    $('.select2-dropdown').off('click').on('click', function(e) {
      shouldClose_myselect = false;
    });
  });

  $('#mySelect').on('select2:closing', function(e) {
    if (!shouldClose_myselect) {
      e.preventDefault();
    }
    shouldClose_myselect = false; // Reset flag
  });

  $('#mySelect').on('select2:select', function() {
    temporarySelection = $('#mySelect').val();
    console.log("tempsel", temporarySelection)
  });

  $('#mySelect').on('select2:unselect', function() {
    temporarySelection = $('#mySelect').val();
    console.log("tempUnsel", temporarySelection)
  });



  //levell1 -------------------------------------------
  $('#level1').select2({
    placeholder: "Select",
    templateResult: formatState_Level1,
    templateSelection: formatSelection,
    closeOnSelect: false,
    minimumResultsForSearch: -1,
    allowClear: false
  });
  $('#level1').on('select2:open', function(e) {
    if (!$('.apply-button-level1').length) {
      $('.select2-dropdown').append(
        '<div class="select2-footer">' +
        '<button class="clear-button-level1">Reset</button>' +
        '<button class="cancel-button-level1">Cancel</button>' +
        '<button class="apply-button-level1">Apply</button></div>'
      );
    }

    $('.apply-button-level1').off('click').on('click', function(e) {
      shouldClose_level1 = true;
      $('#level1').select2('close');
      temporarySelection_level1 = $('#level1').val() || [];
      selectedTextOrg = $('#level1').select2('data').map(option => option.text).join(', ');
      selectedTextPillar = '';
      selectedTextLeader = '';

      console.log("selected-items-myselect", selectedTextOrg)
      $('.select2-container:eq(2)').css('display', 'block');
      getFilterDataAward();


    });

    $('.clear-button-level1').off('click').on('click', function(e) {
      shouldClose_level1 = true;
      temporarySelection_level1 = [];
      $('.select2-checkbox').prop('checked', false);
      $('#selected-items').val('');
      $('#level1').val(null).trigger('change');
      $('#level1').select2('close');
      selectedTextOrg = '';
      selectedTextPillar = '';
      selectedTextLeader = '';


      filterRequest.organizationList = null;
      $('.select2-container:eq(3)').css('display', 'none');
      $('.select2-container:eq(2)').css('display', 'none');
      $('.clear-button-level2').trigger('click');
      $('.clear-button-level3').trigger('click');
      getFilterDataAward();

    });

    $('.cancel-button-level1').off('click').on('click', function(e) {
      shouldClose_level1 = true;
      temporarySelection_level1 = [];
      $('.select2-checkbox').prop('checked', false);
      $('#selected-items').val('');
      $('#level1').val(null).trigger('change');
      $('#level1').select2('close');
      selectedTextOrg = '';
      selectedTextPillar = '';
      selectedTextLeader = '';


      filterRequest.organizationList = null;
      $('.select2-container:eq(3)').css('display', 'none');
      $('.select2-container:eq(2)').css('display', 'none');
      $('.clear-button-level2').trigger('click');
      $('.clear-button-level3').trigger('click');
    });

    $('.select2-dropdown').off('click').on('click', function(e) {
      shouldClose_level1 = false;
    });

  });

  $('#level1').on('select2:closing', function(e) {
    if (!shouldClose_level1) {
      e.preventDefault();
    }
    shouldClose_level1 = false; // Reset flag
  });

  $('#level1').on('select2:select', function() {
    temporarySelection_level1 = $('#level1').val();
    console.log("tempsel_l1", temporarySelection_level1)
  });

  $('#level1').on('select2:unselect', function() {
    temporarySelection_level1 = $('#level1').val();
    console.log("tempUnsel_l1", temporarySelection_level1)
    if (temporarySelection_level1.length == 0) {
      $('.select2-container:eq(3)').css('display', 'none');
      $('.select2-container:eq(2)').css('display', 'none');
      selectedTextPillar = '';
      selectedTextLeader = '';
    }
  });


  $('#level1').on('change', function() {
    var selectedValue = $(this).val();
    console.log("selected value", selectedValue)
    populateLevel2(selectedValue);
  });



  ///////level2------------////

  $('#level2').select2({
    placeholder: "Pillar",
    templateResult: formatState_Level2,
    templateSelection: formatSelection,
    closeOnSelect: false,
    minimumResultsForSearch: -1,
    allowClear: false
  });
  $('.select2-container:eq(2)').css('display', 'none');

  $('#level2').on('select2:open', function() {
    if (!$('.apply-button-level2').length) {
      $('.select2-dropdown').append(
        '<div class="select2-footer">' +
        '<button class="clear-button-level2">Reset</button>' +
        '<button class="cancel-button-level2">Cancel</button>' +
        '<button class="apply-button-level2">Apply</button></div>'
      );
    }


    $('.apply-button-level2').off('click').on('click', function(e) {
      shouldClose_level2 = true;
      $('#level2').select2('close');
      $('#level3').select2('close');
      console.log("myyyy level2", $('#level2').val(temporarySelection_level2))
      $('#level2').val(temporarySelection_level2).trigger('change');
      selectedTextPillar = $('#level2').select2('data').map(option => option.text).join(', ');
      selectedTextLeader = '';
      $('.select2-container:eq(3)').css('display', 'block');
      console.log("selected-items-myselect", selectedTextPillar)
      getFilterDataAward();

    });

    $('.clear-button-level2').off('click').on('click', function(e) {
      shouldClose_level2 = true;
      temporarySelection_level2 = [];
      $('.select2-checkbox').prop('checked', false);
      $('#selected-items').val('');
      $('#level2').val(null).trigger('change');
      $('#level2').select2('close');
      $('.select2-container:eq(3)').css('display', 'none');
      $('.clear-button-level3').trigger('click');
      selectedTextPillar = '';
      selectedTextLeader = '';

      getFilterDataAward();
    });

    $('.cancel-button-level2').off('click').on('click', function(e) {
      $('#level2').select2('close');
      shouldClose_level2 = true;
      temporarySelection_level2 = [];
      $('.select2-checkbox').prop('checked', false);
      $('#selected-items').val('');
      $('#level2').val(null).trigger('change');
      $('#level2').select2('close');
      $('.select2-container:eq(3)').css('display', 'none');
      $('.clear-button-level3').trigger('click');

      selectedTextPillar = '';
      selectedTextLeader = '';
    });

    $('.select2-dropdown').off('click').on('click', function(e) {
      shouldClose_level2 = false;
    });
  });

  $('#level2').on('select2:closing', function(e) {
    if (!shouldClose_level2) {
      e.preventDefault();
    }
    shouldClose_level2 = false; // Reset flag
  });

  $('#level2').on('select2:select', function() {
    temporarySelection_level2 = $('#level2').val();
    console.log("tempsel_l2", temporarySelection_level2)
  });

  $('#level2').on('select2:unselect', function() {
    temporarySelection_level2 = $('#level2').val();
    console.log("tempUnsel_l2", temporarySelection_level2)
    if (temporarySelection_level2.length == 0) {
      $('.select2-container:eq(3)').css('display', 'none');
      selectedTextLeader = '';
    }
  });

  $('#level2').on('change', function() {
    var selectedValue = $(this).val();
    console.log("selected value", selectedValue)
    populateLevel3(selectedValue);
  });

  ///level3--------------------

  $('#level3').select2({
    placeholder: "Leader",
    templateResult: formatState_Level3,
    templateSelection: formatSelection,
    closeOnSelect: false,
    minimumResultsForSearch: -1,
    allowClear: false
  });
  $('.select2-container:eq(3)').css('display', 'none');

  $('#level3').on('select2:open', function(e) {
    if (!$('.apply-button-level3').length) {
      $('.select2-dropdown').append(
        '<div class="select2-footer">' +
        '<button class="clear-button-level3">Reset</button>' +
        '<button class="cancel-button-level3">Cancel</button>' +
        '<button class="apply-button-level3">Apply</button></div>'
      );
    }

    $('.apply-button-level3').off('click').on('click', function(e) {
      shouldClose_level3 = true;
      $('#level3').select2('close');
      console.log("myyyy level3", $('#level3').val(temporarySelection_level3))
      $('#level3').val(temporarySelection_level3).trigger('change');
      selectedTextLeader = $('#level3').select2('data').map(option => option.text).join(', ');
      console.log("selected-items-myselect", selectedTextLeader);

      getFilterDataAward();

    });

    $('.clear-button-level3').off('click').on('click', function(e) {
      shouldClose_level3 = true;
      temporarySelection_level3 = [];
      $('.select2-checkbox').prop('checked', false);
      $('#selected-items').val('');
      $('#level3').val(null).trigger('change');
      $('#level3').select2('close');

      selectedTextLeader = '';
      getFilterDataAward();
    });

    $('.cancel-button-level3').off('click').on('click', function(e) {
      shouldClose_level3 = true;
      $('#level3').select2('close');
      temporarySelection_level3 = [];
      $('.select2-checkbox').prop('checked', false);
      $('#selected-items').val('');
      $('#level3').val(null).trigger('change');
      $('#level3').select2('close');
      selectedTextLeader = '';
    });

    $('.select2-dropdown').off('click').on('click', function(e) {
      shouldClose_level3 = false;
    });
  });

  $('#level3').on('select2:closing', function(e) {
    if (!shouldClose_level3) {
      e.preventDefault();
    }
    shouldClose_level3 = false; // Reset flag
  });

  $('#level3').on('select2:select', function() {
    temporarySelection_level3 = $('#level3').val();
    console.log("tempsel_l3", temporarySelection_level3)
  });

  $('#level3').on('select2:unselect', function() {
    temporarySelection_level3 = $('#level3').val();
    console.log("tempUnsel_l3", temporarySelection_level3)
  });

});

function getDropData() {
  $('.select2-container:eq(2)').css('display', 'none');
  $('.select2-container:eq(3)').css('display', 'none');

  jsonData = {
    "data": [{
        "Organization": "Finance",
        "Pillar": "Enabling IDC Functions",
        "Cost Center Leader": "Rahul Kakkar"
      },
      {
        "Organization": "Finance",
        "Pillar": "Enabling IDC Functions",
        "Cost Center Leader": "Rohit Sudheer Bhagade"
      },
      {
        "Organization": " Data Ventures",
        "Pillar": "wms Data Venture",
        "Cost Center Leader": "Ramesh Kozhissery"
      },
      {
        "Organization": "WMTUS CORE CUSTOMER CARE",
        "Pillar": "WMTUS - Ops OH",
        "Cost Center Leader": "Balasubrahmanyam Chaturvedula"
      }
    ]
  }
  populateDrop();



}


function populateDrop() {
  console.log("inside dropdata 1", jsonData);
  var dropdown = document.getElementById("level1");
  var organizations = [];

  // Extract unique organization names from the JSON data
  for (var i = 0; i < jsonData.data.length; i++) {
    var organization = jsonData.data[i].Organization;
    if (!organizations.includes(organization)) {
      organizations.push(organization);
    }
  }

  // Populate the dropdown with options
  for (var j = 0; j < organizations.length; j++) {
    var option = document.createElement("option");
    option.text = organizations[j];
    dropdown.add(option);
  }

}

function getFilterDataAward(pageNum_filter = pageHomeAward_filter) {
  console.log("filter happening")
}

function clearAll() {
  console.log("clear all")

}
.block-container-component .img-background {
  background-color: #fff !important;
}

.select2-container select2-container--default select2-container--open {
  top: 150px;
}

.select2-container .select2-selection--single .select2-selection__rendered {
  display: block;
  padding-left: 8px;
  padding-right: 20px;
  padding-top: 4px;
  padding-bottom: 0px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.select2-container--default .select2-selection--multiple {
  background-color: white;
  border: 1px solid #aaa;
  border-radius: 4px;
  cursor: text;
  max-width: 300px;
  min-height: 40px;
  padding-bottom: 0px !important;
}

.select2-container--default .select2-selection--single {
  background-color: #fff;
  border: 1px solid #aaa;
  border-radius: 4px;
  min-height: 39px;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice {
  background-color: #fff !important;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice {
  background-color: #e4e4e4;
  border: 1px solid #aaa;
  border-radius: 4px;
  cursor: default;
  float: left;
  margin-right: 5px;
  margin-top: 5px;
  padding: 0 4px;
}

.select2-container .select2-dropdown .select2-results__options .select2-results__option input {
  margin-right: 10px;
}

.select2-container .select2-dropdown .select2-results__options .select2-results__option .select2-checkbox-label {
  pointer-events: none;
}

.select2-container .select2-dropdown .select2-footer {
  padding: 5px;
  text-align: right;
  border-top: 1px solid #ddd;
}

.apply-button,
.clear-button,
.cancel-button,
.apply-button-level1,
.clear-button-level1,
.cancel-button-level1,
.apply-button-level2,
.clear-button-level2,
.cancel-button-level2,
.apply-button-level3,
.clear-button-level3,
.cancel-button-level3,
#apply-btn-date,
#cancel-btn-date,
#reset-btn-date {
  background-color: #0072dc;
  color: white;
  border-radius: 20px;
  min-width: 50px;
  border: none;
  padding: 5px 20px;
  margin-left: 5px;
  font-size: 14px;
  cursor: pointer;
}

#apply-btn-date {
  display: none;
}

.select2-search--dropdown {
  display: none;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice__remove {
  display: none !important;
}

.select2-container--default.select2-container--focus .select2-selection--multiple {
  background-color: #fff;
}

.select2-container .select2-dropdown .select2-results__options .select2-results__option input {
  margin-right: 10px;
  pointer-events: auto !important;
}

.select2-container .select2-dropdown .select2-results__options .select2-results__option {
  display: flex;
  align-items: center;
  pointer-events: none;
}

#select2-date-range-options-results>.select2-container .select2-dropdown .select2-results__options .select2-results__option {
  pointer-events: auto;
}

#select2-date-range-options-results>li {
  pointer-events: auto !important;
}

.dropdown-content {
  display: flex;
  justify-content: flex-start;
  z-index: 1;
  position: relative;
}

.select2-container--default .select2-results__option[aria-disabled=true] {
  color: #999;
  display: none !important;
}

#level1,
#level2 #level3 {
  display: block;
}

#dropdown1,
#dropdown2 {
  margin: 5px;
}

#dropdown2 {
  text-align: center;
}

#date-range-selector {
  margin: 6px;
  padding-bottom: 5px;
}

.datepicker {
  float: left;
  margin-right: 10px;
}

#level2 {
  display: none;
}

#level3 {
  display: none;
}

.select2-container .select2-search--inline {
  float: left;
  display: block;
}

.select2-container--default .select2-selection--multiple .select2-selection__choice__display {
  cursor: default;
  padding-left: 2px;
  padding-right: 5px;
  font-size: 12px;
  position: relative;
  top: -2px;
}

.hidden-important {
  display: none !important;
}

.flex-important {
  display: flex !important;
}

.drop-header-team {
  font-size: 14px;
  text-align: left;
  margin-left: 0px;
}

.drop-header-date {
  font-size: 14px;
}

.select2-container--default .select2-selection--multiple {
  position: relative;
  caret-color: transparent !important;
}

.select2-container--default .select2-search--inline .select2-search__field {
  position: absolute;
}

#filter-by {
  padding: 10.0px 0.0px 0.0px 5.0px;
  font-size: 15.0px;
  font-weight: 700;
}

#clearall-btn {
  margin: 30px 10px 10px 10px;
  color: black;
  text-decoration: underline;
}

.drop-header-team {
  position: relative;
}

.select2-search__field {
  position: absolute;
  top: 3px;
}

.ui-state-active,
.ui-widget-content .ui-state-active {
  border: 1px solid #c5c5c5 !important;
  background: #f6f6f6 !important;
  font-weight: normal;
  color: #454545 !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.1.0-beta.1/css/select2.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<!-- Include Select2 CSS -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />
<!-- Include jQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!-- Include Select2 JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.1.0-beta.1/js/select2.min.js"></script>
<div class="dropdown-content">
  <div id="dropdown1">
    <div class="drop-header-award">Award</div>
    <select id="mySelect" style="width: 300px;" multiple="multiple">
      <option value="1">Bravo</option>
      <option value="2">Impact</option>
      <option value="3">Excellence</option>
      <option value="4">Team Award</option>
      <!-- <option value="5">Service Award</option> -->
      <!-- <option value="6">Tech Fwd</option> -->
    </select>
  </div>
  <div id="dropdown2">
    <div class="drop-header-team">Team </div>
    <!-- <span class="tooltip-icon">ℹ️</span> -->
    <select id="level1" style="width: 300px;" multiple>
    </select>

    <select id="level2" style="width: 300px;" multiple>
    </select>

    <select id="level3" style="width: 300px;" multiple>

    </select>
  </div>
  <a id="clearall-btn" onclick="clearAll()">Clear all</a>
</div>

javascript html jquery drop-down-menu jquery-select2
1个回答
0
投票

问题在于

select2:open
事件侦听器函数内的选择器。当您打开第二个 select2 时,您的
$('.select2-dropdown')
选择器将从第一个 select2 中查找下拉列表,并将按钮也附加到该下拉列表中,如果您使选择器特定于 select2 实例,那么它将被修复。

将下拉容器定位到特定 select2 实例的一种方法是使用以下方法:

var $container = $(e.currentTarget).data('select2').$dropdown;

这是

#level1
select2 的示例:

//levell1 -------------------------------------------
$('#level1').select2({
  placeholder: "Select",
  templateResult: formatState_Level1,
  templateSelection: formatSelection,
  closeOnSelect: false,
  minimumResultsForSearch: -1,
  allowClear: false
});
$('#level1').on('select2:open', function(e) {
  var $container = $(e.currentTarget).data('select2').$dropdown;

  if (!$container.find('.apply-button-level1').length) {
    $container.find('.select2-dropdown').append(
      '<div class="select2-footer">' +
      '<button class="clear-button-level1">Reset</button>' +
      '<button class="cancel-button-level1">Cancel</button>' +
      '<button class="apply-button-level1">Apply</button></div>'
    );
  }

  $container.find('.apply-button-level1').off('click').on('click', function(e) {
    shouldClose_level1 = true;
    $('#level1').select2('close');
    temporarySelection_level1 = $('#level1').val() || [];
    selectedTextOrg = $('#level1').select2('data').map(option => option.text).join(', ');
    selectedTextPillar = '';
    selectedTextLeader = '';

    console.log("selected-items-myselect", selectedTextOrg)
    $('.select2-container:eq(2)').css('display', 'block');
    getFilterDataAward();


  });

  $container.find('.clear-button-level1').off('click').on('click', function(e) {
    shouldClose_level1 = true;
    temporarySelection_level1 = [];
    $('.select2-checkbox').prop('checked', false);
    $('#selected-items').val('');
    $('#level1').val(null).trigger('change');
    $('#level1').select2('close');
    selectedTextOrg = '';
    selectedTextPillar = '';
    selectedTextLeader = '';


    filterRequest.organizationList = null;
    $('.select2-container:eq(3)').css('display', 'none');
    $('.select2-container:eq(2)').css('display', 'none');
    $('.clear-button-level2').trigger('click');
    $('.clear-button-level3').trigger('click');
    getFilterDataAward();

  });

  $container.find('.cancel-button-level1').off('click').on('click', function(e) {
    shouldClose_level1 = true;
    temporarySelection_level1 = [];
    $('.select2-checkbox').prop('checked', false);
    $('#selected-items').val('');
    $('#level1').val(null).trigger('change');
    $('#level1').select2('close');
    selectedTextOrg = '';
    selectedTextPillar = '';
    selectedTextLeader = '';


    filterRequest.organizationList = null;
    $('.select2-container:eq(3)').css('display', 'none');
    $('.select2-container:eq(2)').css('display', 'none');
    $('.clear-button-level2').trigger('click');
    $('.clear-button-level3').trigger('click');
  });

  $container.find('.select2-dropdown').off('click').on('click', function(e) {
    shouldClose_level1 = false;
  });

});

您可能想要更新其他 select2 的侦听器函数,以便它们只更新自己的下拉列表。

© www.soinside.com 2019 - 2024. All rights reserved.