My Main Modal Component is:-
<div class="modal-content">
<div class="modal-header"> </div>
<div class="modal-body">
<display-states-component rolesdata="$ctrl.modaldata.displayStatesRoles"></display-states-component>
</div>
</div>
DisplayStatesComponent is(Angular 1.5 Component architecture)
<form ng-submit="$event.preventDefault()">
<md-autocomplete md-no-cache="true" md-selected-item="$ctrl.displayPerRoles[$index].roleName" md-search-text="$ctrl.searchText" md-items="item in $ctrl.querySearch($ctrl.searchText)" md-item-text="item.value" md-min-length="0" placeholder="Select Role?">
<md-item-template>
<span md-highlight-text="$ctrl.searchText" md-highlight-flags="^i">{{item.value}}</span>{{item.value}}
</md-item-template>
<md-not-found>
No role matching "{{$ctrl.searchText}}" were found.
<a ng-click="$ctrl.manageStates('ADD')">Create a new one!</a>
</md-not-found>
</md-autocomplete>
</form>
And MY Corresponding Display States Controller is:-
$ctrl.states = loadAll();
$ctrl.querySearch = function(query) {
var results = query ? $ctrl.states.filter(createFilterFor(query)) : $ctrl.states,
deferred;
console.log("With my Testings in else--->", results);
return results;
}
/**
* Build `states` list of key/value pairs
*/
function loadAll() {
var allStates = 'Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware,\
Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana,\
Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana,\
Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina,\
North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina,\
South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia,\
Wisconsin, Wyoming';
return allStates.split(/, +/g).map(function(state) {
return {
value: state.toLowerCase(),
display: state
};
});
}
My Filter Functions are
function createFilterFor(query) {
var lowercaseQuery = angular.lowercase(query);
return function filterFn(state) {
return (state.value.indexOf(lowercaseQuery) === 0);
}
};
I am using this code to search for states as the user types state name in autocomplete box. I am getting the results on console but that is not showing item text (I think getting hidden behind bootsrap's modal).
Thanks in advance.
Related
I'm asking if is possible to do something as that in angular
<div ng-app="app">
<div ng-controller="mainController">
<ul ng-repeat="movie in movies |searchFilter:Filter.genre | searchFilter:Filter.name |searchFilter:Filter.pic ">
<li>{{movie.name}}</li>
</ul>
<h2>genre</h2>
<div>
<label>Comedy </label><input type="checkbox" ng-model="Filter.genre.Comedy" ng-true-value="Comedy" data-ng-false-value=''/><br/>
</div>
<h2>PIC</h2>
<label>aa</label><input type="checkbox" ng-model="Filter.pic.aa" ng-true-value="ciao" data-ng-false-value=''/><br/>
<h2>Name</h2>
<label>Shrek</label><input type="checkbox" ng-model="Filter.name.Shrek" ng-true-value="The God" data-ng-false-value=''/><br/>
</div>
</div>
i'm creating a checkbox for filter on different fields (size,name,genre)
ill have a list of avaible sizes,names and genres .
The issue is on ng-model and i tried to write it as "Filter.genre.genre.name" or
"Filter["genre"+genre.name]" and also "Filter.genre[genre.name]" but still not work .
the js.file is
var app =angular.module('app', []);
app.controller('mainController', function($scope) {
$scope.movies = [{name:'Shrek', genre:'Comedy',pic:"cc"},
{name:'Die Hard', genre:'Comedy',pic:"aa"},
{name:'The Godfather', genre:'Drama',pic:"ciao"},
{name:'The Godher', genre:'Comedy',pic:"lel"}];
$scope.genres = [{name:"Comedy"},{name:"Action"},{name:"Drama"}];
});
app.filter('searchFilter',function($filter) {
return function(items,searchfilter) {
var isSearchFilterEmpty = true;
//searchfilter darf nicht leer sein
angular.forEach(searchfilter, function(searchstring) {
if(searchstring !=null && searchstring !=""){
isSearchFilterEmpty= false;
}
});
if(!isSearchFilterEmpty){
var result = [];
angular.forEach(items, function(item) {
var isFound = false;
angular.forEach(item, function(term,key) {
if(term != null && !isFound){
term = term.toLowerCase();
angular.forEach(searchfilter, function(searchstring) {
searchstring = searchstring.toLowerCase();
if(searchstring !="" && term.indexOf(searchstring) !=-1 && !isFound){
result.push(item);
isFound = true;
// console.log(key,term);
}
});
}
});
});
return result;
}else{
return items;
}
}
});
if i make 3 different labels for the field Comedy, Action and Drama with ng-models called as
ng-model="Filter.genre.Comedy" ; ng-model="Filter.genre.Action" and ng-model="Filter.genre.Drama"
it work but it doesnt work if i try to write it into ng-repeat . I hope to have been clearer
In this sample i try to handle your question by change the Model of your page.
we have:
list of movies array => $scope.movies = []
dynamic filters array => $scope.genres = [], $scope.years = [] or more
our target:
Create a dynamic filters to search in movies
what we do
$scope.filterHandler = function (key, value) {}
Run when user start searching on input or select, this function help us to create a filter as object by sending key and value which result is {key:value}
$scope.searchTypeHandler = function (type, dependTo) {}
Run when our filters has some array for example genre has genres as dropdown select, this function help us to return the array which depend to the filter.
var app = angular.module("app", []);
app.controller("ctrl", [
"$scope",
function($scope) {
//your options
$scope.movies = [{
name: 'Shrek',
genre: 'Comedy',
year: 2000
},
{
name: 'Die Hard',
genre: 'Action',
year: 2000
},
{
name: 'The Godfather',
genre: 'Drama',
year: 2015
},
{
name: 'The Godher',
genre: 'Comedy',
year: 2017
}
];
$scope.genres = [{
name: "Comedy"
},
{
name: "Action"
},
{
name: "Drama"
}
];
$scope.years = [{
name: 2000
},
{
name: 2015
},
{
name: 2017
}
];
//
$scope.filter = {}
$scope.filterHandler = function(key, value) {
var object = {};
object[key] = value;
$scope.filter["find"] = object;
};
$scope.searchTypeHandler = function(type, dependTo) {
$scope.filter = {};
$scope.filter.searchType = type;
$scope.filter.options = undefined;
if (dependTo != null) {
$scope.filter.options = $scope[dependTo];
}
};
//default
$scope.searchTypeHandler("name");
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="container" ng-app="app" ng-controller="ctrl">
<div class="page-header">
<div class="row">
<div class="col-lg-12">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
<h4>Movies</h4>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pull-right">
<div class="input-group">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
By {{filter.searchType}}
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-left">
<li><a ng-click="searchTypeHandler('genre', 'genres')">Filter By Genre</a></li>
<li><a ng-click="searchTypeHandler('name', null)">Filter By Name</a></li>
<li><a ng-click="searchTypeHandler('year', 'years')">Filter By Year</a></li>
</ul>
</div>
<input ng-hide="filter.options" type="text" class="form-control" ng-model="filter.query" ng-change="filterHandler(filter.searchType, filter.query)">
<select ng-show="filter.options" class="form-control" ng-model="filter.option" ng-change="filterHandler(filter.searchType, filter.option)" ng-options="option.name as option.name for option in filter.options"></select>
</div>
<!-- /input-group -->
</div>
</div>
</div>
</div>
<ul class="list-group">
<li class="list-group-item" ng-repeat="movie in movies | filter: filter.find">
{{movie.name}} - <label class="label label-info">Ggenre: {{movie.genre}}</label> - <label class="label label-default">Year: {{movie.year}}</label>
</li>
</ul>
</div>
Given a table of JSON data, I need to be able to filter the results based upon the movie genre the user selects from a dropdown menu. I currently have all of the genres from each movie being pulled in, but need to only have each genre available, show up once.
Here's a sample of the JSON:
{
Rank: 1,
Duration: "1 hr. 47 min.",
Description: "The Friedmans are a seemingly typical, upper-middle-class Jewish family whose world is instantly transformed when the father and his youngest son are arrested and charged with shocking and horrible crimes. Caught up in hysteria and with their community in an uproar, the family undergoes a media onslaught. The film inquires not just into the life of a family but into a community, a legal system, and an era.",
Director: "Andrew Jarecki",
Genres: [
"Documentary",
"Special Interest"
],
Actors: [
"Arnold Friedman",
"Elaine Friedman",
"David Friedman",
"Seth Friedman",
"Jesse Friedman",
"Howard Friedman",
"John McDermott",
"Frances Galasso",
"Anthony Sgueglia",
"Detective Frances Ga...",
"Joseph Onorato",
"Judd Maltin",
"Judge Abbey Boklan",
"Ron Georgalis",
"Scott Banks",
"Debbie Nathan",
"Jerry Bernstein",
"Peter Panaro",
"Lloyd Doppman",
"Jack Fallin"
],
Id: 605,
Name: "CAPTURING THE FRIEDMANS (2003)"
}
Here's the markup:
<body ng-app='myApp' ng-controller='MyController'>
<div class="col-lg-12">
<div class="form-inline col-lg-4">
<div class="form-group">
<label for="Name">Search by movie title</label>
<input ng-model="movie" type="text" class="form-control" id="name" placeholder="Title">
</div>
<div class="form-group">
<label for="Actor">Search by actors</label>
<input ng-model="actor" type="text" class="form-control" id="actors" placeholder="Actors">
</div>
</div>
<label for="genres">Search by genre</label>
<select class="form-control" ng-init="cc={Genres: ''}">
<option ng-repeat="movies in results" value="{{movies.Genres}}" ng-model="cc.movies">{{movies.Genres}}</option>
</select>
<table class="table-striped col-lg-8">
<thead>
<td width="15%">Name</td>
<td width="30%">Actors</td>
<td width="10%"></td>
</thead>
<tr ng-repeat="movies in results | filter:Genres">
<td>{{movies.Name}}</td>
<td><li ng-repeat="laptop in movies.Actors | filter:actor" >
<span ng-bind="laptop"></span>
</li></td>
<td>{{movie.Name}} <a class="bookMovie" href="http://www.fandango.com/{{movies.Name}}">Book Now!</a></td>
</tr>
</table>
</div>
and the controller:
app.controller("MyController", ["$scope","$http",
function($scope, $http) {
$http.get('test.json').then(function (response){
console.log(response);
$scope.results = response.data;
});
}]);
Here's the current working plunker with dropdown:
https://plnkr.co/edit/fqJt7pqTc9XKgjgDuGjd?p=preview
You will need to create a list of all unique genres once you get the json data back from your http.get request. I am using Lodash functions to simplify the code
...
$scope.results = response.data
$scope.genres = [];
//Go through every movie
_.forEach($scope.results, function(result) {
//Go through each genre for every movie
_.forEach(result.Genres, function(genre) {
// Add new unique genre to the list
if (!_.includes($scope.genres, genre) {
$scope.genres.push(genre);
}
});
});
Then change your ng-repeat to:
<option ng-repeat="genre in genres" value="{{genre}}">{{genre}}</option>
There a bit of a lot changes in your code for making genre filter to work as expected,so I made a Plunker for that.I've changed the way you written to display genre list from ng-repeat to ng-options.
We can still improve the rest of the filtering processes, but I've answered just to the context.
I have two input options that are linked via ng-model in order to display always the same country in both places.
<select ng-model="selectedCountry" ng-options="country.name for country in countries">
<select ng-model="selectedCountry" ng-options="country.name for country in countries">
Each country has different shipping costs, and once the country is selected you can also choose between different shipping options ("normal" and "express" with a radio button. I want to make it always display the "normal" cost when switching the country, and I made it by adding a ng-click to each of the input options and adding a ng-change in order to listen to the changes:
<select ng-model="selectedCountry" ng-options="country.name for country in countries" ng-change="changeShipping()" ng-click="shippingPrice=selectedCountry.shipping.normal">
The problem is that while the data shown in the view is the correct when changing the country, but the radio button is still set in the wrong option.
This is my controller logic:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.countries = [{
"name": "Freedonia",
"shipping" :
{
"normal":30,
"express":70
}
},{
"name": "Mordor",
"shipping" :
{
"normal":70,
"express":110
}
},{
"name": "Oz",
"shipping" :
{
"normal":140,
"express":180
}
}];
$scope.selectedCountry = $scope.countries[0];
$scope.changeShipping = function() {
return $scope.shippingPrice = $scope.selectedCountry.shipping.normal;
};
});
You can see a working fiddle here Thanks in advance!
Use ng-model with input[radio] and set it's value on select change.
UPDATED JSFIDDLE
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.shippingType = 'normal'; // initial value for input[radio]
$scope.countries = [{
"name": "Freedonia",
"shipping": {
"normal": 30,
"express": 70
}
}, {
"name": "Mordor",
"shipping": {
"normal": 70,
"express": 110
}
}, {
"name": "Oz",
"shipping": {
"normal": 140,
"express": 180
}
}];
$scope.selectedCountry = $scope.countries[0];
$scope.changeShipping = function () {
$scope.shippingType = 'normal'; // set back to normal on country change
return $scope.shippingPrice = $scope.selectedCountry.shipping.normal;
};
});
.summary{
background-color:gray;
border:1px solid black;
font-family:Arial;
margin-bottom:52px;
color:white;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="myCtrl" ng-app="myApp">
<div class="summary">
Summary:<br>
Country:<select ng-model="selectedCountry" ng-options="country.name for country in countries" ng-change="changeShipping()">
</select><br>
Shipping Costs:
{{selectedCountry.shipping[shippingType]}}
</div>
<div>
<select ng-model="selectedCountry" ng-options="country.name for country in countries" ng-change="changeShipping()">
</select>
</div>
Shipping costs:
<form>
<input type="radio" name="shippingOptions" value="normal" ng-model="shippingType">
Normal {{selectedCountry.shipping.normal}}<br>
<input type="radio" name="shippingOptions" value="express" ng-model="shippingType">
Express {{selectedCountry.shipping.express}}<br>
</form>
</div>
I am trying to make a search bar in ionic where if it is similar to this example.
This is the app.html
<ion-content class="has-header" padding="true" ng-controller="appCtrl">
//Search Bar
<label class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<input type="search" placeholder="Search" ng-model="searchText">
</label>
//This is the list that we want to search
<form style="" class="list">
<ion-list>
<ion-item ng-repeat="item in items | filter: userFilter() ">
<div class="row responsive-sm">
<div class="col">
<div>Item Id {{item.id}}</div>
<div>Item detail{{item.detail}}</div>
<div>Date{{item.date}}</div>
</div>
</div>
</ion-item>
</ion-list>
</form>
</ion-content>
This is the controller.js
.controller('appCtrl', function($scope,$state,$location,$ionicModal,$filter) {
$scope.items = [
{
id : 1 ,
detail : "A book about ghost",
date : "20 March 1999"
},
{
id : 2,
detail : "A Book about famous person",
date : "20 March 1999"
},
{
id : 3,
detail : "A Map to a house",
date : "20 March 1999"
},
{
id : 4,
detail : "A famous horror Novel",
date : "20 March 1999"
},
{
id : 5,
detail : "A story about the haunted house",
date : "20 March 1999"
}];
//The filter that is used
$scope.userFilter = function(item) {
// default to no match
var isMatch = false;
if ($scope.searchText) {
// split the input by space
var parts = $scope.searchText.split(' ');
// iterate each of the words that was entered
parts.forEach(function(part) {
// if the word is found in the post, a set the flag to return it.
if (new RegExp('part').test(item)) {
isMatch = true;
}
});
} else {
// if nothing is entered, return all posts
isMatch = true;
}
return isMatch;};})
If the code is working, it supposed to show item no 2 and item no 4 when we write "a famous". Instead, the filter cause all the item in the list disappear. Is there anyway to solve it?
it seems you have call the function in ng-repeat filter | userFilter() without parameter and your have function with item parameter please confirm if this is not problem I can try for your plunkr also your example link is missing.
Good Luck
I had a label containing value user.Rules e.g . London
<label id="ruleId" for="Rules" ng-model="user.Rules" ng-hide="editmode"
style="width:97%; word-wrap:break-word; overflow-wrap:break-word;">{{user.Rules }}
</label>
After click on edit button a drop down list appears containing the list of states e,g Delhi, Pune, London etc.,
<select class="form-control" name="user.Rules" data-ng-model="user.Rules" ng-options="option for option in nestedList" ng-show="editmode" style="width:100%; ">
<option value="" style="margin-left:25px">-Select Rule-</option>
</select>
I need to set the selected value as the label value of drop down list i.e. London
How can I do that ?
Since there is no sample code and sample data for nestedList is not available, so I assume the data as my own and created this sample.
I consider the $scope.RuleId contains the id from the database. I removed the ng-model="user.Rules" from the label and and based on the $scope.RuleId I find its equivalent value.
HTML Code:
<div ng-controller="MyCntrl">
<label id="ruleId" for="Rules" ng-hide="editmode"
style="width:97%; word-wrap:break-word; overflow-wrap:break-word;">{{selectedLabel}}
</label>
<select class="form-control" name="ruleDetails" data-ng-model="RuleId"
ng-options="option.RuleId as option.Rules for option in nestedList"
ng-show="editmode" style="width: 100%;">
<option value="" style="margin-left:25px">-Select Rule-</option>
</select>
<div style="height: 10px"></div>
<div>
<button ng-click="editButton()">Edit</button>
</div>
</div>
Controller Code:
function MyCntrl($scope) {
$scope.editmode = false;
$scope.RuleId = "001";
$scope.nestedList = [{
"Rules": "London",
"RuleId": "001"
}, {
"Rules": "Delhi",
"RuleId": "002"
}, {
"Rules": "Pune",
"RuleId": "003"
}, {
"Rules": "Mumbai",
"RuleId": "004"
}, {
"Rules": "Chennai",
"RuleId": "005"
}];
angular.forEach($scope.nestedList, function(rule) {
if (rule.RuleId === $scope.RuleId) {
$scope.selectedLabel = rule.Rules;
}
});
$scope.editButton = function() {
$scope.editmode = true;
};
}
The same code is added in the Working Sample for your reference.