问题
I have a dropdown list and I am using the Select2 jquery plugon which works great.
One thing i ran into is a use case where each of the items is very long so the text wraps to 3 or 4 lines. I wanted to see if you can style the dropdowns to have alternative row color to make it easier to see where one item ends and another begins when you are scrolling down the list of items.
Is this possible?
回答1:
Yes, it's possible, in fact it's quite easy, this is how you do it:
In your css stylesheet create a class for the items that you want to display in a different color, for instance:
.oddRow {background-color:#CCC;}
Then add that class to the option
elements that you want to be displayed in a different color before the call to select2
, like this:
$(document).ready(function() {
$("#source optgroup option:odd").addClass('oddRow');
$("#source").select2();
});
See a working example in this Plunker or in the embed snippet bellow.
$(document).ready(function() {
$("#source optgroup option:odd").addClass('oddRow');
$("#source").select2();
});
.oddRow {background-color:#CCC;}
<script data-require="jquery@*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link data-require="select2@*" data-semver="3.5.0" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.css" />
<script data-require="select2@*" data-semver="3.5.0" src="//cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.js"></script>
<select style="width:300px" id="source">
<optgroup label="Alaskan/Hawaiian Time Zone">
<option value="AK">Alaska</option>
<option value="HI">Hawaii</option>
</optgroup>
<optgroup label="Pacific Time Zone">
<option value="CA">California</option>
<option value="NV">Nevada</option>
<option value="OR">Oregon</option>
<option value="WA">Washington</option>
</optgroup>
<optgroup label="Mountain Time Zone">
<option value="AZ">Arizona</option>
<option value="CO">Colorado</option>
<option value="ID">Idaho</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NM">New Mexico</option>
<option value="ND">North Dakota</option>
<option value="UT">Utah</option>
<option value="WY">Wyoming</option>
</optgroup>
<optgroup label="Central Time Zone">
<option value="AL">Alabama</option>
<option value="AR">Arkansas</option>
<option value="IL">Illinois</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="OK">Oklahoma</option>
<option value="SD">South Dakota</option>
<option value="TX">Texas</option>
<option value="TN">Tennessee</option>
<option value="WI">Wisconsin</option>
</optgroup>
<optgroup label="Eastern Time Zone">
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="IN">Indiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="OH">Ohio</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WV">West Virginia</option>
</optgroup>
</select>
回答2:
You can do this just by adding two css classes in your css file, or at the top of the file that contain the select field.
Add the following:
.select2-results li:nth-child(2n+1){
background-color:red;
}
Where select2-results
is an outer class of ul
tag. So please change it if your are using some different class.
来源:https://stackoverflow.com/questions/25814005/does-jquery-select2-plugin-support-the-ability-to-have-alternative-row-colors-in