How to display the contents of bootstrap modal tabs? - javascript

I am a very beginner to AngularJS,I have a bootstarp modal with 2 tabs I have made it to display only one tab at a time.In the modal I have two buttons NEXT and PREVIOUS I am able to switch between the tabs through these buttons.But the issue is the content in their respective tabs wont change when I change the tab.Where am I going wrong.Kindly help me.
<div class="modal-body">
<div role="tabpanel">
<!-- Nav tabs -->
<ul class="nav nav-tabs">
<li class="active">
<a href="#tab1" data-toggle="tab">
<div>{{current.data}}</div>
</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab1">
<textarea class="form-control" ng-model="first" name="first" required></textarea>
<md-button class="md-raised md-primary" ng-click="next()">next</md-button>
<md-button class="md-warn md-raised" ng-click="previous()">previous</md-button>
</div>
<div class="tab-pane" id="tab2">
<textarea class="form-control" ng-model="second" name="second" required></textarea>
<md-button class="md-raised md-primary" ng-click="next()">next</md-button>
<md-button class="md-warn md-raised" ng-click="previous()">previous</md-button>
</div>
</div>
</div>
Controller.js
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.dataSet = [
{ data: "First", index: 0 },
{ data: "Second", index: 1 }
],
$scope.current = $scope.dataSet[0],
$scope.next = function() {
var i = $scope.getIndex($scope.current.index, 1);
$scope.current = $scope.dataSet[i];
},
$scope.previous = function() {
var i = $scope.getIndex($scope.current.index, -1);
$scope.current = $scope.dataSet[i];
},
$scope.getIndex = function(currentIndex, shift) {
var len = $scope.dataSet.length;
return (((currentIndex + shift) + len) % len)
}
});

I have updated your code with the ng-class attribute for your selected data kindly refer https://jsfiddle.net/jasonantho/fwowg8q5/

Related

Expandable div with buttons in Angularjs

That's my problem: http://jsfiddle.net/2f6qscrp/311/
shortly: I've got a div that can be expandable and it reveals a new div. This kind of event must be in all div. So, in each section of this div I can open it (like an accordion). The problem is that if i click one of the buttons on this div (they must be there) the div opens. I would avoid this thing if possible. Is it possible?
<div ng-app='home'>
<!-- App goes here -->
<md-content md-scroll-y flex layout="column" class="_md ng-scope layout-column flex" layout-padding ng-controller="MainCtrl as mainCtrl">
<div layout="column" md-scroll-y>
<div layout="row" ng-click="showDetailsFunction();" class="div-outline-none div-cursor-pointer">
<h3 flex class="div-card-panel-title">
Blablablabla
</h3>
<span flex></span>
<ul flex class="div-doc-act div-flex">
<li>
<md-button class="md-icon-button no-margin"
data-ng-click="doGreeting('btn1');">
btn1
</md-button>
</li>
<li>
<md-button class="md-icon-button no-margin"
data-ng-click="doGreeting('btn2');">
btn2
</md-button>
</li>
<li>
<md-button class="md-icon-button no-margin"
data-ng-click="doGreeting('btn3');">
btn3
</md-button>
</li>
</ul>
</div>
<div data-ng-if="showDetails" class="animate-if">
<div>
wevniwernviurevnier
</div>
</div>
</div>
</md-content>
</div>
Angular part
angular.module('home', ['ngAria', 'ngAnimate', 'ngMaterial']);
angular.module('home').config(function ($mdThemingProvider) {
$mdThemingProvider.theme('default')
.primaryPalette('pink')
.accentPalette('grey');
});
angular.module('home').controller('MainCtrl', function ($scope, $window) {
$scope.showDetails = false;
$scope.showDetailsFunction = function() {
$scope.showDetails = !$scope.showDetails;
};
$scope.greeting = 'Hello, World!';
$scope.doGreeting = function(greeting) {
$window.alert($scope.greeting + " " + greeting);
};
});
you can try using event.stopPropagation(); method to avoid the div opening on the button click.
$scope.(your div name) = function($event) {
$event.stopPropagation();
// Some code to find and display the next image
}

Md-tabs AngularJS working once only

