Align label and select dropdown in the same row in the sweetalert 2 [closed]
Asked Answered
C

2

1

I am looking for example where I can align label and dropdown in the same row using sweetalert2.

I have tried to add custom label to achieve this but it is going in the different line and I would like to console the selected dropdown on click of OK button.

Below is my stackblitz.

https://stackblitz.com/edit/angular-sweetalert-dropdown

enter image description here

Cellulosic answered 4/1, 2021 at 17:16 Comment(0)
M
1

Use inputLabel + customClass params:

Swal.fire({
  title: 'Select + label in one row',
  input: 'select',
  inputOptions: {
    apples: 'Apples',
    bananas: 'Bananas',
    oranges: 'Oranges'
  },
  inputLabel: 'Select a fruit',
  customClass: {
    input: 'inline-flex',
    inputLabel: 'inline-flex'
  }
})
.inline-flex {
  display: inline-flex !important;
  margin: .5em !important;
}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script> 

Read more about customClass: https://sweetalert2.github.io/#customClass

Manno answered 5/1, 2021 at 18:52 Comment(0)
P
1

You can use plain/styled html and catch the dropdown value normally as follows :

 Swal.fire({

  showCancelButton: true,
  type: "warning",
  html: ` select reason <select name="cars" id="cars">
          <option value="volvo">Volvo</option>
          <option value="saab">Saab</option>
          <option value="mercedes">Mercedes</option>
          <option value="audi">Audi</option>
          </select>`,

  preConfirm: function() {
  var elm = document.getElementById("cars");
  alert(elm.value); // use user selected value freely
  }

   });
Pretend answered 5/1, 2021 at 4:42 Comment(2)
but I will loose the mandatory validation on click of OK with this feature right?Cellulosic
As per this thread github.com/sweetalert2/sweetalert2/issues/247 unfortunately yes , but you can implement its logic yourself, or overwrite the default styling of the input field if you are ok with itPretend
M
1

Use inputLabel + customClass params:

Swal.fire({
  title: 'Select + label in one row',
  input: 'select',
  inputOptions: {
    apples: 'Apples',
    bananas: 'Bananas',
    oranges: 'Oranges'
  },
  inputLabel: 'Select a fruit',
  customClass: {
    input: 'inline-flex',
    inputLabel: 'inline-flex'
  }
})
.inline-flex {
  display: inline-flex !important;
  margin: .5em !important;
}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script> 

Read more about customClass: https://sweetalert2.github.io/#customClass

Manno answered 5/1, 2021 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.