Angular UI accordion with buttons in the header part - angular-ui-bootstrap

I am using the accordion directive from http://angular-ui.github.com/bootstrap/ and I need to have two buttons in the header part.
Add - Create exactly same accordion below the original.
Remove - Remove the accordion. (the first accordion can not be removed -
disable the Remove button).
I am new to AngularJS and please help me to achieve this.

See a working plunker.
You just need a add and remove function in your controller
$scope.addGroup = function(idx, group, e) {
if (e) {
e.preventDefault();
e.stopPropagation();
}
var newGroup = angular.copy(group);
newGroup.no = $scope.groups.length + 1;
$scope.groups.splice(idx + 1, 0, newGroup);
};
$scope.removeGroup = function(idx, e) {
if (e) {
e.preventDefault();
e.stopPropagation();
}
$scope.groups.splice(idx, 1);
};
and a ng-repeat for your html:
<accordion close-others="oneAtATime">
<accordion-group heading="{{group.title}}" ng-repeat="group in groups">
<accordion-heading>
{{ group.title }} ({{group.no}})
<button class="btn btn-small" ng-click="addGroup($index, group, $event)">+</button>
<button class="btn btn-small" ng-click="removeGroup($index, $event)" ng-show="$index">-</button>
</accordion-heading>
{{group.content}}
</accordion-group>
</accordion>

put just this e.originalEvent.cancelBubble=true;
$scope.addGroup = function(idx, group, e) {
if (e) {
e.originalEvent.cancelBubble=true;
}
var newGroup = angular.copy(group);
newGroup.no = $scope.groups.length + 1;
$scope.groups.splice(idx + 1, 0, newGroup);
};
$scope.removeGroup = function(idx, e) {
if (e) {
e.originalEvent.cancelBubble=true;
}
$scope.groups.splice(idx, 1);
};

Related

Is it possible to transclude the tab content?

Is it possible to create a tab with either HTML content or move the element directly to the content?
Here is what I am trying to do, ultimately, i want to just move existing element(s) inside the tab content:
HTML
<body ng-controller="MainCtrl">
<div class="container">
<tabset ui-sortable="sortableOptions" ng-model="tabs">
<tab class="tab" ng-repeat="tab in tabs" sortable-tab heading="{{tab.title}}">{{tab.content}}</tab>
</tabset>
<button type="button" ng-click="addSimple()">Add simple</button>
<button type="button" ng-click="addHtml()">Add html</button>
<button type="button" ng-click="addElement()">Add element</button>
<button type="button" ng-click="removeTab()">Remove tab</button>
</div>
</body>
JS
var app = angular.module('plunker', ['ui.bootstrap', 'ui.sortable']);
app.controller('MainCtrl', function($scope) {
var count = 0;
$scope.sortableOptions = {
distance: 10,
axis: 'x',
update: function(event, ui) {
// $scope.activeTab = ui.item.sortable.dropindex;
}
};
$scope.tabs = [
{title:'Test', content:'hello world'},
];
$scope.addSimple = function() {
count++;
var newTab = {title: 'New tab ' + count, content: 'Works'};
$scope.tabs.push(newTab);
}
$scope.addHtml = function() {
count++;
var newTab = {title: 'New tab ' + count, content: '<div>Broken</div>'};
$scope.tabs.push(newTab);
}
$scope.addElement = function() {
count++;
var newTab = {title: 'New tab ' + count, content: angular.element('<div>Broken</div>')};
$scope.tabs.push(newTab);
}
$scope.removeTab = function() {
$scope.tabs.pop();
}
});
Plunkr
It's already transcluded, but if you want to inject HTML, you'll have to use ngBindHtml (requires ngSanitize as a dependency).
Updated Plunker
Add angular-sanitize.js make sure ngSanitize is added as a dependency:
var app = angular.module('plunker', ['ui.bootstrap', 'ui.sortable', 'ngSanitize']);
Change your markup to add ng-bind-html:
<tab class="tab" ng-repeat="tab in tabs" sortable-tab heading="{{tab.title}}">
<div ng-bind-html="tab.content"></div>
</tab>
Then you can pass html directly:
{title: 'New tab ' + count, content: '<strong>Works with <span class="red">ngBindHtml</span></strong>'}
Or, using the angular.element html() method to grab the element object's html:
{title: 'New tab ' + count, content: angular.element('<div>Broken</div>').html()}

JQuery UI highlight effect color parameter ignored in Knockout foreach

