计算p:inputTextarea中使用的字符,而不是剩余的字符 [英] Count characters used in p:inputTextarea instead of characters remaining
问题描述
我早些时候发布了一个类似的问题,但是我正在寻找一种不同的方法.我有一个inputTextArea,我需要限制可以输入的字符数量以及显示使用的字符数量(例如:使用的31/50个字符").当前,我只是使用inputTextArea的属性来执行此操作,但是,通过这种方式,我只能获得剩余字符的计数.下面是inputTextArea的图片以及执行此操作的代码.
I posted a similar question earlier but I'm looking for a different approach. I have a inputTextArea and I need to limit the amount of characters that can be entered in to it as well as display the amount of characters used (ex: "31/50 characters used"). Currently, I am just using attributes for the inputTextArea to do this, however, this way, I can only get a count for the characters remaining. Below is a picture of the inputTextArea as well as the code to do this.
<p:inputTextarea id="ogdGacOther" rows="1" style="vertical-align: top; width: 98% !important;" styleClass="preformatted" autoResize="false" value="#{rfeBean.rfe.targetOfficerReasonsForReferral.otherOgdGac}" disabled="#{!rfeBean.ogdGacReferralReasonsChecked[ReferralTemplateConstants.OGD_GAC_OTHER] or !raudUserSessionBean.raudUser.referMode or rfeBean.workItemMissing}"
counter="displayGacCharCount" maxlength="50" counterTemplate="{0} #{msg.rfeCharactersRemaining}">
<p:ajax event="change" process="@this" />
</p:inputTextarea>
<br/>
<h:outputText id="displayGacCharCount" />
我想知道是否有方法可以修改它,以便显示所使用的字符.计数器模板中的{0}是代表剩余的char数量的数字.如果有人知道我可以使用该{0}调用Java方法的方法,那将是无与伦比的.无论哪种方式,请让我知道您认为最佳的方法.我试图避免使用jQuery的任何JavaScript.
I am wondering if there is way to modify this so that it displays characters used. The {0} in the counter template, is the number that represents the amount of char remaining. If anyone know a way I could call a java method using that {0}, I would be golden. Either way, let me know what you think the best approach. I am trying to avoid using any javascript of jQuery.
我尝试过这样的事情:
<h:outputText id="displayGacCharCount" value="#{rfeBean.methodToChangeCharRemainToCharUsed(valueForCharRemain({0})}" />
尝试此操作时,出现类似"El表达式不平衡"的错误消息
When I tried this, I got an error along the lines of "El Expression is unbalanced"
推荐答案
利亚姆
这很简单,忘记计数器并获取字符串长度:
It's very simple, forget the counter and get the string length:
<p:inputTextarea id="ogdGacOther" rows="1" maxlength="50"
value="#{rfeBean.rfe.targetOfficerReasonsForReferral.otherOgdGac}">
<p:ajax event="keyup" update="displayGacCharCount" global="false"/>
<p:ajax event="keydown" update="displayGacCharCount" global="false"/>
<p:ajax event="keypress" update="displayGacCharCount" global="false"/>
</p:inputTextarea>
<h:outputText id="displayGacCharCount"
value="#{rfeBean.rfe.targetOfficerReasonsForReferral.otherOgdGac.length()}"/>
这篇关于计算p:inputTextarea中使用的字符,而不是剩余的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!