I am new at using angularjs and I've been encountering some issues on md-tabs. The next button is working but when I click again on the first tab the next button is not working anymore. Can you please take a look on my codes and tell me what's wrong. Thanks!
VIEW
<div class="row">
<div class="col-md-12" ng-class="{ 'blur' : vm.preloader.action }">
<md-tabs md-dynamic-height md-border-bottom md-selected="selectedIndex">
<md-tab label="Campaign Details">
<md-content class="md-padding">
<div class="md-content">
<div class="row" style="margin-top: 3%;">
<div class="col-md-6">
Content 1
</div>
</div>
<br>
<div class="row">
<div class="col-md-3">
<button class="btn btn-primary btn-lg" style="border-radius: 0; padding-right: 50px; padding-left: 50px; width: 100%;" ng-click="nextTab()">NEXT</button>
</div>
</div>
</div>
</md-content>
</md-tab>
<md-tab label="Recipients">
<md-content class="md-padding">
<div class="md-content">
<div class="row">
Content 2
</div>
<div class="row">
<div class="col-md-3">
<button class="btn btn-primary btn-lg" ng-click="nextTab()" style="width: 100%;">NEXT</button>
</div>
</div>
</div>
</md-content>
</md-tab>
<md-tab label="Confirm">
<md-content class="md-padding" ng-show="show_send_to_all">
<div class="md-content">
<div class="row" style="margin-top: 3%;">
Content 3
</div>
<div class="row">
<div class="col-md-3">
<button class="btn btn-primary btn-lg" ng-click="" style="width: 100%;">SEND</button>
</div>
</div>
</div>
</md-content>
</md-tab>
</md-tabs>
</div>
CONTROLLER:
(function(){
'use strict';
angular
.module('app')
.controller('SmsManagementController', SmsManagementController);
SmsManagementController.$inject = ['$rootScope', 'PreloaderService', 'ToastService', '$mdDialog', 'CaptchaService', '$localStorage', 'StaffRole', 'API'];
function SmsManagementController($rootScope, PreloaderService, ToastService, $mdDialog, CaptchaService, $localStorage, StaffRole, API) {
var vm = this;
// Variables
vm.totalrecord = { general: 0 };
vm.publicKey = API.captchakey;
vm.displayCaptcha = !($.inArray($localStorage.currentUser.role, StaffRole) > -1);
vm.preloader = {
general: true
};
// Initialization
Initialize();
// Public functions
function Initialize(){
PreloaderService.Display();
PreloaderService.Hide();
}
//Tabs
$rootScope.max = 2;
$rootScope.selectedIndex = 0;
$rootScope.nextTab = function() {
var index = ($rootScope.selectedIndex == $rootScope.max) ? 0 : $rootScope.selectedIndex + 1;
$rootScope.selectedIndex = index;
};
}
})();
Or maybe you can suggest a code on how will I convert this on jquery or javascript in order for me to have a next buttons for the tabs. I'm using a material design tabs of angularjs.
Your NEXT button is not working because in your next() method you are doing iterations on $rootscope, do those iterations on $scope..
$scope.max = 2;
$scope.selectedIndex = 0;
$scope.nextTab = function() {
var index = ($scope.selectedIndex == $scope.max) ? 0 : $scope.selectedIndex + 1;
$scope.selectedIndex = index;
};
You are using controllerAs syntax so prefer using vminstead of $scope and also pass vm.selectedIndex here
<md-tabs md-dynamic-height md-border-bottom md-selected="vm.selectedIndex">
Finally, I figured it out.
Instead of using $rootscope, I changed it to vm. So, here's my new code for the tabs:
//Tabs
vm.max = 2;
vm.selectedIndex = 0;
$rootScope.nextTab = function() {
var index = (vm.selectedIndex == $rootScope.max) ? 0 : vm.selectedIndex + 1;
vm.selectedIndex = index;
};
And based on #Rakeschand solution, I passed vm.selectedIndex like this:
<md-tabs md-dynamic-height md-border-bottom md-selected="vm.selectedIndex">
Thanks #Rakeschand for the idea :)

Filtering and Sorting in Ionic Service Factory

