jquery Select2在bootstrap 4.6.0的模式内不工作

zfciruhq  于 2023-06-22  发布在  jQuery
关注(0)|答案(1)|浏览(107)

我是bootstrap 4的新手,我已经在寻找解决这个问题的任何替代方案。select2都能正常工作,但是当我把它放在@include()的模态选项卡内容中时,它开始以不同的方式工作。

    • peralatan放在tindakan上面时:**

<div class="tab-content p-7 pe-3" id="tab-layanan">
                   {{-- Peralatan --}}
                    <div class="tab-pane fade" id="peralatan" role="tabpanel">
                        @include(
                            'pasien.tindakan_peralatan._modal_tambah_peralatan'
                        )
                    </div>
                    {{-- Tindakan --}}
                    <div class="tab-pane fade show active" id="tindakan" role="tabpanel">
                        @include(
                            'pasien.tindakan_peralatan._modal_tambah_tindakan'
                        )
                    </div>
                </div>
    • 这是当peralatan放在tindakan下面时:**

<div class="tab-content p-7 pe-3" id="tab-layanan">
                {{-- Tindakan --}}
                <div class="tab-pane fade show active" id="tindakan" role="tabpanel">
                    @include(
                        'pasien.tindakan_peralatan._modal_tambah_tindakan'
                    )
                </div>
                {{-- Peralatan --}}
                <div class="tab-pane fade" id="peralatan" role="tabpanel">
                    @include(
                        'pasien.tindakan_peralatan._modal_tambah_peralatan'
                    )
                </div>
            </div>

peralatan被放置在tab-content内部时,下面的下拉列表无法正常工作(指示符是占位符未显示时),我一直认为这是tab-content的问题,但它也没有关闭其他可能性,如来自 Bootstrap 本身的modal

    • 另一个JS代码,用于处理peralatantindakan的hide/show div id:**
<script>
    if($('#tipe-data').val() === 'Peralatan'){
        $("#tab_tdk").addClass('active')
        $("#tab_alat").removeClass('active')
        $("#tindakan").addClass('show active')
        $("#peralatan").removeClass('show active')
    
    }else{
        $("#tab_tdk").removeClass('active')
        $("#tab_alat").addClass('active')
        $("#tindakan").removeClass('show active')
        $("#peralatan").addClass('show active')
    }   
</script>
    • 下面是@include('pasien.tindakan_peralatan._modal_tambah_peralatan')中select2的例子:**
