jquery spinner with strings as input

谁都会走 提交于 2019-12-03 20:25:46

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.

Bojan Hrnkas

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();
});

http://jsfiddle.net/k46bA/

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/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!