当我尝试创建自定义代码编辑器时,我正在使用p:inputTextArea标记自动完成代码。当我键入并输入字母时,下拉菜单将显示相应的建议。很好但是问题是如果我键入“ se”,它可能会在下拉列表中显示house。但是,当我选择它时,输出将是“ seuse”。单词的前两个字母将替换为用户键入的单词。 在这种情况下我该怎么办? 以下是我的XHTML文件。
<h:form id="formId">
<p:autoComplete class="ui-auto" id="text1" value = "#{username.d}"
completeMethod="#{dbConnect.completeArea}" queryDelay="1" minQueryLength="1">
<f:converter converterId = "www" />
</p:autoComplete>
<br></br>
<h:commandButton type="button" value = "submit" action = "t1" onclick="myFunction()"/>
<br></br> <p:outputLabel for="text1" id="demo"></p:outputLabel>
</h:form>
<script type="text/javascript">
const keywords = {
IF: {style: "code-elem", indent: 4},
ENDIF: {style: "code-elem", indent: -4},
IFLISTING: {style: "code-str", indent: 4},
ENDIFLISTING: {style: "code-str", indent: -4},
VAR: {style: "code-comment", indent: 0},
LISTING: {style: "code-comment", indent: 0}
};
function myFunction() {
let indent = 0;
document.getElementById('formId:demo').innerHTML = document.getElementById('formId:text1_input').value.split(/[\r\n]+/).map(line => {
const oldIndent = indent;
line = line.trim().replace(/###([A-Z]+)(.*?)###/g, (m, keyword, arg) => {
const param = keywords[keyword];
if (!param)
return m;
indent += param.indent;
return '<span class="' + param.style + '">' + m + '</span>';
});
return " ".repeat(Math.min(indent, oldIndent)) + line;
}).join("<br/>");
}
window.onload = myFunction;
</script>
如果有什么用,以下也是我的java文件。
public List<String> completeArea(String inputName) {
List<String> result = new ArrayList<>();
try {
Class.forName("com.mysql.jdbc.Driver");
String query1 = "select name from labels where name like ? order by name";
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost/company", "root", "");
PreparedStatement pst = con.prepareStatement(query1)) {
pst.setString(1,"%" + inputName + "%");
try (ResultSet rs = pst.executeQuery()) {
while (rs.next()) {
result.add(rs.getString(1));
}
}
}
} catch (Exception ex) {
System.out.println("error occured" + ex);
}
return result;
}
我编辑了我的问题,因为@selaron说p:autoComplete无法解决我的问题。