$(document).each(function(){
            $('#input-search-peralatan-pasien').select2({
                placeholder: 'Cari Peralatan',
                dropdownParent:  $('#input-search-peralatan-pasien').parent('#modal_tambah_tdk_alat_pasien'),,
                ajax: {
                        ...}
                 ...
}
    • 展示流程的模块完整代码:**
if($('#tipe-data').val() === 'Peralatan'){
            $("#tab_tdk").addClass('active')
            $("#tab_alat").removeClass('active')
            $("#tindakan").addClass('show active')
            $("#peralatan").removeClass('show active')
        
        }else{
            $("#tab_tdk").removeClass('active')
            $("#tab_alat").addClass('active')
            $("#tindakan").removeClass('show active')
            $("#peralatan").addClass('show active')
        }
<div class="modal fade"  style="overflow:hidden;" id="modal_tambah_tdk_alat_pasien" data-bs-backdrop="static" data-bs-keyboard="false">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">{{__('Tambah Data Tindakan Medis & Peralatan')}}</h5>

                <div class="btn btn-icon btn-sm btn-active-light-primary ms-2" data-bs-dismiss="modal" aria-label="Close">
                    <span class="fa fa-times"></span>
                </div>
            </div>

            <div class="modal-body p-0">

                <div class="card">
                    <div class="card-body">

                        <ul class="nav nav-tabs nav-line-tabs nav-line-tabs-2x nav-stretch fs-6 border-2 px-7 pt-3">
                            <li class="nav-item">
                                <a class="nav-link active" id="tab_tdk" data-bs-toggle="tab" href="#tindak">{{__('Tindakan')}}</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" id="tab_alat" data-bs-toggle="tab" href="#peralat">{{__('Peralatan')}}</a>
                            </li>
                        </ul>

                        <div class="tab-content p-7 pe-3" data-focus="false" id="tab-layanan">
                            {{-- Tindakan --}}
                            <div class="tab-pane fade show active" id="tindak" role="tab-1">
                                @include(
                                    'pasien.tindakan_peralatan._modal_tambah_tindakan'
                                )
                            </div>

                            {{-- Peralatan --}}
                            <div class="tab-pane fade" data-focus="false" id="peralat" role="tab-2">
                                @include(
                                    'pasien.tindakan_peralatan._modal_tambah_peralatan'
                                )
                            </div>
                        </div>

                    </div>
                </div>

            </div>

            <div class="modal-footer bg-light">
                <div class="row w-100">
                    <div class="col-6">
                        <button type="button" class="btn btn-sm btn-light-primary rounded-1" data-bs-dismiss="modal">{{ __('Tutup') }}</button>
                    </div>
                    <div class="col-6 text-end">
                        <button type="button" class="btn btn-sm btn-primary rounded-1 btn-action-simpan-tdk-alat btn-submit-tera">{{ __('Simpan') }}</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

如果你知道并能指出我代码中的问题,请帮助我找到它,谢谢:)

wa7juj8i

wa7juj8i1#

你应该永远不要像这样更改标签:

if ($('#tipe-data').val() === 'Peralatan') {
  $("#tab_tdk").addClass('active')
  $("#tab_alat").removeClass('active')
  $("#tindakan").addClass('show active')
  $("#peralatan").removeClass('show active')
} else {
  $("#tab_tdk").removeClass('active')
  $("#tab_alat").addClass('active')
  $("#tindakan").removeClass('show active')
  $("#peralatan").addClass('show active')
}

这段代码只是破坏了制表符。如果您需要手动干预选项卡的状态,请使用适当的Bootstrap函数。

$(document).each(function(){
  $('#input-search-peralatan-pasien').select2({
    placeholder: 'Cari Peralatan',
    dropdownParent:  $('#input-search-peralatan-pasien').parent('#modal_tambah_tdk_alat_pasien'),,
    ajax: {
      ...
    }
  ...
}

这里使用each()循环遍历$(document)。这不是each()的作用。
看起来你想要这样的东西:

$(function() {
  $('#input-search-peralatan-pasien').each((input) => {
    input.select2(...);
  });
});

但也是错的你不应该在id=“input-search-peralatan-pasien”的元素上循环,因为你应该NEVER有多个元素具有相同的ID。文档中只能重复类,但ID必须唯一。
不要使用循环,只加载'pasien.tindakan_peralatan._modal_tambah_peralatan'应该加载的任何内容。

$(function(){
  $("#modal_tambah_tdk_alat_pasien").on('show.bs.modal', () => {
    if ($('#tipe-data').val() === 'Tindakan') {
      $('#tipe-tabs button[data-target="#tindak"]').tab('show');
    } else if ($('#tipe-data').val() === 'Peralatan') {
      $('#tipe-tabs button[data-target="#peralat"]').tab('show');
    }
  });
  
  const dataTindakan = [
    {
      id: 0,
      text: 'Tindakan 1'
    },
    {
      id: 1,
      text: 'Tindakan 2'
    }
  ],
  dataPeraltan = [
    {
      id: 0,
      text: 'Peraltan 1'
    },
    {
      id: 1,
      text: 'Peraltan 2'
    }
  ];
  
  $('#input-search-tindakan-medis-pasien').select2({
    placeholder: 'Tindaken',
    data: dataTindakan
  });
  $('#input-search-peralatan-pasien').select2({
    placeholder: 'Peralatan',
    data: dataPeraltan
  });
});
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css">

<div class="container py-5">
  <div class="input-group">
    <select id="tipe-data" class="custom-select">
      <option selected value="Tindakan">Tindakan</option>
      <option value="Peralatan">Peralatan</option>
    </select>
    <div class="input-group-append">
      <button id="tipe-modal-button" class="btn btn-primary" data-toggle="modal" data-target="#modal_tambah_tdk_alat_pasien">Show modal</button>
    </div>
  </div>

  <div class="modal fade" id="modal_tambah_tdk_alat_pasien">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">Tambah Data Tindakan Medis & Peralatan</h5>
          <div class="btn btn-icon btn-sm btn-active-light-primary ms-2" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </div>
        </div>
        <div class="modal-body pt-0">
          <ul class="nav nav-tabs nav-line-tabs nav-line-tabs-2x nav-stretch fs-6 border-2 px-7 pt-3" id="tipe-tabs" role="tablist">
            <li class="nav-item">
              <button class="nav-link" id="tab_tdk" data-toggle="tab" data-target="#tindak">Tindakan</button>
            </li>
            <li class="nav-item">
              <button class="nav-link" id="tab_alat" data-toggle="tab" data-target="#peralat">Peralatan</button>
            </li>
          </ul>

          <div class="tab-content" id="tab-layanan">
            <div class="tab-pane p-4 bg-light fade" id="tindak" role="tabpanel">
              <select id="input-search-tindakan-medis-pasien"></select>
            </div>
            <div class="tab-pane p-4 bg-light fade" id="peralat" role="tabpanel">
              <select id="input-search-peralatan-pasien"></select>
            </div>
          </div>
        </div>
        <div class="modal-footer justify-content-between bg-light">
          <button type="button" class="btn btn-sm btn-dark rounded" data-dismiss="modal">Tutup</button>
          <button type="button" class="btn btn-sm btn-primary rounded btn-action-simpan-tdk-alat btn-submit-tera">Simpan</button>
        </div>
      </div>
    </div>
  </div>
</div>

相关问题