ngTable doesn't work with UI-Router - angularjs

I always used the directive ngTable with ngRoute module. However, I recently migrated to the UI-Router for better fit the project I'm developing right now.
But for some reason, ngTable does not work correctly with the UI-Router.
The table is generated normally, however the settings do not.
It is not created neither the header nor paging. As the group also is not working.
I also tried to create the table header manually, but I noticed that it disappears when I add the attribute "ng-include" with the value "templates.header".
I'm used the following versions:
ngTable - 1.0.0-beta.9
UI-Router - 0.2.15
Someone have come across this problem? I can not find the solution anywhere.
JavaScript:
app.controller("users", function($http, $rootScope, $stateParams, $scope, NgTableParams) {
$scope.users = {};
$http({
method: "POST",
url: "./controllers/users/read.php",
data: {
id: $stateParams.id,
language_id: $rootScope.globals.language.id
}
}).success(function(response) {
$scope.tableParams = new NgTableParams({
count: 10,
sorting: {
name: "asc"
}
}, {
dataset: response
});
$scope.users = response;
});
});
HTML:
<table ng-table="tableParams" class="table table-bordered table-striped">
<tr ng-repeat="row in $data">
<td data-title="'txt.pages.users.fields.name' | translate" sortable="'name'" width="45%">
{{ row.name }}
</td>
<td data-title="'txt.pages.users.fields.email' | translate" sortable="'email'" width="45%">
{{ row.email }}
</td>
<td data-title="'txt.pages.users.fields.actions' | translate" width="10%">
<button ng-click="edit(row.id)" class="btn btn-warning btn-sm">
<icon class="glyphicon glyphicon-edit"></icon>
</button>
<button ng-click="delete(row)" class="btn btn-danger btn-sm">
<icon class="glyphicon glyphicon-remove"></icon>
</button>
</td>
</tr>
</table>
stateProvider:
app.config(function($stateProvider, $urlRouterProvider) {
$stateProvider.state("site", {
url: "/",
views: {
"outside#": {
templateUrl: "./template/site/index/read.html"
},
"inside#site": {
templateUrl: "./template/site/home/read.html"
}
}
}).state("cms", {
url: "/cms",
views: {
"outside#": {
templateUrl: "./template/cms/index/read.html"
}
}
}).state("dashboard", {
parent: "cms",
url: "/dashboard",
views: {
"inside#cms": {
templateUrl: "./template/cms/dashboard/read.html"
}
}
}).state("login", {
parent: "cms",
url: "/login",
views: {
"inside#cms": {
templateUrl: "./template/cms/login/read.html"
}
}
}).state("users", {
parent: "cms",
url: "/users",
views: {
"inside#cms": {
controller: "users",
templateUrl: "./template/cms/users/read.html"
}
}
});
});

I think you should use $users instead of $data in below line.
<tr ng-repeat="row in $data">

Related

Pass parameter outside of AngularJS directive into HTML

This is the problem: I want to sort a table by clicking on a table header. To avoid repeating code every , I have made a directive. The table headers show up nicely but I cannot find a way to make them sortable.
This is (part of) my HTML:
<div class="table-responsive" >
<table class="table table-striped" >
<thead>
<tr>
<th> </th>
<th><t-header name="vnaam" hoofd="voornaam" ></t-header></th>
<th><t-header name="anaam" hoofd="achternaam"></t-header></th>
</tr>
</thead>
<tbody>
<tr dir-paginate="cursist in homeCtrl.ckaart | orderBy: homeCtrl.sortKey:!homeCtrl.reverse" current-page="homeCtrl.currentPage">
<td><a ng-click="homeCtrl.gotoDetail(cursist.id)"><span ng-class="cursist.keuzemotivatie.length || cursist.leerdoelen.length ? 'infosign-true' : 'infosign-false'" class="fa fa-info-circle infosign"></span></a></td>
<td>{{cursist.vnaam}}</td>
<td>{{cursist.anaam}}</td>
</tr>
</tbody>
My directive:
.directive('tHeader', function(){
return {
restrict: 'EA',
scope: {
name: "#",
hoofd: "#"
},
templateUrl: 'templates/theader.html',
controllerAs: 'vm',
bindToController: true,
controller: function() {
var vm = this;
vm.sortKey = '-instrument';
// sort t.b.v. table sorting
vm.sort = function(keyname) {
// alert(keyname);
vm.sortKey = keyname;
vm.reverse = !vm.reverse;
console.log(vm.sortKey);
}
}
}
})
And the template belonging to the directive:
<div ng-click="vm.sort(vm.name)">{{vm.hoofd}}
<span ng-show="vm.sortKey === vm.name" ng-class="{ 'fa fa-chevron-up':vm.reverse, 'fa fa-chevron-down':!vm.reverse }"></span>
</div>
It seems to me that the 'vm.sortKey' parameter as well as the 'vm.reverse' parameter are not being passed into the HTML.
Anyone an idea? What mistake do I make?

