Muhammad Shahzad
Muhammad Shahzad

Reputation: 9652

Category and subcateory in nodejs and mongodb

I want to build a Category and Subcategory feature in MEAN application.

I have a model for ProductCategoryModel.js like this:

var mongoose = require('mongoose');

var productCategorySchema = mongoose.Schema({
    title:{ type:String, required:true },
    slug:{ type:String, required:true },
    status:{ type:Number, required:true, default:1},
    description:{ type:String, required:true },
    post_type:{type:String, default:'product_catgory'},
    parentID:{ type:mongoose.Schema.Types.ObjectId, ref:'ProductCategory', default:null},
});
module.exports = mongoose.model('ProductCategory', productCategorySchema);

Product category controller

adminApp.controller('ProductCategoryModalInstanceCtrl', function ($scope, productcategory, $http, $uibModalInstance) {

$scope.productCatList =  [];    
return $http.get('/api/product-category/parent-cat-list')
      .then(function (result) {
        $scope.productCatList =  result.data;
      });

});

product category form

<div class="form-group" ng-class="{ 'has-error' : productCategoryForm.parentID.$invalid && !productCategoryForm.parentID.$pristine }">
 <label>Parent Category</label>
<select name="parentID" class="form-control" id="parentID" ng-model="productcategory.parentID">
    <option ng-repeat="category in productCatList" value="{{category._id}}">
    {{category.title}}</option>
</select>
</div>

Now my issue is I want to show sub categories of selected parent category in new dropdown, How I can do this,I think there should a new dropdown and update its content when change parent category dropdown.

And second issue is this line of code not returning me Parent categories where parentID=null, It return me list of all categories.

return Promise.denodeify(Models.ProductCategoryModel.find.bind(Models.ProductCategoryModel))({ parentID: null })
    .then(function (results) {
      return results;
    });

Upvotes: 3

Views: 4195

Answers (1)

SiddAjmera
SiddAjmera

Reputation: 39432

You can do something like this :

adminApp.controller('ProductCategoryModalInstanceCtrl', function ($scope, productcategory, $http, $uibModalInstance) {
        $scope.productCatList =  [];
        return $http.get('/api/product-category/parent-cat-list')
                    .then(function (result) {
                          $scope.productCatList =  result.data;
                      });

        $scope.$watch('productcategory.parentID', function(newValue, oldValue, scope) {
              if(newValue != oldValue){
                    $http.get('/api/product-category/' + $scope.productcategory.parentID)
                         .then(function (result) {
                                $scope.productSubCatList =  result.data;
                         });
              }
        });
  });

$watch would look for changes to the productcategory.parentID and then, when it changes, you can send a get request to the API to get all the categories where the parentID is the one specified.

On the server side, you will have to create an endpoint that returns all the categories based on the parentID field. In the request handler, you can do this:

Product.find({parentID: req.params.parentID}, function(err, categories){
  if(err) console.log("Error: " + JSON.stringify(err));
  else if(categories) res.json(200, categories);
});

This might not completely give you the solution, but this is an approach that could be followed.

Upvotes: 1

Related Questions