Hi I searched but i didn't find such solution.Is it possible to create a spinner with jquery which has text values (strings) as input instead of numbers.Or in other words the same as this http://jsfiddle.net/yaQSP/ but instead ..-1,0,1.. to spin text values from list or array of strings.
Update
Just cleaned that up a bit http://jsfiddle.net/yaQSP/25/
see: http://jsfiddle.net/yaQSP/23/
You could do it a bit hacky like this: Create your Array.
Set:
$('#spinner').spinner({
step: 1,
numberformat: "n"
});
and bind an change event on the input field. Then call it like this ->
yourArr[i]
whereas i
is the value of the input field.
Previous answer does not work. I tested it under given links with chrome and firefox.
Look here for another solution.
You need to overwrite the _parse and _format functions of the spinner widget.
Here is an example with just simple Bool-like spinner:
$.widget("ui.boolspinner", $.ui.spinner, {
options: {
min: 0,
max: 1,
start: 1
},
_parse: function (value) {
if (typeof value === "string") {
return (value.toLowerCase() == "ja")?1:0;
}
return value;
},
_format: function (value) {
return (value == 1)?"Ja":"Nein";
}
});
$(function() {
$("#spinner-testprint").boolspinner();
});
I finished my own widget. It uses a string array as input.
$.widget("ui.formatSpinner", $.ui.spinner, {
options: {
},
_parse: function (value) {
if (typeof value === "string") {
return this.options.values.indexOf(value);
}
return value;
},
_format: function (value) {
//wrap around
if (value < 0) {
value = this.options.values.length-1;
}
if (value > this.options.values.length-1) {
value = 0;
}
var format = this.options.values[value];
return format;
},
});
Usage:
//paper type spinner
var arrPaperTypes = ["Standard 80g", "Standard 100g", "Gloss 100g", "Gloss 120g"];
$(function() {
$("#spinner-paper").formatSpinner({
values: arrPaperTypes
});
});
Building on from Bojan's answer, with a little finessing, and by overwriting _adjustValue, a cleaner solution is available:
$.widget("ui.textSpinner", $.ui.spinner, {
options: {
wrap: true
},
_parse: function (value) {
if ((value === '') || isNaN(value)) {
value = this.options.values.indexOf(value);
if (value === -1) {
value = 0;
}
}
if (value < 0) {
value = this.options.wrap ? (this.options.values.length -1) : 0;
} else if (value >= this.options.values.length) {
value = this.options.wrap ? 0 : (this.options.values.length - 1);
}
return value;
},
_format: function (value) {
return this.options.values[value];
},
_adjustValue: function (value) {
if (value < 0) {
value = this.options.wrap ? (this.options.values.length - 1) : 0;
} else if (value >= this.options.values.length) {
value = this.options.wrap ? 0 : (this.options.values.length - 1);
}
return value;
}
});
var arrText = ["John", "Paul", "George", "Ringo"];
$(function() {
$("#spinner-text").textSpinner({
values: arrText,
spin: function( event, ui ) {
$( "#spinner-value" ).text(ui.value);
}
});
$("#spinner-text-nowrap").textSpinner({
values: arrText,
wrap: false,
spin: function( event, ui ) {
$( "#spinner-value-nowrap" ).text(ui.value);
}
});
});
<link href="http://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<p>
Wrapping <input id="spinner-text" value="3" readonly /><br />
Value: <span id="spinner-value">3</span>
</p>
<p>
No wrapping <input id="spinner-text-nowrap" value="3" readonly /><br />
Value: <span id="spinner-value-nowrap">3</span>
</p>
Also available here: http://jsfiddle.net/MartynDavis/gzmvc2ds/
来源:https://stackoverflow.com/questions/9245047/jquery-spinner-with-strings-as-input