AngularJS Displaying a sorted $http response in template partial

I'm currently working on a project that I need to sort the response by category and subcategory using Angular.
My controller currently looks like:
function($http, $stateParams) {
let vm = this;
$http({
method: 'GET',
url: url,
timeout: 10000,
contentType: "application/json;odata=verbose",
headers: {"Accept": "application/json;odata=verbose"},
params: {},
}).then(function(data, status, headers, config) {
let documents = data.data.d.results;
$.each(documents, function(i, item){
vm.data = sortDocuments(documents);
})
}).catch(function(error) {
console.log(error);
});
function sortDocuments(documents) {
return _.sortBy(documents, item => {
return {
category: item.Category,
subcategory: item.Subcategory,
documentURL: item.EncodedAbsUrl,
tags: item.Tags
}
})
};
})
I need to sort the category and subcategories in my view, but not sure how to go about doing it. My template partial currently looks like:
<div uib-accordion-group class="panel-default" heading="Subcategory Name">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Tags</th>
<th>Link</th>
</tr>
</thead>
<tbody ng-show="item.subcategory == 'Subcategory Name'" ng-repeat="item in documents.data">
<tr>
<td>
<p>{{ item.name }}</p>
</td>
<td>
<p>{{ item.tags.results }}</p>
</td>
<td>
<a href="{{ item.documentURL }}">Open
Document</a>
</td>
</tr>
</tbody>
</table>
</div>
Can anyone tell me what I'm missing in order to sort the view by category and subcategory?
I'm not sure why you're using _.map there. _.sortBy should do what you need.
http://underscorejs.org/#sortBy
this should be pretty close:
vm.data = _.sortBy(data.data.d.results, document => { return document.Category + document.Subcategory } )

How to filter a row in AngularJS based on data in a particular 'td'

