Reputation: 188
I have two Models named "Jeans" and "Shirts" which have two variables "name" and "color"
I want to have a search page in which user can search through database to find shirts and jeans in specific color or name.
This link might be a hint but I just could not figure it out Similar Problem
I have got something here But it does not work. I appreciate if you tell me how to fix it.Thanks!
View
<section data-ng-controller="AllsController" data-ng-init="find()">
<div class="page-header">
<h1>Alls</h1>
</div>
<div class="Search">
<h2>Search Section</h2>
<select data-ng-model="search1" id="search">
<option value="Model1">Jeans</option>
<option value="Model2">Shirts</option>
</select>
<select data-ng-model="search2" id="search">
<option value="Model1">Jeans</option>
<option value="Model2">Shirts</option>
</select>
<select data-ng-model="variable1" id="variable1">
<option selected="selected">AnyThing</option>
<option value="color1">Blue</option>
<option value="color2">Red</option>
</select>
<select data-ng-model="variable2" id="variable2">
<option selected="selected">AnyThing</option>
<option value="name1">John</option>
<option value="name2">Bob</option>
</select>
</div>
<br></br><br></br>
<h2>Result Section</h2>
<div class="list-group">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Color</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="all in alls">
<td data-ng-bind="all.name"></td>
<td data-ng-bind="all.color"></td>
</tr>
</tbody>
</table>
</div>
In controller; first I see which properties user has selected and assign it to FindArray Then I see in which model user want to search.(AnyThing means that user has not selected anything)
Server Side Controller
exports.list = function(req, res) {
if (variable1 === "AnyThing")
{
if (variable2 === "AnyThing")
{
FindArray = {};
}else
{
FindArray = { name = variable2 };
}
}else
{
FindArray = { color = variable1 , name = variable2 };
}
if (req.body.search1 === 'Jeans')
{
if (req.body.search2 === 'Shirts')
{
Jeans.find(FindArray).sort('-created').populate('user', 'displayName').exec(function(err, jeans) {
Shirt.find(FindArray).sort('-created').populate('user', 'displayName').exec(function(err, shirts) {
var all = shirts.concat(jeans);
res.jsonp(all);
});
});
}else{
Jeans.find(FindArray).sort('-created').populate('user', 'displayName').exec(function(err, jeans) {
res.jsonp(jeans);
});
}
}else{
Shirt.find(FindArray).sort('-created').populate('user', 'displayName').exec(function(err, shirts) {
res.jsonp(shirts);
});
}
};
$Resource Service:
angular.module('alls').factory('Alls', ['$resource',
function($resource) {
return $resource('alls/:allId', { allId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}]);
Client Side Controller
angular.module('alls').controller('AllsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Alls', 'Jeans', function($scope, $stateParams, $location, Authentication, Alls, Jeans) {
$scope.find = function() {
$scope.search1 = search1;
$scope.search2 = search2;
$scope.variable1 = variable1;
$scope.variable2 = variable2;
$scope.alls = Alls.query();
};
}]);
Server Side Route
module.exports = function(app) {
var alls = require('../../app/controllers/alls.server.controller');
app.route('/alls').get(alls.list);
};
Upvotes: 3
Views: 1004
Reputation: 11
Preliminary review of this looks to me like your route is expecting a get, but your $resource is running a put. I'm also thinking that all of your scope assignments aren't going to work because you're assigning non existent variables.
Upvotes: 1