Im trying to apply the JQuery UI highlight effect to an element when an item that is bound to a knockout observablearray is updated.
The highlight effect is applied but the highlight color used is always the elements current background color. even if I specify the highlight color using the { color: 'XXXXXXX' } option.
any ideas what might be happening?
Thanks,
Steve.
Code below: The element is the span.tag
<div class="row">
<div class="span12">
<div class="tagsinput favs span12" style="height: 100%;" data-bind="foreach: favs, visible: favs().length > 0">
<span class="tag" data-bind="css: $root.selectedFav() == userPrefID() ? 'selected-fav' : '', attr: { id: 'fav_' + userPrefID() }">
<span data-bind="text: name, click: $root.loadFav.bind($data)"></span>
<a class="tagsinput-fav-link"><i class="icon-trash" data-bind="click: $root.delFav.bind($data)"></i></a>
<a class="tagsinput-fav-link-two" data-bind="visible: $root.selectedFav() == userPrefID()"><i class="icon-save" data-bind=" click: $root.saveFav.bind($data)""></i></a>
</span>
</div>
</div>
</div>
// This is the code that does a save via ajax then highlights the element when done.
$.getJSON('#Url.Action("SaveFav","User")', { id: item.userPrefID(), fav: window.JSON.stringify(fav) }, function (result) {
var savedFav = ko.utils.arrayFirst(self.favs(), function (aFav) {
return aFav.userPrefID() == result.userPrefID; // <-- is this the desired fav?
});
// Fav found?
if (savedFav) {
// Update the fav!
savedFav.value(result.value);
}
}).done(function () {
var elementID = "#fav_" + item.userPrefID();
highlightElement(elementID);
});
// Function to highlight the element
function highlightElement(element) {
$(element).effect("highlight", {}, 1500);
}
I would do this the 'knockout' way... use a custom bindingHandler. You shouldn't be directly manipulating DOM in your viewModel, but only touching properties of your viewModel.
Taking this approach, you simply set a boolean value to true when your save is complete... this triggers the highlight effect (the jquery/dom manipulation neatly hidden away from your viewmodel) and when highlight effect completes, the handler sets the boolean back to false. Nice and tidy.
HTML:
<div id="#fav" data-bind="highlight: done">This is a test div</div>
<br />
<button data-bind="click: save">Simulate Save</button>
Javascript:
ko.bindingHandlers.highlight = {
update: function(element, valueAccessor) {
var obs = valueAccessor();
var val = ko.unwrap(obs);
if (val) {
$(element).effect("highlight", {}, 1500, function() {
obs(false);
});
}
}
};
var vm = function() {
var self = this;
self.done = ko.observable(false);
self.save = function() {
self.done(true);
};
}
ko.applyBindings(new vm());
Fiddle:
http://jsfiddle.net/brettwgreen/pd14q4f5/

Break a string and merge string in c# with show and hide options

My Requirement:
My string should be break after some words and at place we need to place "+more" option. when user click the "+more" need to show entire text. after end of the text need to show "-hide". when user click the "-hide" then it should be show previous. means some text with "+more" option. can any one help this.
my code:
var fullString= "string with above 150 charecters here";
var compressedString = TotalNews.fullString(150);
<div class="Temphide">
#compressedString
</div>
<a class="show" id="#newsItem.ApplicationNewsId">+More</a>
var Continues = fullString.Substring(150, TotalNews.Length - 150);
<div style="display:none;" >
#fullString <a class="hide">-Hide</a>
</div>
script:
<script type="text/javascript">
$(document).ready(function () {
$('.show').click(function () {
$(this).next('div').slideToggle();
});
$('.hide').click(function () {
$(this).parent().slideUp();
});
});
</script>
My problem:
Here when i click "+more" option i am showing "+more" with "-hide". requirement is when click "+more" need to show fullstring with "-hide" option. but i am doing showing "+more" and "-hide". please can any one help this.
Script:
$.fn.truncate = function(options) {
$(this).append('<span class="truncate_lh" style="display:none;"> </span>')
var defaults = {
maxlines: 2,
moreText: 'More',
lessText: 'Less',
ellipsis: '...'
};
$.extend(options, {
lineheight: ($('.truncate_lh').css('height').replace('px', ''))
});
$.extend(options, {
maxheight: (options.maxlines * options.lineheight)
});
options = $.extend(defaults, options);
return this.each(function() {
var text = $(this);
if (text.height() > options.maxheight) {
text.css({
'overflow': 'hidden',
'height': options.maxheight + 'px'
});
var link = $('' + options.moreText + '');
var wrapDiv = $('<div class="truncate_wrap" />');
text.wrap(wrapDiv);
text.after(link);
link.click(function() {
if (link.text() == options.moreText) {
link.text(options.lessText);
text.css('height', 'auto');
} else {
link.text(options.moreText);
text.css('height', options.maxheight + 'px');
}
return false;
});
}
});
};
$().ready(function() {
$('.truncate').truncate( {
maxlines: 4
});
});
View
<p class="truncate">//Here text...</p>
I have done this with help of
http://jsfiddle.net/Pjgzq/1/
visit this link

