๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Lect & Tip/bootstrap

โœˆ bootstrap selectpicker multiple js ์ˆ˜์ • ์ „์ฒด์„ ํƒ ํ•ด์ œ ๋ณ€๊ฒฝ css

by ๋‚ฏ์„ ๊ณต๊ฐ„2019 2019. 12. 22.

๋ชฉ์ฐจ

    bootstrap selectpicker multiple js ์ˆ˜์ • ์ „์ฒด์„ ํƒ ํ•ด์ œ ๋ณ€๊ฒฝ.

    ์ด ์ œ๋ชฉ์„ ๋ญ๋ผ๊ณ  ์„ค๋ช…ํ•ด์•ผ ํ• ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค.

    ์•„๋ฌดํŠผ ์š”์ง€๋Š” ์ด๋ ‡๋‹ค.

    ๋ถ€ํŠธ์ŠคํŠธ๋žฉ4์˜ selectpicker multiple ์˜ต์…˜์„ ์‚ฌ์šฉํ•  ๋•Œ ๋””ํดํŠธ๋กœ ์ฃผ์–ด์ง€๋Š” ๋ชจ๋‘์„ ํƒ ๋ชจ๋‘ํ•ด์ œ ํ† ๊ธ€ ๋˜๋Š”๊ฒŒ ํด๋ผ์ด์–ธํŠธ ๋งˆ์Œ์— ์•ˆ๋“ค์—ˆ๋‚˜๋ณด๋‹ค.

    ๋ชจ๋“  option์ด ์„ ํƒ๋˜๋ฉด ๋ชจ๋‘ ํ•ด์ œ๋˜๋Š” ๊ฒƒ์€ ๋‹น์—ฐํ•˜์ง€๋งŒ ํ•˜๋‚˜์˜ ์˜ต์…˜๋งŒ ์„ ํƒํ•ด๋„ ๋ชจ๋‘ํ•ด์ œ๋กœ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ด ํด๋ผ์ด์–ธํŠธ๋Š” ์ดํ•ด๊ฐ€ ์•ˆ๋œ ๋ชจ์–‘์ด๋‹ค.

    ๊ทธ๋ž˜์„œ ํด๋ผ์ด์–ธํŠธ๋Š” ์ „์ฒด๊ฐ€ ์•„๋‹Œ ์˜ต์…˜์„ ์„ ํƒํ•˜๋ฉด ์ „์ฒด์˜ต์…˜์€ ๋ณ€ํ•˜์ง€ ์•Š๊ณ , ์ „์ฒด์˜ต์…˜์„ ์„ ํƒํ•˜๋ฉด ๋ชจ๋“  ์˜ต์…˜๋„ ์ฒดํฌ๊ฐ€ ๋œ ๊ฒƒ ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ํ•ด ๋‹ฌ๋ผ๋Š” ๊ฒƒ์ด๋‹ค.

    ๊ธฐ๋ณธ์ ์ธ ์…€๋ ‰ํŠธํ”ผ์ปค์˜ ํƒœ๊ทธ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

              <select multiple data-actions-box="true" class="form-control selectpicker" data-width="135px"
                name="ctrlSettingsg2" id="ctrlSettingsg2" title="๊ฒ€์ƒ‰์กฐ๊ฑด">
                <option>๊ทธ๋ฃน๋ช…</option>
                <option>๊ทธ๋ฃน์„ค๋ช…</option> 
                <option>๋ถ€์„œ๋ช…</option>
              </select>

    ์ƒˆ๋กœ ๋ณ€๊ฒฝํ•ด์„œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ง  html๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.

              <select multiple class="form-control selectpicker" data-width="135px" name="ctrlSettingsg2"
                id="ctrlSettingsg2" title="๊ฒ€์ƒ‰์กฐ๊ฑด">
                <option class="selAll">์ „์ฒด</option>
                <option>๊ทธ๋ฃน๋ช…</option>
                <option>๊ทธ๋ฃน์„ค๋ช…</option>
                <option>๋ถ€์„œ๋ช…</option>
              </select>

    ๋‹ฌ๋ผ์ง„ ์ ์€ data-actions-box="true" ์ด ๋น ์ง€๊ณ , ์ „์ฒด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์˜ต์…˜์ด ์ถ”๊ฐ€๋˜๋ฉด์„œ class์ธ์‹์‚ฌ๋กœ selAll์ด๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

    jquery์—์„œ๋Š” ์ด ์„ ํƒ์ž๋ฅผ ํ‘œ์ ์œผ๋กœ ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

        //SelectAll2
        $('.selectpicker').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
            if (clickedIndex == 0 && $(this).find('option:eq(0)').hasClass('selAll')) {
                if (isSelected) {
                    $(this).selectpicker('selectAll');
                } else {
                    $(this).selectpicker('deselectAll');
                }
            }
        });

    ํ•จ์ˆ˜ ์ž์ฒด๋Š” selectpicker์˜ ์ด๋ฒคํŠธ ์˜ต์…˜์„ ์‚ฌ์šฉํ•œ๋‹ค.

    data-actions-box๊ฐ€ ์‚ฌ์šฉ๋œ ๊ฒฝ์šฐ์—๋Š” ๋งˆ์น˜ ์ฒดํฌ๋ฐ•์Šค๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋˜์–ด ์žˆ์–ด์„œ ์„ ํƒํ•˜๋„๋ก ํ–ˆ๋‹ค.

      & .bs-actionsbox+.inner ul.dropdown-menu li {
        & a:not(.selected) {
          & span.check-mark {
            display: inline-block;
            margin-right: 5px;
    
            &:after {
              background: #fff url(/assets/images/bg/chkbox.png) left center no-repeat;
              border: 0 none;
              transform: rotate(0deg);
              width: 18px;
              height: 18px;
              border-radius: 2px;
            }
          }
        }
    
        & a.selected {
          & span.check-mark {
            position: relative !important;
            display: inline-block !important;
            margin-right: 5px;
            top: auto !important;
            right: auto !important;
    
            &:after {
              background: #fff url(/assets/images/bg/chkbox_on.png) left center no-repeat;
              border: 0 none;
              transform: rotate(0deg);
              width: 18px;
              height: 18px;
              border-radius: 2px;
            }
          }
        }
      }

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€