我想構建一個簡單的搜索功能,但我找不出爲什麼我的代碼不工作。實現搜索功能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>
Criteria =>屬性;值=>該屬性的值;基本上,我試圖寫:Human.find()。其中({id:2})(例如:criteria = id; value = 2;) –
我試過了你提供的代碼,但它仍然沒有工作。也許我錯過了一些東西。我會盡力更好地解釋這個過程。我有一個搜索ID(使用Zurb Foundation Framework)的頁面上的按鈕:Search當有人點擊這個按鈕時,它會觸發我發佈並導航到localhost:1337/human/search的點擊事件;它返回一個空視圖; –
檢查編輯的答案 – ataman