4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

haml とscssでプルダウンを作る

Posted at

カード有効期限を入力するプルダウンを作りたい!

こんなの作りたい〜〜〜
スクリーンショット 2019-06-29 18.51.12.png

:point_up_tone1:JSを使わずにhamlとscssだけでできる:bangbang:

.howtopay__card--month
 %select{:helper => ""}
  %option{:value => "1"} 01
  %option{:value => "2"} 02
  %option{:value => "3"} 03
  %option{:value => "4"} 04
  %option{:value => "5"} 05
  .card__month--icon
   = fa_icon("chevron-down") 
 .card__month--month 月

↑これでもできるけど冗長になってしまうのでこの好まれない:frowning2:

じゃあどうやればいいの:robot:

select_tagを使う

.howtopay__card--month
    = select_tag 'card[month]', options_for_select(["1", "2", "3", "4", "5"]) 

↓下向き矢印アイコン
.card__month--icon
 = fa_icon("chevron-down") 
.card__month--month 月

↑select_tagで書けばいい感じに短く簡単にできる:relaxed:

さらに、

controllerで

@card.month = params[:card][:month]

で値をとってこれる:ok_hand_tone1:

scssも

.howtopay__card {
            position: relative;
            padding-left: 0;
            &--month {
              overflow: hidden;
              width: 120px;
              margin-top: 8px;
              text-align: center;
              display: flex;
              select {
                width: 100%;
                padding-right: 1em;
                padding-left: 10px;
                cursor: pointer;
                text-indent: 0.01px;
                text-overflow: ellipsis;
                border: 1px solid $light-gray;
                height: 50px;
                border-radius: 4px;
                outline: none;
                background: transparent;
                box-shadow: none;
                -webkit-appearance: none;
                appearance: none;
              }
              .card__month--icon {
                color: $icon-gray;
                position: absolute;
                left: 80px;
                top: 5px;
                font-size: 1.5rem;
                font-weight: 100;
                cursor: pointer;
              }
              .card__month--month {
                position: absolute;
                top: 15px;
                left: 130px;
              }
              .card__year--icon {
                color: $icon-gray;
                position: absolute;
                left: 80px;
                top: 65px;
                font-size: 1.5rem;
                font-weight: 100;
                cursor: pointer;
              }
              .card__year--month {
                position: absolute;
                top: 75px;
                left: 130px;
              }
            }

こんな感じです。
:arrow_double_down:のアイコンをselect_tagの中に入れる方法がわからなかったので
最後の手段として、position: relative、absoluteで重ねました:fearful:

-webkit-appearance: none;

これは
スクリーンショット 2019-06-29 18.53.00.png

これをなくすためです:raised_hand_tone1:

参考

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?