I have been trying to solve this problem for days.
i'm trying to filter Json data using checkbox , for example when i checkbox more than one id , it shows the rest that id data
enter image description here
if user selects check boxes ( model numbers ),models will be filtered based on those check boxes selection
controller('compareModelsCtrl',['$scope','$http', '$state' ,'$location',
function($scope, $http, $state,$location, filterFilter) {
$http.get('js/data.json').success(function(data){
$scope.models = data;
$scope.whichmodel = $state.params.modelId;
$scope.order = {};
$scope.format=function(){
$scope.modifiedOrder=[];
angular.forEach($scope.order, function(value, key) {
if(value){
$scope.modifiedOrder.push(parseInt(key));
}
});
<ion-checkbox ng-repeat="model in models | filter:query" ng-model="order[model.id]" ng-change="format()">{{model.model_number}}</ion-checkbox>
</ion-item>
current order : {{modifiedOrder}}
DataJson
{
"id":"0",
"model_number":"BT168562.3",
"rotor_drawing_number":4.01591,
"material":"17-4 PH",
"number_of_lobes":"5/6",
"contour_length":51.00,
"overall_length":55.00,
"major_dia_in":1.063,
"minor_dia_in":0.761,
"rotor_head_dia_in":1.000,
"thread_form":"N/A",
"rotor_weight_lbs":11,
"rotor_ecc":0.0755,
"compatibility_weatherford":"",
"compatibility_r_&_m":"TRUE",
"compatibility_dyna_drill":"",
"compatibility_mono_pv":"",
"compatibility_roper":"",
"stator_drawing_number":4.0162,
"stator_length_in":60.00,
"stator_contour_in":52.00,
"cutback_top":4.00,
"cutback_end":4.00,
"tube_od":1.6875,
"tube_id_ends":1.38,
"tube_id_middle":1.38,
"stator_number_of_stages":2.3,
"stator_weight_lbs":14,
"std_vector_nbr_55":0.913,
"std_vector_nbr_hp":0.913,
"fit_5_os_vector_nbr_55":0.000,
"fit_5_os_vector_nbr_hp":0.000,
"fit_1_0_os_vector_nbr_55":0.000,
"fit_1_0_os_vector_nbr_hp":0.000,
"fit_1_5_os_vector_nbr_55":0.000,
"fit_1_5_os_vector_nbr_hp":0.000,
"fit_2_0_os_vector_nbr_55":0,
"fit_2_0_os_vector_nbr_hp":0.000,
"2_us_vector_nbr_55":0,
"2_us_vector_nbr_hp":0,
"fit_1_5_us_vector_nbr_55":0,
"fit_1_5_us_vector_nbr_hp":0,
"fit_1_0_us_vector_nbr_55":0.903,
"fit_1_0_us_vector_nbr_hp":0.903,
"fit_5_us_vector_nbr_55":0,
"fit_5_us_vector_nbr_hp":0,
"fit_2_5_os_vector_nbr_55":0,
"fit_2_5_os_vector_nbr_hp":0,
"fit_3_os_vector_nbr_55":0,
"fit_3_os_vector_nbr_hp":0,
"tolerance":0.010,
"nbr_thermal_expansion_coef":0.000190,
"nbr_hp_thermal_expansion_coef":0.000190,
"number_of_stages":2.3,
"rev_per_gal":9.25,
"torque_slope_ft_lb_psi":0.29,
"pressure_per_stage_nbr_55_psi":150,
"pressure_per_stage_nbr_hp_psi":225,
"pressure_per_stage_nbr_hpx_psi":235,
"plot_gpm_1":20,
"plot_gpm_2":30,
"plot_gpm_3":40,
"slip_at_gpm_2":"15%",
"":0,
"flow_range_low":20,
"flow_range_high":40,
"speed_range_low":185,
"speed_range_high":370,
"off_bottom_pressure":31,
"max_diff_pressure_nbr":345,
"max_diff_pressure_nbr_hpx":541,
"stall_diff_pressure_nbr":518,
"stall_diff_pressure_nbr_hpx":851,
"max_torque_nbr":100,
"max_torque_nbr_hpx":157,
"stall_torque_nbr":185,
"},
"id":"1",
"model_number":"BT168565.0",
"rotor_drawing_number":4.01530,
"material":"17-4 PH",
"number_of_lobes":"5/6",
"contour_length":87.00,
"overall_length":93.00,
"major_dia_in":0.990,
"minor_dia_in":0.714,
"rotor_head_dia_in":1.100,
"thread_form":"N/A",
"rotor_weight_lbs":16,
"rotor_ecc":0.0690,
"compatibility_weatherford":"",
"compatibility_r_&_m":"TRUE",
"compatibility_dyna_drill":"",
"compatibility_mono_pv":"",
"compatibility_roper":"TRUE",
"stator_drawing_number":4.01637,
"stator_length_in":99.00,
"stator_contour_in":91.00,
"cutback_top":4.00,
"cutback_end":4.00,
"tube_od":1.6875,
"tube_id_ends":1.38,
"tube_id_middle":1.38,
"stator_number_of_stages":5.0,
"stator_weight_lbs":27,
"std_vector_nbr_55":0.853,
"std_vector_nbr_hp":0.853,
"fit_5_os_vector_nbr_55":0.000,
"fit_5_os_vector_nbr_hp":0.000,
"fit_1_0_os_vector_nbr_55":0.863,
"fit_1_0_os_vector_nbr_hp":0.863,
"fit_1_5_os_vector_nbr_55":0.000,
"fit_1_5_os_vector_nbr_hp":0.000,
"fit_2_0_os_vector_nbr_55":0,
"fit_2_0_os_vector_nbr_hp":0,
"2_us_vector_nbr_55":0,
"2_us_vector_nbr_hp":0,
"fit_1_5_us_vector_nbr_55":0,
"fit_1_5_us_vector_nbr_hp":0,
"fit_1_0_us_vector_nbr_55":0,
"fit_1_0_us_vector_nbr_hp":0,
"fit_5_us_vector_nbr_55":0,
"fit_5_us_vector_nbr_hp":0,
"fit_2_5_os_vector_nbr_55":0,
"fit_2_5_os_vector_nbr_hp":0,
"fit_3_os_vector_nbr_55":0,
"fit_3_os_vector_nbr_hp":0,
"tolerance":0.010,
"nbr_thermal_expansion_coef":0.000190,
"nbr_hp_thermal_expansion_coef":0.000190,
"number_of_stages":5.0,
"rev_per_gal":15.72,
"torque_slope_ft_lb_psi":0.19,
"pressure_per_stage_nbr_55_psi":150,
"pressure_per_stage_nbr_hp_psi":225,
"pressure_per_stage_nbr_hpx_psi":235,
"plot_gpm_1":25,
"plot_gpm_2":40,
"plot_gpm_3":55,
"slip_at_gpm_2":"15%",
"":0,
"flow_range_low":25,
"flow_range_high":55,
"speed_range_low":393,
"speed_range_high":865,
"off_bottom_pressure":67,
"max_diff_pressure_nbr":750,
"max_diff_pressure_nbr_hpx":1175,
"stall_diff_pressure_nbr":1125,
"stall_diff_pressure_nbr_hpx":1851,
"max_torque_nbr":143,
"max_torque_nbr_hpx":223,
"stall_torque_nbr":393,
"stall_torque_nbr_hpx":865
}
Any help will be appreciated , Thanks
First off make sure the JSON data or Models is as
[{
"id":"0",
"model_number":"BT168562.3",
"rotor_drawing_number":4.01591,
"material":"17-4 PH",
"number_of_lobes":"5/6",
"contour_length":51.00,
"overall_length":55.00,
"major_dia_in":1.063,
"minor_dia_in":0.761,
"rotor_head_dia_in":1.000,
"thread_form":"N/A",
"rotor_weight_lbs":11,
"rotor_ecc":0.0755,
"compatibility_weatherford":"",
"compatibility_r_&_m":"TRUE",
"compatibility_dyna_drill":"",
"compatibility_mono_pv":"",
"compatibility_roper":"",
"stator_drawing_number":4.0162,
"stator_length_in":60.00,
"stator_contour_in":52.00,
"cutback_top":4.00,
"cutback_end":4.00,
"tube_od":1.6875,
"tube_id_ends":1.38,
"tube_id_middle":1.38,
"stator_number_of_stages":2.3,
"stator_weight_lbs":14,
"std_vector_nbr_55":0.913,
"std_vector_nbr_hp":0.913,
"fit_5_os_vector_nbr_55":0.000,
"fit_5_os_vector_nbr_hp":0.000,
"fit_1_0_os_vector_nbr_55":0.000,
"fit_1_0_os_vector_nbr_hp":0.000,
"fit_1_5_os_vector_nbr_55":0.000,
"fit_1_5_os_vector_nbr_hp":0.000,
"fit_2_0_os_vector_nbr_55":0,
"fit_2_0_os_vector_nbr_hp":0.000,
"2_us_vector_nbr_55":0,
"2_us_vector_nbr_hp":0,
"fit_1_5_us_vector_nbr_55":0,
"fit_1_5_us_vector_nbr_hp":0,
"fit_1_0_us_vector_nbr_55":0.903,
"fit_1_0_us_vector_nbr_hp":0.903,
"fit_5_us_vector_nbr_55":0,
"fit_5_us_vector_nbr_hp":0,
"fit_2_5_os_vector_nbr_55":0,
"fit_2_5_os_vector_nbr_hp":0,
"fit_3_os_vector_nbr_55":0,
"fit_3_os_vector_nbr_hp":0,
"tolerance":0.010,
"nbr_thermal_expansion_coef":0.000190,
"nbr_hp_thermal_expansion_coef":0.000190,
"number_of_stages":2.3,
"rev_per_gal":9.25,
"torque_slope_ft_lb_psi":0.29,
"pressure_per_stage_nbr_55_psi":150,
"pressure_per_stage_nbr_hp_psi":225,
"pressure_per_stage_nbr_hpx_psi":235,
"plot_gpm_1":20,
"plot_gpm_2":30,
"plot_gpm_3":40,
"slip_at_gpm_2":"15%",
"":0,
"flow_range_low":20,
"flow_range_high":40,
"speed_range_low":185,
"speed_range_high":370,
"off_bottom_pressure":31,
"max_diff_pressure_nbr":345,
"max_diff_pressure_nbr_hpx":541,
"stall_diff_pressure_nbr":518,
"stall_diff_pressure_nbr_hpx":851,
"max_torque_nbr":100,
"max_torque_nbr_hpx":157,
"stall_torque_nbr":185,
},
{"id":"1",
"model_number":"BT168565.0",
"rotor_drawing_number":4.01530,
"material":"17-4 PH",
"number_of_lobes":"5/6",
"contour_length":87.00,
"overall_length":93.00,
"major_dia_in":0.990,
"minor_dia_in":0.714,
"rotor_head_dia_in":1.100,
"thread_form":"N/A",
"rotor_weight_lbs":16,
"rotor_ecc":0.0690,
"compatibility_weatherford":"",
"compatibility_r_&_m":"TRUE",
"compatibility_dyna_drill":"",
"compatibility_mono_pv":"",
"compatibility_roper":"TRUE",
"stator_drawing_number":4.01637,
"stator_length_in":99.00,
"stator_contour_in":91.00,
"cutback_top":4.00,
"cutback_end":4.00,
"tube_od":1.6875,
"tube_id_ends":1.38,
"tube_id_middle":1.38,
"stator_number_of_stages":5.0,
"stator_weight_lbs":27,
"std_vector_nbr_55":0.853,
"std_vector_nbr_hp":0.853,
"fit_5_os_vector_nbr_55":0.000,
"fit_5_os_vector_nbr_hp":0.000,
"fit_1_0_os_vector_nbr_55":0.863,
"fit_1_0_os_vector_nbr_hp":0.863,
"fit_1_5_os_vector_nbr_55":0.000,
"fit_1_5_os_vector_nbr_hp":0.000,
"fit_2_0_os_vector_nbr_55":0,
"fit_2_0_os_vector_nbr_hp":0,
"2_us_vector_nbr_55":0,
"2_us_vector_nbr_hp":0,
"fit_1_5_us_vector_nbr_55":0,
"fit_1_5_us_vector_nbr_hp":0,
"fit_1_0_us_vector_nbr_55":0,
"fit_1_0_us_vector_nbr_hp":0,
"fit_5_us_vector_nbr_55":0,
"fit_5_us_vector_nbr_hp":0,
"fit_2_5_os_vector_nbr_55":0,
"fit_2_5_os_vector_nbr_hp":0,
"fit_3_os_vector_nbr_55":0,
"fit_3_os_vector_nbr_hp":0,
"tolerance":0.010,
"nbr_thermal_expansion_coef":0.000190,
"nbr_hp_thermal_expansion_coef":0.000190,
"number_of_stages":5.0,
"rev_per_gal":15.72,
"torque_slope_ft_lb_psi":0.19,
"pressure_per_stage_nbr_55_psi":150,
"pressure_per_stage_nbr_hp_psi":225,
"pressure_per_stage_nbr_hpx_psi":235,
"plot_gpm_1":25,
"plot_gpm_2":40,
"plot_gpm_3":55,
"slip_at_gpm_2":"15%",
"":0,
"flow_range_low":25,
"flow_range_high":55,
"speed_range_low":393,
"speed_range_high":865,
"off_bottom_pressure":67,
"max_diff_pressure_nbr":750,
"max_diff_pressure_nbr_hpx":1175,
"stall_diff_pressure_nbr":1125,
"stall_diff_pressure_nbr_hpx":1851,
"max_torque_nbr":143,
"max_torque_nbr_hpx":223,
"stall_torque_nbr":393,
"stall_torque_nbr_hpx":865
}]
There was formating issue in your question.
Second, you need to use filter function. I have created a filtered object named as selectedObjects and modified the value in the format function and displayed in HTML as follows.
<ion-checkbox ng-repeat="model in models | filter:query" ng-model="order[model.id]" ng-change="format()">{{model.model_number}}</ion-checkbox>
<pre>
current order : {{modifiedOrder}} <br>
selected objects:{{selectedObjects}} </pre>
And JS
This implementation assumes that id is unique in JSON.
$scope.order = {};
$scope.format = function() {
$scope.modifiedOrder = [];
$scope.selectedObjects = [];
angular.forEach($scope.order, function(value, key) {
if (value) {
var filtered = $scope.models.filter(function(unit) {
return unit.id === key
});
$scope.selectedObjects.push({
"overall_length":filtered[0].overall_length,
"contour_length": filtered[0].contour_length
});
$scope.modifiedOrder.push(parseInt(key));
}
});
}
Find sample implementation here.
https://codepen.io/anon/pen/BLNJzB?editors=1010
Good luck.
Related
I'm using the azure search api trying to filter by a certain field value: businesstype = store. It always returns 3 stores, even though I should have thousands. I can't tell for sure what's inside the index. In the Azure search web portal I type businessType eq 'store' and it gives me two stores, then starts returning businesstype = restaurant. Not sure what is going on. We have other implementations in other projects where filters are working. Here's code that I'm executing as it is invoked by using ASP.NET Web API
var indexClient = new SearchIndexClient(GlobalSettings.SearchServiceName, $"businesses{GlobalSettings.Environment}", new SearchCredentials(GlobalSettings.SearchServiceAdminApiKey));
if (latitude == null && longitude == null)
{
//chicago
latitude = 41.8333925;
longitude = -88.0121478;
}
// get all attributes and camel case them
var attributes = typeof(BusinessSearchItem).GetProperties().Select(x => char.ToLowerInvariant(x.Name[0]) + x.Name.Substring(1)).ToList();
var parameters = new SearchParameters
{
Select = attributes,
QueryType = QueryType.Full,
Top = take,
Skip = skip,
IncludeTotalResultCount = true,
OrderBy = new List<string>() { $"geo.distance(location, geography'POINT({longitude} {latitude})')" }
};
// filters
string filter = "";
if (!string.IsNullOrEmpty(businessType))
{
switch (businessType.ToLower())
{
case "restaurant":
filter += "businessType eq 'Restaurant'";
break;
case "store":
filter += "businessType eq 'Store'";
break;
}// end switch on business type
}
parameters.Filter = filter;
try
{
// run the search
var results = indexClient.Documents.Search<BusinessSearchItem>(q, parameters);
Logger.Log.Info($"Search conducted. Query: {q} Business Type: {businessType} Lat: {latitude} Long: {longitude} User: {username}");
var businessDTOs = results.Results.Select(x => new BusinessDTO
{
.........
).ToList()
}).ToList();
the model BusinessSearchItem has a field BusinessType of string that has the attribute searchable. The skip is 0 and take 40.
The problem wasn't the search at all, it was that the data was not in the index
How can I use find select distinct in angularJS then counting it, I'm new to mean-stack and I don't know how to use distinct method.
Here is my table structure with sample data:
donation_no:"3124"
donors_name:"Jeremy Adrian De Vera"
date:"12/26/2018"
time:"8:30"
blood_group:"B"
volume:"2"
branch:"Pasig"
I already tried it using php like this but how can I do this in angular:
$query = "SELECT DISTINCT fruits, COUNT(*) as counter FROM my_table WHERE date_tested BETWEEN '$date_from' AND '$date_to' GROUP BY fruits";
Here is my code for finding the values from my db
api.js
router.get('/blooddonationmanagement', function(req, res) {
Blooddonation.find({},function(err, blooddonations) {
res.json({ success: true, blooddonations: blooddonations });
});
});
blooddonationCtrl.js
angular.module('blooddonationControllers', [])
.controller('blooddonationCtrl', function(Blooddonation,$scope) {
var app = this;
function getBlooddonations() {
Blooddonation.getUsers().then(function(data) {
app.blooddonations = data.data.blooddonations;
});
}
chapters.html
<tr ng-repeat="person in blooddonationmanagement.blooddonations">
<td>{{ person.branch}}</td>
</tr>
How can I count the distinct values then counting it from my db, Im expecting like this:
branch count
Pasig 29
Manila 16
Pasay 19
You should try distinct()
Blooddonation.distinct('branch',... );
Just read the docs here: https://docs.mongodb.com/manual/reference/method/db.collection.distinct/
You can use:
db.collection.distinct()
I GET NullPointerException with status code : 400
I am pushing data into a JsonObject and then post it to my back end (Spring ) and get the field using Json mapper (data.get("Field name").asText;.
I also tried get(index) and data.path but this return null, where as in the debug console It indicates that Json objects were received, what is it that makes these fields to be null?
Is it because this data is not directly from ng-model, or is it because I have pushed it into json object using Angular, because I am getting 400 erro code.
MY ANGULAR JS :
app.controller("OrderOperationsCtrl", function($scope, $http,$window) {
$http.get(productURL).then(function(response) {
$scope.productData = response.data;
$scope.newData = [];
$scope.total = 0.0;
$scope.sync = function(bool,productData,submit){
if(bool === true){
var numberOrdered = 0;
numberOrdered++;
$scope.total = $scope.total + productData.price;
var newD = $scope.newData.push({
"numberOrdered":numberOrdered,
"customerId":localStorage.getItem('takealot_customer'),
"id":productData.id,
"total":$scope.total
});
$scope.sender = $scope.newData;
console.log(" NewD " + $scope.newData);
}
if(submit === true){
$http.put(orderURL,JSON.stringify($scope.sender)).then(function(response){
if(response.data)
console.log("Order created !");
},function(response){
console.log("Error creating Order !");
console.log(response.status);
});
MY SPRING CONTROLLER UPDATE
public ResponseEntity createOrder(#RequestBody OrderOperations[] operation) throws OrderOperationsNotFoundException, IOException{
logger.info("recieved json String " + operation);
services.createOrder(operation);
return new ResponseEntity(HttpStatus.OK);
}
CONSOLE OUTPUT :
Hibernate: select products0_.id as id1_3_, products0_.category as category2_3_, products0_.description as descript3_3_, products0_.name as name4_3_, products0_.price as price5_3_, products0_.quantity as quantity6_3_, products0_.supplier as supplier7_3_ from products products0_
2017-11-03 11:34:41.689 INFO 10504 --- [nio-8080-exec-9]
c.p.C.OrderOperationsController : recieved json String
[{"numberOrdered":1,"customerId":null,"id":1,"total":78.32,"$$hashKey":"object:17"},
{"numberOrdered":1,"customerId":null,"id":2,"total":156.70999999999998,"$$hashKey":"object:19"},
{"numberOrdered":1,"customerId":null,"id":1,"total":235.02999999999997,"$$hashKey":"object:21"}]
THanks in advance
First of all you dont need to map the json into the OrderOperation Object.
Spring will automatically convert the JSON Object into OrderOperations by passing as Parameter
public ResponseEntity createOrder(#RequestBody OrderOperations operation)
And i think you get the Nullpointer while you parsing the Data
Finally I have got a solution. some weeks ago.
- For those who are still struggling to save multiple objects from JSON array
see below ... I hope this helps someone else.
public List<String> createOrder(#RequestBody OrderOperations[] json) throws OrderOperationsNotFoundException, IOException{
logger.info("recieved json String " + json);
List<String> response = new ArrayList<String>();
for(OrderOperations op : json){
services.createOrder(op);
response.add("saved order : " + op.toString());
}
return response;
}
I am using mongodb and node.js. In my database i have 6 fields that is
bid
color (Array)
size (Array)
cat_id
sub_cat_id
All is working fine. Now i want to add filter in my code. In filter area i have add this all fields. user select multiple colors and sizes so it will come in Array format but most of the time user will not select color option or size option at that time field values comes blank so my filter will not take any result from database. so i want to remove color or size field if value is empty during search. I have tried below code but its not working.how i do this.
var catId = new Array();
var sort = saveFilterSort.sort;
var filter = req.body;
if(req.body.catId){
catId.push("category_id:"+req.body.catId);
}
if(req.body.subcatid){
catId.push("sub_category_id:"+req.body.subcatid);
}
if(req.body.minprice){
catId.push("price:{$gt:"+req.body.minprice+"}");
}
if(req.body.maxprice){
catId.push("price:{$lt:"+req.body.maxprice+"}");
}
if(req.body.color){
catId.push("color:{$in:"+req.body.color+"}");
}
if(req.body.size){
catId.push("attribute:{$in:"+req.body.size+"}");
}
var finalCat = catId.join(',');
console.log(finalCat);
console.log(catId);
if((filter) && (sort)){
Product.find(
{
brand_id:bid, finalCat
},
function(error,fetchallFeatProds)
{
console.log('#######################');
console.log(fetchallFeatProds);
console.log('#######################');
callback(error,fetchallFeatProds);
}).sort( {_id:-1,price:-1} );
This code is not working. Please help me.
Mongoose find prototype handle json and not string
var query = {brand_id:bid};
var sort = saveFilterSort.sort;
var filter = req.body;
if(req.body.catId){
query.category_id = req.body.catId;
}
if(req.body.subcatid){
query.sub_category_id = req.body.subcatid;
}
if(req.body.minprice){
query.price = {$gt:req.body.minprice};
}
if(req.body.maxprice){
query.price = {$lt:req.body.maxprice};
}
if(req.body.color){
query.color = {$in:req.body.color};
}
if(req.body.size){
query.attribute = {$in:req.body.size};
}
if((filter) && (sort)){
Product.find(query, ...
I a user list that pops up when a user types # into a text field. When this happens I want the user list to be filtered for every letter typed after the # symbol. So if they typed "#s" all usernames starting with an "s" would show. I'm recording the text typed after the # and assigning it as the text to filter by, but the filter won't act until two characters have been typed. Can someone tell me why this would be happening? Thank you for your help.
HTML/SLIM:
/ USER LIST
.user-list.list-group [ng-show="usersShow"]
a.user.list-group-item [ng-repeat="user in users | filter:formattedUser"]
strong
| {{user.username}}
| {{' : ' + (user | fullName)}}
/ COMMENT FORM
= form_for Comment.new, html: {action: nil, name: 'newCommentForm', 'ng-submit' => 'createComment($event, comment)'} do |f|
= f.text_field :text, placeholder: 'Add comment...',
'ng-model' => 'comment.text', 'ng-change' => 'showUserList(comment.text)'
span.input-group-btn
button.btn type="submit" Add Comment
JS:
$scope.showUserList = function(comment) {
var USERNAME_PATTERN = /(?:^|\s)#\S*$/;
var comment = comment || '';
var userMatch = comment.match(USERNAME_PATTERN) || '';
if(userMatch) {
$scope.userFormatted = userMatch[0].replace('#', '').trim();
}
$scope.usersShow = USERNAME_PATTERN.test(comment);
};