HemantNegi / jquery.sumoselect

A jQuery Single/Multi Select plugin which can be used on almost any device
http://hemantnegi.github.io/jquery.sumoselect
559 stars 287 forks source link

Add Data attribute to select option #335

Open BlackBurnVP opened 2 years ago

BlackBurnVP commented 2 years ago

For now it's impossible to add some data attributes to select options. If we add it to html

  • Zenoo commented 2 years ago

    Could you give me an example of what you would need?

    AzzaAzza69 commented 2 years ago

    I think he would like the attributes set against the regular options transferring to the equivalent "li". so when this is "converted" to a sumoselect: {select} {option data-whatever="test">option1{/option} {option data-something="else">option2{/option} {/select}

    it ends up like this: {div class="optWrapper"} {ul class="options"} {li class="opt selected" data-whatever="test"}{label}option1{/label}{/li} {li class="opt" data-something="else"}{label}option2{/label}{/li} {/ul} {/div}

    BlackBurnVP commented 2 years ago

    Yes. It's what I want to achieve. Thank you

    I think he would like the attributes set against the regular options transferring to the equivalent "li". so when this is "converted" to a sumoselect: {select} {option data-whatever="test">option1{/option} {option data-something="else">option2{/option} {/select}

    it ends up like this: {div class="optWrapper"} {ul class="options"} {li class="opt selected" data-whatever="test"}{label}option1{/label}{/li} {li class="opt" data-something="else"}{label}option2{/label}{/li} {/ul} {/div}

    Zenoo commented 2 years ago

    I see, I will handle this as soon as I have time

    AzzaAzza69 commented 2 years ago

    This would do it: in the createLi()...

    I changed:

         // AzzaAzza69 : create without any attributes (including class!)
         const li = $(`<li><label>${opt.html()}</label></li>`);
    
         // +AzzaAzza69 : copy all attributes from original option to new list item
         for(attr of opt[0].attributes) {
           li[0].setAttribute(attr.name, attr.value);
         }
         li[0].classList.add('opt');
    ...
    // -AzzaAzza69 : new attribute copy will pull these across
    //          if (opt.attr('class'))
    //            li.addClass(opt.attr('class'));
    
    //          if (opt.attr('title'))
    //            li.attr('title', opt.attr('title'));
    ...