Add custom data attributes to Semantic UI Dropdown when items are retrieved via Ajax

ardavis

Here is how I was building my dropdown (in haml):

#my_dropdown.ui.dropdown{ data: { path: objects_path } }
  = f.hidden_field :my_id
  %i.dropdown.icon
  .default.text Text
  .menu
    - @collection.each do |object|
      .item{ data: { value: object.id, text: object.name, color: object.color } }
        = object.name

This was working great. My .item elements had extra data, such as .data('color'). I decided I wanted to switch to an ajax approach instead of loading all of the elements at once.

$.fn.api.settings.api =
    'search objects': $('#my_dropdown').data('path') + '?q={query}'

$('#my_dropdown').dropdown
    apiSettings:
      action: 'search objects'

    onChange: (value, text, selectedItem) ->
      console.log selectedItem.data()

Here is what my returned JSON looks like:

{
  "success":true,
  "results": [
    { "name":"Object #1", "value":1, "color":"blue" },
    { "name":"Object #2", "value":2, "color":"red" },
    { "name":"Object #3", "value":3, "color":"green" }
  ]
}

When I do the console.log selectedItem.data() after the dropdown select is changed, I only see:

{ value: 1 }

And the generated HTML looks like:

<div class="item" data-value="1">Object #1</div>
<div class="item" data-value="2">Object #2</div>
<div class="item" data-value="3">Object #3</div>

How can I get the items to include the custom data attributes, such as data-color while using Ajax?

ardavis

I was able to manually override the Semantic UI menu template with the following code:

$.fn.dropdown.settings.templates =
  menu: (response, fields) ->
    values = response[fields.values] or {}
    html = ''
    $.each values, (index, option) ->
      extraData = ''
      $.each fields.extraAttrs, (_j, attr) ->
        if option[attr]
          extraData += "data-#{attr}='" + option[attr] + "''"

      maybeText = if option[fields.text] then 'data-text="' + option[fields.text] + '"' else ''
      maybeDisabled = if option[fields.disabled] then 'disabled ' else ''
      html += '<div class="' + maybeDisabled + 'item" data-value="' + option[fields.value] + '"' + extraData + maybeText + '>'
      html += option[fields.name]
      html += '</div>'
      return
    html

Then when I'm initializing my dropdown, I can do:

$('#my_dropdown').dropdown
    apiSettings:
      action: 'search objects'
    fields:
      extraAttrs: ['color']

That allows me to access extra data elements on each of the dropdown items. If someone has a better way to do this, please let me know.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

semantic ui dropdown when hover

Inline items in semantic ui dropdown menu

How to add icon to semantic ui dropdown?

In Semantic-UI-React, is there a way to add an x icon to a text input or dropdown that will clear the text when clicked?

Searchable dropdown in semantic-ui-react does not immediately display full list of options retrieved from API

How to display the values of the attributes of the data queried and retrieved by Ajax call in django

Semantic dropdown ui

Add Avatar (user profile photo) to Semantic UI React's Dropdown

Add custom data in AJAX

Semantic UI react, render dropdown/modal only when it's visible?

How to trigger a modal when clicking on a dropdown item in Semantic UI React?

Selected value from semantic ui react dropdown not appearing when selected

Semantic-UI Dropdown doesn't show when using sidebar

Semantic-UI Dropdown searches also in the 'data-value'

How to pass the data with dynamic key in semantic UI vue dropdown?

How to add data to a form when submitting via a POST AJAX request?

How to add custom color to menu in tab - Semantic UI React

Dropdown in Semantic UI is not working on Mobile

Semantic UI dropdown change handler

Semantic UI, remove autofoucs on dropdown

Semantic-ui dropdown is not working

Dynamic Dropdown using semantic ui

Semantic-ui dropdown sizing

Add fading transition to items loaded via ajax

KendoUI DropDown list - Add custom data fields

Is there any way to add custom attributes in Keycloak via REST API?

Aurelia and Semantic UI - custom theme

Asp.Net data attributes dynamically updated through JS/jQuery but the updated attributes aren't updated when retrieved through code behind

Howto have a Semantic UI React Dropdown auto-focus when its parent component mounts?