我是一个新手,试图在Grails上找到Neo4J的用途。
基本上,我已经通过Neo4J浏览器创建了20个杂货项目节点,我想创建一个简单的Grails网站,让用户搜索杂货项目并直观地显示与之相关的项目。
我的 index.gsp 有:
<input id="item" />
我的可视化.js具有:
$('#item').keyup(function() {
var item = $('#item').val();
"我的项目域"类具有
class Item {
static mapWith = "neo4j"
String name
类具有:
def index() {
def item = Item.list() [item:item] //No idea, just trying out whatever i find :(
以及如下所示的查询:
def query = Item.cypherStatic ("""start n=node({Item}) match (n)-[r]->(x) where r='partner' return n, x)
问题:
- 如何正确地将 JS 'item' 变量发送到 ItemController?
- 如何使用"item"变量正确查询与项目有"伙伴"关系的节点名称?
除了Motilals答案之外,您肯定需要一个包装表单,其中包含指向控制器的操作
喜欢
<g:form controller="itemController" action="index" >
<input type="text" id="item" name="item" value="" />
<input type="submit" value="submit" >
</g:form>
然后在单击提交时,for将调用您的索引操作,在那里您可以使用
def item = params.item
但它看起来更像是你想要一些异步的东西在keyup函数之后,因此你可以像这样做:
$('#item').keyup(function() {
var item = $('#item').val();
$.ajax({
url: "${createLink(controller:'itemController', action:'index')}",
data:"&item="+item
})
.done(function( data ) {
console.log(data)
});
});
在这种情况下,您需要注意索引操作返回的内容,因此您可以在 .done() 中对响应执行任何您想要的操作。
另请注意,当您将操作命名为"索引"时,它将在
.../myproject/item/index
或者,这很重要
.../myproject/item/
因此,如果您的索引方法需要来自输入的数据,那么如果用户直接转到该 URL,它将错过它们
。因此,您的索引操作宁愿使用输入呈现页面然后定义另一个操作,用于根据输入和返回数据执行查询
项目设置为隐藏字段,然后您可以使用参数直接在控制器中访问它
给你:
//in index.gsp add below hidden field and set the hidden filed in your js code
<g:hiddenField name="item" value="" />
$('#item').keyup(function() {
var item = $('#item').val();
//in your controller
def index() {
def item = params.item
print item // you see the value for item
//do your stuff
}
获得项目值后,可以直接使用 HQL 查询或使用域实例
希望这对你有帮助
问候
莫蒂拉尔