sorry for newbie question. I want to add filtering and sorting by name to my page. When i try using this code it is still work when i am using list of items from variable in controller scope but after i am using factory in services.js, there is two problem:
When i am typing the search word in search box, filter is work and only show the filtered item but i can't clear the filter again (when i am deleting the search word with backspace the item is still filtered)
The order by doesn't work anymore for sorting the items
Here is the code :
(HTML)
<ion-view view-title="Foods">
<ion-content class="padding">
<div class="bar bar-header item-input-inset">
<label class="item-input-wrapper">
<i class="icon ion-ios-search placeholder-icon"></i>
<input type="search" placeholder="Search" ng-model="foodSearch.search">
</label>
</div>
<button class="button button-block button-energized" ng-click="showOrderOptions()">
Order by
</button>
<div class="list ">
<a class="item" href="#" ng-hide="isOrderOptionsHide" ng-click="reverse=!reverse;order('name',reverse)">
Name
</a>
<a class="item" href="#" ng-hide="isOrderOptionsHide" ng-click="reverse=!reverse;order('rating',reverse)">
Rating
</a>
</div>
<ion-list>
<ion-item class="list card" ng-repeat="food in foods = (foods | filter: filterFood)"
href="#/tab/foods/{{food.id}}">
<div class="item item-head">
<h2 style="text-align:center;"><b>{{food.name}}</b></h2>
<div class="row">
<div class="col item item-image" style="width:100px;height:100px;">
<img class="" src="{{food.imageSrc}}" >
</div>
<div class="col item item-image">
<h2>{{food.title}}</h2>
<div class="row">
<img src="img/smile.jpg" style="width:100%; height:100%; margin-top: -10px;"class="col col-40">
<h3 class="col col-60" >{{food.rating}}</h3>
</div>
</div>
</div>
</div>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
(Controller.js):
.controller('FoodsCtrl', function($scope,Foods,$filter) {
$scope.foods = Foods.all();
$scope.remove = function(food) {
Foods.remove(food);
};
$scope.foodSearch = {search: ''};
$scope.filterFood = function (item){
return item.title.toLowerCase().indexOf($scope.foodSearch.search.toLowerCase()) >= 0
|| item.name.toLowerCase().indexOf($scope.foodSearch.search.toLowerCase()) >= 0;
}
$scope.isOrderOptionsHide = true;
$scope.reverse=true;
$scope.showOrderOptions = function (){
$scope.isOrderOptionsHide = !$scope.isOrderOptionsHide;
}
var orderBy = $filter('orderBy');
$scope.order = function(predicate, reverse) {
$scope.foods = orderBy($scope.foods, predicate, reverse);
$scope.isOrderOptionsHide = true;
};
})
Just do this way ng-repeat="food in foods | filter: foodSearch.search | orderBy: 'name'" You do not have to do this :
$scope.filterFood = function (item){
return item.title.toLowerCase().indexOf($scope.foodSearch.search.toLowerCase())..
angular filter automaticaly does that.

Sorting object in angularjs whilst paginating

I have data coming from a server that I want to display. This data is being paginated and also sorted. The relevant part of the controller is as follows:
appControllers.controller('PostsCtrl',
['$scope', 'Restangular', 'messageCenterService',
'$location', '$anchorScroll',
'$filter',
function ($scope, Restangular, messageCenterService,
$location, $anchorScroll, $filter) {
// Get all posts from server.
var allPosts = Restangular.all('api/posts');
allPosts.getList().then(function (posts) {
$scope.posts = posts;
$scope.predicate = 'rank';
$scope.reverse = false;
$scope.itemsPerPage = 10;
$scope.currentPage = 1;
$scope.totalItems = $scope.posts.length;
$scope.pageCount = function () {
return Math.ceil($scope.totalItems / $scope.itemsPerPage);
};
$scope.$watch('currentPage + itemsPerPage',
function () {
var begin = (($scope.currentPage - 1) * $scope.itemsPerPage),
end = begin + $scope.itemsPerPage;
$scope.position = (10 * ($scope.currentPage - 1));
$scope.filteredPosts = $scope.posts.slice(begin, end);
$location.hash('top');
// call $anchorScroll()
$anchorScroll();
});
});
Here is the html:
<li class="list-group-item" ng-repeat="post in filteredPosts | orderBy:predicate:reverse">
<div class="row-fluid">
<div class="col-sm-1 ">
<p style="margin-top: 1em">{{($index + 1) + position}}.</p>
</div>
<div class="col-sm-1 v-center">
<i class="fa fa-angle-up"></i> <br />
<small>{{post.votes}}votes</small> <br />
<i class="fa fa-angle-down"></i>
</div>
<div class="col-sm-8">
<h4><a href={{post.url}}>{{post.title}}</a></h4>
<div>
<ul class="list-inline">
<li ng-repeat="tag in post.tags">
<span class="fa fa-tag"></span>
<button ng-click="getTags(tag.id)">{{tag.name}}</button>
</li>
</ul>
<div>
<em>{{ post.created_at | timeAgo}}</em>
by <cite>{{post.author.username}}</cite>
</div>
<div>
<a href ng-click="select(post)">
<span class="fa-stack fa-2x">
<i class="fa fa-comment fa-stack-2x"></i>
<strong class="fa-stack-1x fa-stack-text fa-inverse">
{{post.comments.length}}
</strong>
</span>
</a>
</div>
</div>
<div class="panel" ng-show="isSelected(post)">
<ul class="nav nav-list" ng-repeat="comment in post.comments">
<li class="list-group-item">
<strong><cite>{{comment.author.username}}</cite></strong>
{{comment.updated_at | timeAgo }}
<p>{{comment.message}}</p>
</li>
</ul>
<form name="CommentForm" novalidate role="form" ng-if="user.isLoggedIn" ng-submit="CommentForm.$valid && comment(post)">
<label for="InputComment">Add a Comment</label>
<textarea ng-model="newComment.message" class="form-control" id="InputComment" rows="3" required></textarea>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
</li>
<pagination total-items="totalItems" items-per-page="itemsPerPage"
ng-model="currentPage" ng-change="pageChanged()"></pagination>
I need some help with figuring out how to filter the order in which the data is displayed on the page. I have read other questions which ask about sorting but I did not see any where the content is also paginated with angularjs. The question I have is how do I do a live filter considering that I have objects coming back from the server and the content is then paginated?
You could create a paging filter, and combine it with orderBy to simulate filteredPosts.
app.filter('page', function () {
return function (input, start, end) {
return input.slice(start, end);
};
});
Then you can apply both filters in your template.
ng-repeat="post in posts | orderBy:predicate:reverse | page:begin:end"
You can use the orderBy filter also programmatically to sort your array
$scope.posts = posts;
$scope.posts=$filter('orderBy')($scope.posts,'rank',false );
$scope.itemsPerPage = 10;
...
$filter u have already as a dependency in your controller

Bootstrap radio tabs not working with Knockout

I tried to use Bootstrap radio button data toggle for tabs, my render of DOM for this happens through Knockout. the tab does not seem to switch between active and inactive tab correctly.
Here is a working example: http://jsfiddle.net/msurguy/x8GvJ/
Here is how to recreate the problem (JSFiddle):
<div id="tabtest" data-bind="foreach: arrayItems">
<div data-bind="attr:{'id':'itemtab' + $index()}" class="btn-group " data-toggle="buttons-radio">
<a data-bind="attr:{href:'#entitysearchitem' + $index()}" class="btn " style="background-color: #f5f5f5" data-toggle="tab">Item</a>
<a data-bind="attr:{href:'#entitymemberssearch' + $index(),'data-index':$index}" style="background-color: #f5f5f5" class="btn " data-toggle="tab">Member</a>
</div>
<div class="tab-content">
<div class="tab-pane fade in " data-bind="attr:{id:'entitysearchitem' + $index()}">
<div class="row padding3centt">
<div class="col-xs-12">
<div class="row">
<div class="col-xs-1"></div>
<div class="col-xs-11">
<ul data-bind="foreach:interests" class="list-styled">
<li>
<span class="fontsize80" data-bind="text:Description">
</span>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="tab-pane fade" data-bind="attr:{id:'entitymemberssearch' + $index()}">
Hello i am entitymemberssearch
</div>
</div>
<br/>
</div>
JS:
var myObject = [{
"first": "John",
"interests": [ {"Description": "Reading"}, {"Description":"Mountain Biking"}, {"Description":"Hacking"} ]
},
{
"first": "Boy",
"interests": [ {"Description":"Reading"}, {"Description":"Mountain Biking"}, {"Description":"Hacking"} ]
}]
var koData ={
arrayItems :ko.observableArray()
}
ko.mapping.fromJS(myObject, {}, koData.arrayItems);
ko.applyBindings(koData, $("#tabtest").get(0));
I have described the problem in the picture below as well. Any ideas on how to correctly get the bootstrap radio tabs to work?
You need to reconsider the approach you are trying to use : Here's a beautiful solution by Ryan Niemeyer which you can refer to..
http://jsfiddle.net/rniemeyer/cGMTV/
You need to rewrite your code in following format.
var Section = function (name, selected) {
this.name = name;
this.isSelected = ko.computed(function() {
return this === selected();
}, this);
}
var ViewModel = function () {
var self = this;
self.selectedSection = ko.observable();
self.sections = ko.observableArray([
new Section('Section 1', self.selectedSection),
new Section('Section 2', self.selectedSection),
new Section('Section 3', self.selectedSection)
]);
//inialize to the first section
self.selectedSection(self.sections()[0]);
}
ko.applyBindings(new ViewModel());

Categories