$.dropdown

$.dropdown() creates dropdowns that integrate well with $.urlfilter and $.urlchange.

It requires the bootstrap-select library and its dependencies.

<link
  rel="stylesheet"
  href="ui/bootstrap-select/dist/css/bootstrap-select.min.css"
/>
<script src="ui/bootstrap-select/dist/js/bootstrap-select.min.js"></script>

Example:

<div class="container1"></div>
<script>
  $(".container1").dropdown({ data: ["Red", "Green", "Blue"] });
</script>

This renders a dropdown with 3 options – Red, Green, Blue.

<div class="container2"></div>
<script>
  $(".container2").dropdown({ key: "colors", data: ["Red", "Green", "Blue"] });
</script>

key enables urlfilter for dropdown. If Red option is selected from dropdown, URL is appended with ?colors=Red

If the labels are different from the values, you can use a list of {value: ..., label: ...} objects.

<div class="dropdown-object"></div>
<script>
  $('.dropdown-object').dropdown({
    data: [{ label: 'Red', value: 1 }, { label: 'Green', value: 2 }]
  ])
</script>

The data objects need not use label: and value: as keys. You can pick any other key using the label: and value: options.

<div class="dropdown-object"></div>
<script>
  $('.dropdown-object').dropdown({
    data: [{ x: 'Red', y: 1 }, { x: 'Green', y: 2 }],
    label_key: 'x',   // use data[*].x as the label, instead of data[*].label
    value_key: 'y'    // use data[*].y as the value, instead of data[*].value
  ])
</script>

You can set the default value of the dropdown using the value: key.

<div class="dropdown-default"></div>
<script>
  $(".dropdown-default").dropdown({
    key: "colors",
    data: ["Red", "Green", "Blue"],
    value: $("#color").val(url.searchKey.city), // Must match data string or .value
  });
</script>

By default, the selected dropdown values are appended to URL query string. To append to the hash instead, use target: '#'.

<div class="container3"></div>
<script>
  $(".container3").dropdown({
    key: "colors",
    data: ["Red", "Green", "Blue"],
    target: "#",
  });
</script>

To change URL without reloading the page, use target: 'pushState'.

<div class="container4"></div>
<script>
  $(".container4").dropdown({
    key: "colors",
    data: ["Red", "Green", "Blue"],
    target: "pushState",
  });
</script>

To use bootstrap-select options, use options:

<div class="container5"></div>
<script>
  $(".container5").dropdown({
    data: ["Red", "Green", "Blue"],
    key: "colors",
    options: {
      style: "btn-primary",
      liveSearch: true,
    },
  });
</script>
<div class="container6"></div>
<script>
  $(".container6")
    .on("load", function () {
      // Your code here
    })
    .on("change", function () {
      // Your code here
    })
    .dropdown({
      key: "colors",
      data: ["Red", "Green", "Blue"],
    });
</script>