I have a table made in AngularJS and I am trying to learn SortBy and Filter options.
I want to sort the table based on data in a particular TD. I don't want to filter by inputting a filter manually, but I want to hardcode that filter to the table.
My current table is :
<table ng-table="talentPoolList" show-filter="true" class="table table-striped table-bordered">
<tr ng-repeat="employee in data | filter: testFilter">
<td data-title="'#'">{{$index + 1}}</td>
<td data-title="'First Name'" sortable="'employee.employee.firstName'" filter="{ 'employee.employee.firstName': 'text' }">
{{employee.employee.firstName}}
</td>
<td data-title="'Last Name'" sortable="'employee.employee.lastName'" filter="{ 'employee.employee.lastName': 'text' }">
{{employee.employee.lastName}}
</td>
<td data-title="'Current State'" sortable="'currentState'" filter="{ 'currentState': 'text' }">
{{employee.state.state}}
</td>
<td data-title="'Reserve to (state):'" ng-if="employee.state.state == 'Available' ">
<select>
<option disabled selected value> -- select an option -- </option>
<option id="123">123</option>
<option id="234">234</option>
<option id="345">345</option>
<option id="456">456</option>
<option id="567">567</option>
</select>
</td>
</tr>
</table>
In the above table, I want the filtering to be done based on the :
<td data-title="'Current State'" sortable="'currentState'" filter="{ 'currentState': 'text' }">
{{employee.state.state}}
</td>
{{employee.state.state}} returns several values. They can be any one of the following :
1. Available
2. Resigned
3. OnNotice
4. Blocked
5. Accepted
6. Rejected
7. Shadow
I want the table to only display the states Available and Blocked..
My Controller is:
'use strict';
angular.module('employeeTalentPool', []);
//Routers
myApp.config(function ($stateProvider) {
$stateProvider.state('employeeTalentPool', {
url: '/employeeTalentPool',
templateUrl: 'partials/talentPoolEmployees/employeeTalentPool.html',
data: {
auth: true
}
});
});
//Factories
myApp.factory('employeeTalentPoolServices', ['$http', function ($http) {
var factoryDefinitions = {
//getAllBenchers: function () {
// return $http.get(myApp.TalentPoolBaseUrl + '/EmployeeState?state=Available&pageNumber=1&pageSize=5').success(function (data) { return data; });
// },
getAllBenchers: function () {
return $http.get(myApp.TalentPoolBaseUrl + '/EmployeeState?state=&pageNumber=0&pageSize=0').success(function (data) { return data; });
},
reserveEmployee: function () {
return $http.post('').success(function (data) { return data;});
}
}
return factoryDefinitions;
}
]);
//Controllers
myApp.controller('getAllBenchersController', ['$scope', 'employeeTalentPoolServices', function ($scope, employeeTalentPoolServices) {
employeeTalentPoolServices.getAllBenchers().then(function (result) {
$scope.data = result.data;
$scope.testFilter = function (item) {
return (item.state.state.toLowerCase() === 'available' || item.state.state.toLowerCase() === 'rejected');
}
});
}]);
Can anyone help me with creating a hard coded filter in the table that will show only those two states?
Thank You
angular.module("testApp", [])
.controller("testController", function testController($scope) {
$scope.data = [{
state: {
state: 'Available'
}
}, {
state: {
state: 'Available'
}
}, {
state: {
state: 'Misc'
}
}, {
state: {
state: 'Rejected'
}
}, {
state: {
state: 'Available'
}
}, {
state: {
state: 'Abc'
}
}, ];
$scope.testFilter = function(item) {
return (item.state.state.toLowerCase() === 'available' || item.state.state.toLowerCase() === 'rejected');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<table ng-table="talentPoolList" show-filter="true" class="table table-striped table-bordered" ng-controller="testController" ng-app="testApp">
<tr ng-repeat="employee in data | filter: testFilter">
<td>{{employee.state.state}}</td>
</tr>
</table>
<tr ng-repeat="employee in data | filter: {state: 'Available'}">
The simplest way to do this is like above.
You can also write your own filter, as shown in the snippet

How to get data from smart-table plugin?

I am trying to adapt the example of how to use a custom plugin with smart-table. See the "Create your own plugin" section of http://lorenzofox3.github.io/smart-table-website/
Here is a screen shot of my web app so far:
The problem I have is that I do not know how to get the items select from the smart-table. The selected items on the smart-table are "Beer" and "Mountain Biking". Here's my app.js file:
var app = angular.module("app", ['smart-table']);
app.directive('csSelect', function () {
return {
require: '^stTable',
template: '<input type="checkbox"/>',
scope: {
row: '=csSelect'
},
link: function (scope, element, attr, ctrl) {
element.bind('change', function (evt) {
scope.$apply(function () {
ctrl.select(scope.row, 'multiple');
});
});
scope.$watch('row.isSelected', function (newValue, oldValue) {
if (newValue === true) {
element.parent().addClass('st-selected');
} else {
element.parent().removeClass('st-selected');
}
});
}
};
});
Here is my controller:
app.controller("vendorCtrl", function($scope,$http) {
$scope.vendor_to_look_for = "";
$scope.newvendor = "";
$scope.newlogo = "";
$scope.vendors_types = [];
$scope.itemsByPage=5;
$http.get("http://192.168.1.115:8080/vendor").then(function(response) {
$scope.all_vendors = response.data;
});
$http.get("http://192.168.1.115:8080/type").then(function(response) {
$scope.all_types = response.data;
});
$scope.submit_vendor = function() {
// alert("add new vendor [" + $scope.newvendor + "]" );
var data = $.param({ vendor_name: $scope.newvendor, vendor_logo: $scope.newlogo, vendors_types: $scope.vendors_types });
$http.post("http://192.168.1.115:8080/vendor/add",
// [{'vendor': $scope.newvendor}],
data,
{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(function(response) {
console.log(response);
console.log(response.data);
});
$http.get("http://192.168.1.115:8080/vendor").then(function(response) {
console.log(response);
console.log(response.data);
$scope.all_vendors = response.data;
});
};
});
Update: Here's the pertinent HTML:
<form ng-submit="submit_vendor()">
<table>
<tr><td><label>Vendor name</label></td><td><input type="text" ng-model="newvendor" placeholder="Name"></td></tr>
<tr><td><label>Logourl</label></td><td><input type="text" ng-model="newlogo" placeholder="Url"></td></tr>
</table>
<table st-table="displayedCollection" st-safe-src="all_types" class="table table-striped">
<thead>
<tr>
<th st-sort="type">Select</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in displayedCollection">
<td cs-select="x"></td>
<td>{{x.type}}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" class="text-center">
<div st-pagination="" st-items-by-page="itemsByPage" st-displayed-pages="7"></div>
</td>
</tr>
</tfoot>
</table>
<button type="submit" class="btn btn-success btn-lg btn-block"><span class="glyphicon glyphicon-flash"></span> Add </button>
</form>
When I click the add button my submit_vendor() function executes but my the vendor_types I pass in my form data is empty. How do I get the items selected in my table into my form data?
Thanks!
I found this https://github.com/vitalets/checklist-model
This makes things a lot easier. I do not need to declare my csSelect directive :)

AngularJS Router and ui-view

Hi I have having trouble getting my router to work. I have created a page called lookup.html and I've added it to my router file, but it's not loading in the ui-view when I go to the url I've specified in the router. I've created a plnkr and hope it will help.
http://plnkr.co/edit/II7Mv7jNYENhJHKX1ht5?p=linter
here's my router:
(function () {
'use strict';
angular
.module('crm.ma')
.config(config);
config.$inject = ['$stateProvider', '$urlRouterProvider'];
function config($stateProvider, $urlRouterProvider) {
$stateProvider
.state('login', {
url: '/login?token',
data: {
skipAuth: true
},
resolve: {
valid: function (authService, $stateParams, sessionService, $state) {
if (!sessionService.getSession()) {
if ($stateParams.token) {
sessionService.setSession($stateParams.token);
} else {
sessionService.removeSession();
}
}
authService.login();
}
}
})
.state('main', {
abstract: true,
templateUrl: 'app/components/common/layout.html',
controller: 'mainController as vm',
data: {
pageTitle: 'Homepage'
}
})
.state('main.index', {
url: '/',
templateUrl: 'app/main/main.html',
controller: 'mainController as vm',
data: {
pageTitle: 'Homepage'
}
})
.state('search', {
url: '/search',
templateUrl: 'app/advancedsearch/advanced-search.html',
controller: 'advancedsearch.controller as vm',
data: {
pageTitle: 'Search'
}
})
.state('lookup', {
url: '/lookup',
templateUrl: 'app/components/common/lookup.html',
controller: 'LookUpCtrl as vm',
data: {
pageTitle: 'Look Up'
}
})
.state('dealer', {
url: '/dealer',
templateUrl: 'app/components/common/layout.html',
controller: 'dealerController as vm',
data: {
pageTitle: 'Dealer'
}
});;
$urlRouterProvider.otherwise('/');
}
})();
and here is my lookup.html page.
<div>
<div>Lookup Results</div>
<table>
<thead>
<tr>
<td>Acc. ID</td>
<td>Acc. Name</td>
<td>Acc Address</td>
<td>City</td>
<td>Zip</td>
<td>Phone</td>
<td>Parent Name</td>
<td>Account Type</td>
<td>Account Status</td>
<td>Credit Term</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="result in vm.results">
<td>{{ result.accountId }}</td>
<td>{{ result.accountName }}</td>
<td>{{ result.address }}</td>
<td>{{ result.city }}</td>
<td>{{ result.state }}</td>
<td>{{ reuslt.zip }}</td>
<td>{{ result.phone }}</td>
<td>{{ result.parentName }}</td>
<td>{{ result.accountType }}</td>
<td>{{ result.accountStatus }}</td>
<td>{{ result.accountStatus }}</td>
<td>{{ result.creditTerm }}</td>
</tr>
</tbody>
</table>
If any further info is needed please let me know. I didn't write this, and I'm still a little unsure on how to use the router.
Maybe you has imported angular-route.js and not angular-ui-route.js; they are differents. With angular-route.js you should use:
<div ng-view></div>
And in angular-ui-route.js you should use:
<div ui-view></div>
Another problem maybe is the first line of you router file, it should be like:
'use strict';
angular.module(.........

Resources