2013-08-20 231 views
0

我想构建一个简单的搜索功能,但我找不出为什么我的代码不工作。实现搜索功能sailsjs

这是我建立了搜索行动:我有我的主网页上的按钮

search: function(req, res) { 

    var criteria = req.param('criteria'); 

    var value = req.param('value'); 


    Human.find().where({ criteria: value }).done(function(err, humans) { 
     if(err) { 
     return console.log('Error:' + err); 
     }else{ 
     res.view({ 
      title: 'Search', 
      humans: humans 
     }); 
     } 
    }); 
} 

与搜索的ID。我想这样做只要有人点击我的搜索按钮,它就会查询数据库并在localhost:1337/model/search处返回结果。到目前为止,我已经尝试使用两个变量(条件,值)向该控制器操作发送ajax请求,但它不起作用。

这是AJAX调用,我提出:

$('#search').on('click', function() { 
    var criteria = $('#filter').val(); 

    var value = $('#value').val(); 

    $.ajax({ 
     type: 'POST', 
     url: 'http://localhost:1337/human/search', 
     cache: false, 
     data: { 
      criteria: criteria, 
      value: value 
     }, 
     success: function(data) { 
      console.log('SUCCESS!'); 
      window.location.href = 'http://localhost:1337/human/search'; 
     }, 
     error: function(err) { 
      console.log('ERROR!'); 
     } 
    }); 
}); 

这是对应的视图:

<table> 
    <thead> 
     <tr> 
    <th>ID</th> 
    <th width="150">First Name</th> 
    <th width="150">Last Name</th> 
    <th width="150">Contact</th> 
    <th width="150">E-Mail</th> 
    <th>View</th> 
    <th>Edit</th> 
    </tr> 
</thead> 

<tbody> 
<% _.each(humans, function(model) { %> 
<tr> 
<td> <%= model.id %> </td> 
<td> <%= model.firstName %> </td> 
<td> <%= model.lastName %> </td> 
<td> <%= model.contact %> </td> 
<td> <%= model.email %> </td> 
<td><a href="/human/view/<%= model.id %>" class="tiny button">VIEW</a></td> 
<td><a href="/human/edit/<%= model.id %>" class="tiny button">EDIT</a></td> 
    </tr> 
<% }) %> 
</tbody> 
</table> 

回答

1

Promlem#1:当您搜索这样的模式:Human.find().where({ criteria: value }) ,您实际上按名称为“标准”的字段进行搜索,而不是按字段进行搜索,该变量名称保存在criteria变量中。 尝试创建搜索对象是这样的:

var searchObj = {}; 
searchObj[criteria] = value; 
// and then search like you did before 
Human.find().where(searchObj).done(function(err, humans) { 
    if(err) { 
    console.log('Error:' + err); 
    // you should return some response here: 
    res.send(err, 500); 
    }else{ 
    res.view({ 
     title: 'Search', 
     humans: humans 
    }); 
    } 
}); 

问题2:你为什么AJAX请求,然后做重定向到相同的URL?
首先,你做了POST请求,虽然GET请求更适合搜索引擎。通常在您使用创建资源时使用POST。
其次,在阿贾克斯成功处理程序,您会收到与发现人类模型视图后,你只需浏览器重定向到URL http://localhost:1337/human/search没有传递任何参数,所以控制器将尝试空值和标准Human.find().where({ "": "" })进行搜索。所以你不会看到预期的结果。

目前还不清楚您是想通过ajax获取数据,还是仅仅将其显示在新的HTML页面中?

编辑:如果你不希望使用AJAX,让HTML表单为你做的工作:

<form action="human/search"> 
     <input name="criteria" value="foo"> 
     <input name="value" value="bar"> 
     <button type="submit" id="search">Search</button> 
    </form> 

搜索按钮,点击会提交表单,并通过在所有形式的数据GET请求的查询字符串:http://localhost:1337/human/search?criteria=foo&value=bar

当然,您可以使用javascript手动构建查询字符串,而不使用表单,并将浏览器重定向到该URL。结果将是相同的。

+0

Criteria =>属性;值=>该属性的值;基本上,我试图写:Human.find()。其中​​({id:2})(例如:criteria = id; value = 2;) –

+0

我试过了你提供的代码,但它仍然没有工作。也许我错过了一些东西。我会尽力更好地解释这个过程。我有一个搜索ID(使用Zurb Foundation Framework)的页面上的按钮:Search当有人点击这个按钮时,它会触发我发布并导航到localhost:1337/human/search的点击事件;它返回一个空视图; –

+0

检查编辑的答案 – ataman