Clear button in AutoComplete of Angular Material blocks all the DOM elements - angularjs

Yes, this is very rare situation but somehow, if i use the autocomplete as follows, i get all the dom elements blocked and i cant interact anymore with an element from my page.
This is the html-part:
<md-autocomplete style="background-color:white; height:10px;"
md-selected-item="selectedItem"
md-search-text-change="searchTextChange(searchText)"
md-search-text="searchText"
md-selected-item-change="selectedItemChange(item)"
md-items="item in querySearch(searchText) | orderBy:'text'"
md-item-text="item.text"
md-min-length="0"
placeholder="Filteren op tag"
md-menu-class="autocomplete-custom-template">
<md-item-template style="background-color:white;">
<span class="select-title">
<!--<md-icon md-svg-icon="selectboxIcon.svg"></md-icon>-->
<span class="item-tags"> {{item.text}} </span>
</span>
</md-item-template>
</md-autocomplete>
and this is the corresponding parts from my controller:
$scope.querySearch = function (query) {
var results = query ? $scope.allTags.filter($scope.createFilterFor(query)) : $scope.allTags;
return results;
}
$scope.createFilterFor = function (query) {
var lowercaseQuery = angular.lowercase(query);
return function filterFn(item) {
console.log(item);
var itemName = angular.lowercase(angular.lowercase(item.text));
return (itemName.indexOf(lowercaseQuery) === 0);
};
}
$scope.searchTextChange = function searchTextChange(text) {
$log.info('Text changed to ' + text);
}
$scope.selectedItemChange = function selectedItemChange(item) {
console.log("selected");
console.log(item);
}
ps: every functionality works fine and without error. Just clicking the clear button - as shown in the following image- causes this problem -tested in last versions of chrome and mozilla-.

There is an issue posted on github regarding this.
You can check it out here.
It is resolved in the update 0.10.1-rc4.
Update your angular-material to master.
Temporary Workaround:
CSS:
.md-scroll-mask{
position: initial
}

Related

Show autocomplete dropdown at first click in AngularJS Material

I have an md-autocomplete component from AngularJS Material and it does not show the dropdown with the options after first click in the input, as it is supposed to shown. It shows the full list of options in the dropdown when I click on input after I've done a search and then delete the entered input. Can anyone tell what is wrong in my code?
Here is my code:
I tried the md-min-legth attribute but it does not work.
<md-autocomplete ng-disabled="false"
md-no-cache="true"
name="projectAutocompleteFormRecord"
md-selected-item="model.TimeRecord.Project"
md-items="project in querySearchForProjectRecord(searchProjectTextRecord)"
md-item-text="project.Code + (project.DerivedCode ? project.DerivedCode : '') + '-' + project.Title"
md-search-text="searchProjectTextRecord"
md-min-length="0"
placeholder="Project">
<md-item-template>
<div class="item-title">
<span md-highlight-text="searchProjectTextRecord"
md-highlight-flags="ig">
{{project.Code + (project.DerivedCode ? project.DerivedCode : "") + "-" + project.Title}}
</span>
</div>
</md-item-template>
</md-autocomplete>
<script>
// initialize of $scope.projs;
$scope.searchProjectTextRecord = "";
$scope.querySearchForProjectRecord = function (query) {
var results = [];
var projects = $scope.projs;
if (projects) {
results = query ? projects.filter(item => item.Code.toLowerCase().includes(query.toLowerCase()) ||item.Title.toLowerCase().includes(query.toLowerCase())) : projects;
}
return results;
}
</script>
I managed to fix it by adding the <md-not-found> element.

Show and Hide a <div> inside an ng-repeat with 'dirPagination' (AngularJS)