Direct URL to overlay on the page

I have an overlay on my page and I would like to give user a link / URL which leads them directly to the page, with the overlay loaded.
Is it possible to be done? How do I do that?
This is my code:
$(function() {
$('#overlay_job').click(function() {
document.getElementById('iframe_job').src = "http://targeturl-for-my-overlay-content";
$('#overlay_bg2').fadeIn('fast',function(){
$('#overlay_box').fadeIn('fast');
});
});
$('#boxclose').click(function() {
$('#overlay_box').fadeOut('fast',function() {
$('#overlay_bg2').fadeOut('fast');
});
});
var overlay_bg2 = $("#overlay_box");
//var top = $(window).scrollTop() - (overlay_bg2.outerHeight() / 2);
var top=0;
var left = -(overlay_bg2.outerWidth() / 2);
overlay_bg2.css({ 'margin-top': top,'margin-left': left });
if(getUrlVars()["openJobOverLay"] == "Y") {
$('#overlay_job').trigger('click');
}
});
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
function(m,key,value) {
vars[key] = value;
});
return vars;
}
===========
and on the page, user can click on this link to get the overlay shown:
<a id="overlay_job" class="underline" href="javascript:void(0);%20opener.jobframe.location.reload();">
See job openings
</a>
<img src="link_arrow.gif" class="linkarrow">
</div>
Hide the overlay with CSS: display: none;. Give the overlay element an id, then add the id to the URL like so http://www.yourpage.com#id. Then use jQuery to do something like this:
$(document).ready(function(){
if(window.location.hash) $('#' + window.location.hash).show();
});
Edit:
In your specific case add this line as the last line inside your $(document.ready();.
$(function() {
// All of your code inside this function...
if(window.location.hash) $('#' + window.location.hash).click();
});
function getUrlVars() {
// Some more of your code...

using iscroll with jquerymobile

I am building an app using jquerymobile in phonegap. I am using he following code to achieve fixed header, footer, scrollable content using iscroll.js. The problem is that I am unable to scroll the content div. Pls help me.
enter code here<body onload="loaded()">
<div data-role="page" id="detail">
<div class="fixedHeader">
</div>
<div id="wrapper" >
<div id="scroll-content">
<div data-role="content">
<!-- dynamic content goes here -->
dynamic content goes here
</div>
</div>
</div>
<div class="fixedFooter" ></div>
</div>
#wrapper {
position:absolute; z-index:1;
top:45px; bottom:48px; left:0;
width:100%;
overflow:auto;
}
#scroller {
position:relative;
/* -webkit-touch-callout:none;*/
float:left;
width:100%;
padding:0;
}
Javascript code
var myScroll;
function loaded() {
myScroll = new iScroll('wrapper', {
onBeforeScrollStart: function (e) {
var target = e.target;
while (target.nodeType != 1) target = target.parentNode;
if (target.tagName != 'SELECT' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA')
e.preventDefault();
}
});
}
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', loaded, false);
i got iscroll working in jquerymobile by editing js as
var myScroll = [];
$(document).delegate('[data-role="page"]', 'pageshow', function () {
var $page = $(this);
// setup iScroll
$($page.find('#wrapper')).each(function(index) {
var scroller_id = $(this).get(0);
myScroll.push(
new iScroll(scroller_id, {
useTransform: false,
onBeforeScrollStart: function (e) {
var target = e.target;
while (target.nodeType != 1) target = target.parentNode;
if (target.tagName != 'SELECT'&& target.tagName !='option'&& target.tagName !='option' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA')
e.preventDefault();
e.stopPropagation();
}
}));
});
});
$(document).delegate('[data-role="page"]', 'pagehide', function () {
// unset and delete iScroll
for (x in myScroll)
{
myScroll[x].destroy();
myScroll[x] = null;
myScroll.splice(x, 1);
}
});
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

Resources