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); } });
最佳答案
我在网上寻找您的问题,但也找不到任何有用的解决方案。所以我决定使用与我的 answer 相同的方法.
这是我的建议:
从 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 的人(我不是)可以为这个函数编写紧凑的脚本。但这对我有用。
请给我意见 ;) 我也是新来的。
关于javascript - 在 primefaces 中使用颜色选择器的 JQuery 自然接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18846248/