2

I have used select2 type dropdown and bind with value-text pairs and it's doing filter properly based on (tenancyname) text as per below.

<li class="dropdown" id="liTenancy" style="margin-top: 4px; min-width: 250px !important;">                           
                        <select class="form-control select2me" data-placeholder="Select..." style="white-space: nowrap;">


                                    @foreach (var Tenancy in Entity)
                                    {
                                        <option @(SelectedTenancy == Tenancy.TenancyId ? "selected=\"selected\"" : "") value="@Tenancy.TenancyId">
                                            <a class="optionTenancy" href="javascript:void(0);" id="@Tenancy.TenancyId">@Tenancy.TenancyName</a>
                                        </option>
                                    }
                        </select>
                    </li>

Now i would like to filter this list with tenancyname (text) or tenancyid (value) (user can user both to filter list) then how can i customize js without doing it with remote call??

Arun Rana
  • 8,426
  • 14
  • 67
  • 107

1 Answers1

2

Thanks kevin for your suggestion, I found my solution as below which can be helpful to any other user

I have put id in value attribute and guid in alt attribute in option tag and use custom matcher as below

$('select.select2me').select2({
                placeholder: "Select",
                allowClear: true,                
                matcher: function (term, text, opt) {
                    return text.toUpperCase().indexOf(term.toUpperCase()) >= 0
                    || opt.attr("alt").toUpperCase().indexOf(term.toUpperCase()) >= 0
                    || opt.attr("value").toUpperCase().indexOf(term.toUpperCase()) >= 0;
                }
            });
Arun Rana
  • 8,426
  • 14
  • 67
  • 107