In this very site I've seen this question many times, but none of them works for me because of two things: I need the div to toggle when clicking the "options" button, but also to hide when clicking outside of the div; and I need it to work with dirPagination.
I saw this answer, it works fine but with one problem I just can't solve: it shows ALL the hidden divs at the same time, instead of only showing the one I clicked.
Here's my code:
<body ng-controller="MainCtrl">
<!-- pagination -->
<dir-pagination-controls
max-size="7"
direction-links="true"
boundary-links="true">
</dir-pagination-controls>
<ul>
<li dir-paginate="report in reports | itemsPerPage:4">
Options
<h3>{{report.Title}}</h3>
<div class="options" ng-show="dp">
<p>These are some options</p>
</div>
</li>
</ul>
</body>
And the JS to show the options:
//options div
$scope.showOptions = function(event){
if($scope.dp === false || $scope.dp === undefined) {
$scope.dp = true;
event.stopPropagation();
} else {
$scope.dp = false;
}
};
window.onclick = function(){
if($scope.dp){
$scope.dp = false;
$scope.$apply();
}
};
I've made a Plunker in case you wanna se it in action: my Plunker link
Can somebody help me with this issue? :(
Add a new boolean property on your reports array , for example show
var reports = [
{
"ID":1,
"Title":"Report 1",
"Show":false
},
{
"ID":2,
"Title":"Report 2",
"Show":false
}
]
Apply the property in to ng-show and also pass the current report scope object in to showOptions method to write the logic for hide and show.
<li dir-paginate="report in reports | itemsPerPage:4">
Options
<h3>{{report.Title}}</h3>
<div class="options" ng-show="report.Show">
<p>These are some options</p>
</div>
</li>
$scope.showOptions = function(event,report){
report.Show=!report.Show;
/*you can iterate through each reports and change show to false if the clicked report id not equal to report id , Angular JS will automatically update the scope in to the view*/
reports.forEach(function(item, index) {
if (item.ID !== report.ID) {
item.Show = false;
}
});
if($scope.dp === false || $scope.dp === undefined) {
$scope.dp = true;
event.stopPropagation();
} else {
$scope.dp = false;
}
};
https://next.plnkr.co/edit/hnhWMrgR3GMtVcES
You need to use a separate variable for each div you want to show. You could add the dp attribute to the report. There is no need to loop over the reports to hide them. You can just keep track of the currently visible report and hide it when another one is toggled.
Here is the relevant HTML:
<li dir-paginate="report in reports | itemsPerPage:4">
Options
<h3>{{report.Title}}</h3>
<div class="options" ng-show="report.dp">
<p>These are some options</p>
</div>
</li>
and JavaScript
var visibleReport;
$scope.showOptions = function(event, report){
if (report == visibleReport) {
report.dp = !report.dp;
}
else {
if (visibleReport) visibleReport.dp = false;
report.dp = true;
}
visibleReport = report
event.stopPropagation();
};
window.onclick = function(){
if (visibleReport) visibleReport.dp = false;
visibleReport = null;
$scope.$apply();
};
Here is a working plunker https://next.plnkr.co/edit/sWLxBGlF8D22nvYp?preview

md-autocomplete doesn't show li items text

I have a md-autocomplete field:
<md-autocomplete md-selected-item="videoInfo.lineUp[1]" md-items="item in searchQuery(searchText)" md-search-text="searchText" md-item-text="item.display"></md-autocomplete>
I populate md-items with
$scope.searchQuery = function (searchText) {
var users = [];
angular.forEach($scope.users,
function (value, key) {
// value = user object
// key = userId
var dN = value["display_name"];
if (dN) {
var obj = {};
obj[key] = value;
obj["display"] = dN;
if (dN.toLowerCase().indexOf(searchText.toLowerCase()) !== -1) {
users.push(obj);
}
}
});
return users;
}
It working in the sense that I can type in the input field and suggestions are loaded, however the dropdown is empty, i.e. it doesn't show display_name as I would have expected. When I select one of the options, the display_name of the selected item does show up in the input field. Any ideas what I could be doing wrong?
You need to define how you display information in the dropdown within the <md-autocomplete> tag. Here's an example - CodePen
Markup
<div ng-controller="AppCtrl as vm" ng-cloak="" ng-app="MyApp">
<md-autocomplete flex
md-selected-item="text"
md-no-cache="true"
md-items="item in vm.items()"
md-min-length="0">
<span id="autocompleteText" md-highlight-text="searchText">{{item}}</span>
</md-autocomplete>
</div>
https://material.angularjs.org/latest/demo/autocomplete

ng-tags-input not working as desired

I am using ng-Input-tags in my angular project.But when I am searching the keyword my autocomplete is not getting updated with the searched results. I checked my method it is returning correct results but they are not appearing in dropdown of autocomplete.
This is my html code:
<tags-input ng-model="selectedDrivers" display-property="name" replace-spaces-with-dashes="false" add-from-autocomplete-only="true" placeholder="Search by driver name or driver id.." on-tag-added="emptyScope()">
<auto-complete source="searchDriver($query)" debounce-delay="500"></auto-complete>
</tags-input>
And this is my js code:
$scope.searchDriver = function(query) {
$scope.searchedResults = [];
for(var key in $scope.driversInfo){
if($scope.driversInfo[key].name.toLowerCase().indexOf(query.toLowerCase()) >= 0 || $scope.driversInfo[key].id.toString().indexOf(query.toString()) >= 0)
$scope.searchedResults.push($scope.driversInfo[key]);
}
return $scope.searchedResults;
};
Somehow the suggestionList is not getting updated.
The source attirbute of the autocomplete is waiting for a promise so you have to use $q.
let result: Array<any> = list.filter(
(elt: any) => { return elt[property].toLowerCase().indexOf(query.toLowerCase()) !== -1; });
deferred.resolve(result);
return deferred.promise;

Can I filter promise when using Angular Material Autocomplete (md-autocomplete)

I have the following in my autocomplete
md-autocomplete(flex-gt-sm="50"
placeholder="Select label or enter new label",
md-selected-item="ctrl.node.label",
md-items="item in ctrl.getLabels() | filter:ctrl.labelSearchText",
md-item-text="item",
md-search-text="ctrl.labelSearchText",
md-floating-label="Label")
md-item-template
span {{item}}
With the following as getLabels
this.getLabels = function() {
return Restangular.all('label').getList();
};
When I run this code it does not filter the results instead I get the entire list. Is there a way to filter these results?
I am not sure if you can filter with angular filters in the tag itself. But you can do this with lodash.filter or angular filters in the javascript function.
md-autocomplete(flex-gt-sm="50"
placeholder="Select label or enter new label",
md-selected-item="ctrl.node.label",
md-items="item in ctrl.getLabels(ctrl.labelSearchText)",
md-item-text="item",
md-search-text="ctrl.labelSearchText",
md-floating-label="Label")
md-item-template
span {{item}}
And the getLabels function.
this.getLabels = function(searchText) {
return this.$q(function (resolve) {
Restangular.all('label').getList().then(function (result) {
resolve(_.filter(result.data, function(label) {
// filter here, simple case string equality
return label === searchText;
}));
});
});
};

Resources