How to get row data through angular material menu inside ag-grid - javascript

I've a requirement where in ag-grid, I need to open a menu to add/edit/delete that row data. I'm using angular material menu component as cell template url. but when click on that menu item, it does not trigger onCellClicked event in ag-grid to get reference to clicked row data however If I click pixels away from menu item then event is fired.
Need help to figure out, how can I get row data on menu click.
my ag-grid code goes here:
var columnDefs = [
{headerName: "", field: "icon", width:65},
{headerName: "Categories",field:"category_name", width:1025, cellRenderer:function(params){
// my cell renderer code goes here
},
onCellClicked:function(params){
console.log("Cell is still getting click "+params.data);
//This click does not work
},
{headerName: "", field: "options", width:87, suppressMenu: true, templateUrl:"partials/options.html"
}
];
options.html
<md-menu>
<!-- <md-button aria-label="Open phone interactions menu" class="md-icon-button" ng-click="openMenu($mdOpenMenu, $event)">
<md-icon md-svg-src="images/icons/options.svg" aria-label="android "></md-icon>
</md-button> -->
options
<md-menu-content width="4">
<md-menu-item>
<md-button ng-click="modifyOptions($event)">
<md-icon md-svg-src="images/icons/add.svg" aria-label="android "></md-icon>
Add
</md-button>
</md-menu-item>
<md-menu-item>
<md-button ng-click="toggleNotifications()">
<md-icon md-svg-src="images/icons/edit.svg" aria-label="android "></md-icon>
Edit
</md-button>
</md-menu-item>
<md-menu-divider></md-menu-divider>
<md-menu-item>
<md-button disabled="disabled" ng-click="checkVoicemail()">
<md-icon md-svg-src="images/icons/delete.svg" aria-label="android "></md-icon>
Delete
</md-button>
</md-menu-item>
</md-menu-content>

I found the answer for my question which was not pretty hard though: Cell template has reference to data object for each row, so property can be accessed by calling data.your_property. Here I didn't need onCellClicked event at all. Below is working code:
var columnDefs = [
{headerName: "", field: "icon", width:65},
{headerName: "Categories",field:"category_name", width:1025, cellRenderer:function(params){
// Cell Renderer goes here
},
{headerName: "", field: "options", width:87, suppressMenu: true, templateUrl:"partials/options.html"
}
];
option.html
<md-menu>
options
<md-menu-content width="4">
<md-menu-item>
<md-button ng-click="modifyOptions($event, data)">
<md-icon md-svg-src="images/icons/add.svg" aria-label="android "></md-icon>
Add
</md-button>
</md-menu-item>
<md-menu-item>
<md-button ng-click="modifyCategory(data)">
<md-icon md-svg-src="images/icons/edit.svg" aria-label="android "></md-icon>
Edit
</md-button>
</md-menu-item>
<md-menu-divider></md-menu-divider>
<md-menu-item>
<md-button disabled="disabled" ng-click="deleteCategory(data)">
<md-icon md-svg-src="images/icons/delete.svg" aria-label="android "></md-icon>
Delete
</md-button>
</md-menu-item>

Related

AngularJS UI Router: ui-sref not updating URL in address bar?

I'm building an AngularJS app and I'm using UI-Router in order to navigate through the app.
The flow of the web app consist in first registering or login, then it takes you to a main grid that has a lot of elements that are being displayed through ng-repeat and when you click in one of them it takes you to a dashboard that displays information of the element that has been clicked and it also successfully changes the path to index.html#!/dashboard/(here goes the name of the element)
This is the html 5 code of that main grid:
<div class="container">
<div class="row" ng-controller="ribtStateServicesController" >
<div class="col-md-3 ribt_HomeGrid" ng-repeat="estado in estados" >
<div class="img-ribt_overlay" >
<a ng-click="doWork(estado.idState)" ui-sref="dashboard({ribtNameState: estado.idState})">
<img src="{{estado.image}}" class="img-thumbnail">
<div class="ribt_overlay" ></div>
</a>
</div>
</div>
</div>
</div>
However, in that same dashboard I have a side menu, that has the same elements that were used in the main grid before. So I figured that it would take the same logic to be able to use the params that the UI-Router offer in order to change the URL and the content displayed. But it does nothing.
This is the code in the HTML of the dashboard's side menu:
<div class="col-md-6 nopadding" ng-repeat="estado in estados | filter:busqueda">
<md-card md-ink-ripple="#ffffff">
<img ng-src="{{estado.nolabel}}" class="md-card-image ribt_estado_side_img"
ui-sref="dashboard({ribtNameState: estado.idState})"
alt="Estado" ng-click="doWork(estado.idState); toggleLeft()">
<md-card-title class="nopadding">
<md-card-title-text>
<p class="ribt_card_estado_nombre">{{estado.nombre}}</p>
</md-card-title-text>
<md-menu>
<md-button class="md-icon-button float-right" aria-label="Share" ng-click="$mdMenu.open()">
<i class="material-icons">more_vert</i>
</md-button>
<md-menu-content width="4" ng-mouseleave="$mdMenu.close()">
<md-menu-item ng-repeat="item in [1, 2, 3]">
<md-button>
Option {{item}}
</md-button>
</md-menu-item>
</md-menu-content>
</md-menu>
</md-card-title>
</md-card>
</div>
Heres is the code in my app config:
ribtApp.config(function ($stateProvider,$mdThemingProvider,$locationProvider) {
$stateProvider
.state('login', {
url: '',
templateUrl: 'ribtComponents/ribtLogin/ribtLogin.html',
controller: 'ribtLoginController'
})
.state('home', {
url: '/home',
templateUrl: 'ribtComponents/ribtHome/ribtHome.html',
controller: 'ribtStateServicesController'
})
.state('dashboard', {
url: '/dashboard/{ribtNameState}',
templateUrl: 'ribtComponents/ribtDashboard/ribtDashboard.html',
controller: 'ribtStateServicesController',
resolve:{
ribtNameState: ['$stateParams', function($stateParams){
return $stateParams.ribtNameState;
console.log($stateParams.ribtNameState)
}]
}
});
I tried a console log to see if the param was being changed and it is but it does nothing to the URL path. I've tried a lot of code that I've found in stackoverflow but none of them has worked. I would be deeply thankful if you could help me with this.
You need to switch your ng-hrefs to ui-srefs, which is what ui-router uses to set up links to change states. So something like...
<div class="col-md-6 nopadding" ng-repeat="estado in estados | filter:busqueda">
[...]
<a ng-click="doWork(estado.idState)" ng-href="index.html#!/dashboard/{{estado.idState}}">
should be something more like...
<div class="col-md-6 nopadding" ng-repeat="estado in estados | filter:busqueda">
[...]
<a ng-click="doWork(estado.idState)" ui-sref="dashboard({ribtNameState: estado.idOrWhatever})">
with the caveat that I don't know what doWork is actually doing, so I don't know if that is necessary.
If you need to transition in a controller, you can also inject $state and use $state.go(...), but most of the time ui-srefs can get it handled in my experience.

Angular material md-button hover color change

<div class="non-active" layout layout-align='space-around center'>
<md-button ng-click="$ctrl.widget.type = 'chart'; $ctrl.validateForm()" layout='column' ng-class="{selIcon : $ctrl.widget.type == 'chart'}">
<md-icon md-menu-align-target md-svg-src="trending-up"></md-icon>
<label>{{'LABELS.CHART' | translate}}</label>
</md-button>
<md-button ng-click="$ctrl.widget.type = 'card';$ctrl.validateForm()" layout='column' ng-class="{selIcon : $ctrl.widget.type == 'card'}">
<md-icon md-menu-align-target md-svg-src="info-white"></md-icon>
{{'LABELS.CARD' | translate}}
</md-button>
<md-button ng-click="$ctrl.widget.type = 'currentValue'; $ctrl.validateForm()" layout='column' ng-class="{selIcon : $ctrl.widget.type == 'currentValue'}">
<md-icon md-menu-align-target md-svg-src="filter2"></md-icon>
{{'LABELS.CURRENTVAL' | translate}}
</md-button>
</div>
///csss
.selIcon {
background: #91a865;
}
.non-active{
.md-active-button:hover{
background-color: #91a865;
}
}
as you can see, my button when clicked is set to green not by md-raised but by simple css (.selIcon {
background: #91a865;
}) . The only problem is I want to remove the hover effect to gray color. once the button is clicked and turned green when I hover it's going to gray. How do I disable this feature? also, can I wrap it inside a div so the hover disable only affect this particular button
You only need to target the class of the md-button element which is .md-button and change the background-color using css. md-button element of angular material by default has the grey background color.
Here is a working plunker

Injecting $mdMenu into scope. Cannot read property open() of undefined

I am trying to get the Angular Material Design menu to work but I don't seem to be able to use the $mdMenu that is supposed to be injected by the ng-click.
My HTML markup:
<div layout="column" layout-fill ng-controller="AuthControl">
<md-toolbar ng-controller="navigationControl">
<div ng-controller="menu as ctrl">
<md-menu>
<md-button class="md-icon-button" ng-click="ctrl.open($mdMenu, $event)">
<md-icon>menu</md-icon>
</md-button>
<md-menu-content width="4">
<md-menu-item>
<md-button>
<md-icon>account_circle</md-icon>
</md-button>
</md-menu-item>
</md-menu-content>
</md-menu>
</div>
</md-toolbar>
</div>
The Angular Controller:
controllers.controller('menu', function menuControl($mdDialog) {
var originatorEv;
this.open = function($mdMenu, ev) {
originatorEv = ev;
$mdMenu.open(ev);
};
});
The contoller gets injected properly but when I run I get the Error
TypeError: Cannot read property 'open' of undefined
Does anybody know how to fix this?
Thanks
Instead of mdMenu, pass mdOpenMenu
<md-button aria-label="menu" class="md-fab md-mini md-primary" ng-click="ctrl.openMenu($mdOpenMenu, $event)">
Controller:
this.openMenu = function($mdOpenMenu, ev) {
originatorEv = ev;
$mdOpenMenu(ev);
};
DEMO
"$mdOpenMenu" has been replaced by "$mdMenu.open" and is now deprecated. Use the latest version of Angular Material and it will work just fine.

Deleting row in a list

I have an array of JSON objects that contain 2 key and values. This array is populated in a md-list to show its content. However when I try to delete a row in the list, it doesn't delete the row in the list UI.
Following is a simplified example:
<md-list>
<md-subheader class="md-no-sticky">Learning Center Activity Name</md-subheader>
<md-list-item class="md-2-line"
ng-repeat="learningCenterActivityName in $parent.mondayLearningCenterActivityNameList track by $index"
ng-hide="$parent.mondayLearningCenterActivityNameList[$index]==undefined">
<div class="md-list-item-text compact">
<h3>{{learningCenterActivityName}}</h3>
<md-button class="md-raised md-primary" flex="none"
ng-click="deleteLearningCenterListItem($index)">x
</md-button>
</div>
<md-divider></md-divider>
</md-list-item>
</md-list>-->
CONTROLLER CODE:
$scope.deleteLearningCenterListItem = function(index) {
delete $scope.mondayLearningCenterActivityNameList[index];
};
Use array.splice instead of delete and AngularJS will update:
$scope.deleteLearningCenterListItem = function(index) {
$scope.mondayLearningCenterActivityNameList.splice(index, 1);
};

Angular Material switch between show and hide programmatically

Is it possible to switch between the attributes show and hide by button click (programmatically)? I have for example a card with a map and a list view.
These are usually displayed side by side. On mobile devices, but is for the List view flex = 100 increases. The map is no longer displayed. The user should however have the possibility to switch between the two views. How I can do that?
My example tags:
<md-card flex-gt-xs="40" flex-xs="100">
<list></list>
</md-card>
<md-button>toggle Views</md-button>
<md-card flex="60" hide-xs show-gt-xs >
<leaflet height="40vh" ></leaflet>
</md-card>
Update:
Summary
I would like to have 2 columns that can be switched on mobile devices, and side by side on larger devices.
I'm not 100% sure what you are asking but this CodePen demonstrates the basics of toggling programatically.
md-button has an ng-click attribute that calls the function toggle() which toggles the view value. view is passed to the ng-if of each card.
Markup
<div ng-controller="AppCtrl" ng-cloak="" ng-app="MyApp" style="height:100%" layout="column">
<md-card flex-gt-xs="40" flex-xs="100" ng-if="view">
Card 1
<list></list>
</md-card>
<md-button ng-click="toggle()">toggle Views</md-button>
<md-card flex="60" hide-xs show-gt-xs ng-if="!view">
Card2
<leaflet height="40vh" ></leaflet>
</md-card>
</div>
JS
angular.module('MyApp',['ngMaterial', 'ngMessages', 'material.svgAssetsCache', 'ngDialog'])
.controller('AppCtrl', function($scope) {
$scope.view = true;
$scope.toggle = function () {
$scope.view = !$scope.view;
}
});
You can replace ng-if with ng-show if you need to retain information in elements that you are toggling as ng-if recreates the element each time it is true.
I have found the solution. $mdMedia does all the magic.
js:
$scope.$watch(function () {
return $mdMedia('sm');
}, function (big) {
$scope.screenIsSmall = $mdMedia('sm');
});
View:
<md-card flex="60" ng-hide="screenIsSmall&&!showMap" style="max-height: 40vh">
<md-button ng-show="screenIsSmall" ng-click="showMap = !showMap">toggle</md-button>
<leaflet height="40vh"></leaflet>
</md-card>
app.component.html
<div fxLayout="column" fxLayout.gt-sm="row wrap">
<div fxFlex="50" class="flex-p">
<mat-slide-toggle
[checked]="isSlideChecked"
(change)="toggleChanges($event)"
>Hide/Show - card</mat-slide-toggle
>
</div>
</div>
<mat-card *ngIf="isSlideChecked">Simple card</mat-card>
app.component.ts
isSlideChecked: boolean = false;
toggleChanges($event: MatSlideToggleChange) {
this.isSlideChecked = $event.checked;
}

Categories