p:一旦达到最大值,微调器就不会将值从最大值重置为最小值

时间:2018-01-15 11:19:58

标签: jsf primefaces spinner

在PrimeFaces 6.1 p:spinner中,一旦值超过最大值,该值就不会重置为最小值。它应该重置为最小值,但它没有重置(我认为这是微调器的定义)。如果PrimeFaces不提供此功能,那么我们如何实现这一目标呢?

1 个答案:

答案 0 :(得分:1)

PrimeFaces的来源是开放式的。所以你总能看看它。在这种情况下spinner.js

在旋转功能中你会看到以下内容(ll.165-167):

if(this.cfg.max !== undefined && newValue > this.cfg.max) {
    newValue = this.cfg.max;
}

如您所见,如果newValue超过最大值,则newValue将设置为最大值(而不是您想要的最小值)。 因此PrimeFaces不支持此功能。最佳选择是PrimeFaces的功能请求。

另一种选择是覆盖旋转函数的上述部分:

使用以下内容创建一个.js文件(在这种情况下,它被称为spinner_adaption.js):

(function() {
    PrimeFaces.widget.Spinner.prototype.spin = function(dir) {
    var step = this.cfg.step * dir,
    currentValue = this.value ? this.value : 0,
    newValue = null;

    if(this.cfg.precision)
        newValue = parseFloat(this.toFixed(currentValue + step, this.cfg.precision));
    else
        newValue = parseInt(currentValue + step);

    if(this.cfg.maxlength !== undefined && newValue.toString().length > this.cfg.maxlength) {
        newValue = currentValue;
    }

    if(this.cfg.min !== undefined && newValue < this.cfg.min) {
        newValue = this.cfg.max;
    }

    if(this.cfg.max !== undefined && newValue > this.cfg.max) {
        newValue = this.cfg.min;
    }

    this.value = newValue;
    this.format();
    this.input.attr('aria-valuenow', newValue);  
    }
})();

它是原始功能的副本。我刚刚在上面提到的newValue = this.cfg.max中将newValue = this.cfg.min更改为newValue = this.cfg.min 一部分。

包括要添加功能的脚本,例如:

<h:body>
    <h:form id="form">
        <h:outputScript name="js/spinner_adaption.js" />
        <p:spinner id="spinner" value="1" min="1" max="3" />
    </h:form>
</h:body>

但是,在更新到较新的PrimeFaces版本时,请小心覆盖类似的内容。