3

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);
};
Community
  • 1
  • 1
Saeed Baba
  • 188
  • 2
  • 14
  • How is it not working exactly? What do you expect to happen which isn't happening? Are you getting the form data on server from client? Is the database lookup not working or what? – laggingreflex May 24 '15 at 13:23
  • It is about controllers I guess my controllers cannot identify search1 and search2 and variable1 and variable2 When I add those lines my app dos not show anything! Do I need to send the data from my view to controllers? Cause I Only have GET in the route and I think I need to send these variables to my controller. When I comment out these variables my view show the whole database means that I can read from database to my view So I think I need something to send data from my view to controller – Saeed Baba May 24 '15 at 20:28
  • If you have any simple search page which works this way that would be a great help – Saeed Baba May 24 '15 at 20:47
  • It may be tough to debug without a plunkr but this code in your AllsController is wrong: `$scope.search1 = search1; $scope.search2 = search2; $scope.variable1 = variable1; $scope.variable2 = variable2;` Those variables on the right side do not exist. The HTML view assumes that the variables referenced are in the $scope already so that is implied. – Garrett McCullough May 25 '15 at 17:20

1 Answers1

1

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.

bammons
  • 11
  • 2