首页 文章

在primefaces中使用颜色选择器的JQuery自然界面

提问于
浏览
3

Primefaces 3.5,Mojara 2.1.21,Omnifaces 1.5

我想使用Primefaces组件颜色选择器来选择颜色并更新文本框中的颜色 .

<h:outputText value="#{bean.color}" id="colorId"/>
<p:colorPicker value="#{bean.color}" />

那么问题是如何在h:outputText中更新值(我只需要客户端) .

JQuery颜色选择器组件有一个很好的接口来做到这一点 . 但是我怎么能用呢?如何在生成的组件的颜色选择器中注册onChange事件?

$('#colorSelector').ColorPicker({
    color: '#0000ff',
    onChange: function (hsb, hex, rgb) {
        $('#colorSelector div').css('backgroundColor', '#' + hex);
    }
});

1 回答

  • 1

    我在网上寻找你的问题,但也找不到任何有用的解决方案 . 所以我决定使用与我的_892166中相同的方法 .

    这是我的建议:

    从primefaces获取JS代码并重写它:

    <h:form prependId="false">
    
                    <h:outputText value="t#{testas.color}" id="colorId3"/>
                    <p:colorPicker id="cid" value="#{testas.color}" widgetVar="co" />           
    
            </h:form>
    
    
            <script type="text/javascript">
    
            PrimeFaces.widget.ColorPicker = PrimeFaces.widget.BaseWidget.extend({
    
                init: function(cfg) {
                    this._super(cfg);
    
                    this.input = $(this.jqId + '_input');
                    this.cfg.popup = this.cfg.mode == 'popup';    
                    this.jqEl = this.cfg.popup ? $(this.jqId + '_button') : $(this.jqId + '_inline');
                    this.cfg.flat = !this.cfg.popup;
                    this.cfg.livePreview = false;
                    this.cfg.nestedInDialog = this.jqEl.parents('.ui-dialog:first').length == 1;
    
                    this.bindCallbacks();
    
                    //ajax update check
                    if(this.cfg.popup) {
                        this.clearOrphanOverlay();
                    }
    
                    //create colorpicker
                    this.jqEl.ColorPicker(this.cfg);
    
                    //popup ui
                    if(this.cfg.popup) {
                        PrimeFaces.skinButton(this.jqEl);
                        this.overlay = $(PrimeFaces.escapeClientId(this.jqEl.data('colorpickerId')));
                        this.livePreview = $(this.jqId + '_livePreview');
                    }
                },
    
                bindCallbacks: function() {
                    var _self = this;
    
                    this.cfg.onChange = function(hsb, hex, rgb) {
                        _self.input.val(hex);
    
                        if(_self.cfg.popup) {
    
                            _self.livePreview.css('backgroundColor', '#' + hex);
                        }
                    };
    
                    this.cfg.onShow = function() {
                        if(_self.cfg.popup) {
                            _self.overlay.css('z-index', ++PrimeFaces.zindex);
                        }
    
                        var win = $(window),
                        positionOffset = _self.cfg.nestedInDialog ? '-' + win.scrollLeft() + ' -' + win.scrollTop() : null;
    
                        if(_self.cfg.nestedInDialog) {
                            _self.overlay.css('position', 'fixed');
                        }
    
                        //position the overlay relative to the button
                        _self.overlay.css({
                                    left:'',
                                    top:''
                            })
                            .position({
                                my: 'left top'
                                ,at: 'left bottom'
                                ,of: _self.jqEl,
                                offset : positionOffset
                            });
                    }
    
                    this.cfg.onHide = function(cp) {
                        _self.overlay.css('z-index', ++PrimeFaces.zindex);
    
                        $('#colorId3').html(_self.input.val()); // -> ADDED BY ME
    
                        $(cp).fadeOut('fast');
                        return false;
                    }
                },
    
                /**
                 * When a popup colorpicker is updated with ajax, a new overlay is appended to body and old overlay
                 * would be orphan. We need to remove the old overlay to prevent memory leaks.
                 */
                clearOrphanOverlay: function() {
                    var _self = this;
    
                    $(document.body).children('.ui-colorpicker-container').each(function(i, element) {
                        var overlay = $(element),
                        options = overlay.data('colorpicker');
    
                        if(options.id == _self.id) {
                            overlay.remove();
                            return false;   //break;
                        }
                    });
                }
    
            });
    
            </script>
    

    我添加了这部分:$('#colorId3') . html(_self.input.val());

    我希望有人知道JQuery(我不是),可以为这个函数编写紧凑的脚本 . 但这对我有用 .

    请给我这个意见;)我也是新来的 .

相关问题