I am trying to remove item using ng-if and ng-else-if.
"ng-if="navItem.gbn.indexOf('12345') !== -1" is true then array item only show contains('12345') items.
but the item still remain in array so I think ng-else-if is not working.
I don't know how to delete it.
I have the following Angular code:
<div ng-controller="PIDAEController">
<div class="container-fluid" style="padding-top:30px;">
<div class="row">
<div class="col-lg-2 col-md-3 col-xs-6 tubmbimg" ng-repeat="navItem in navItems | Fpage: curPage * pageSize | limitTo:pageSize" ng-if="navItem.gbn.indexOf('12345') !== -1">
<a class="thumbnail border" href="#/item/{{navItem.site}}">
<div class="b-marjin">
<span align="center" class="label label-primary full-width" >{{ navItem.gbn }}</span>
</div>
<div>
<img style="border: 1px solid black;" class="img-responsive full-width" src="{{navItem.pimg}}" alt="">
</div>
<div align="center" class="under-block">
<span title="{{ navItem.pname }}" align="center">{{ navItem.pname2}}</span>
</div>
</a>
</div>
<div ng-else-if="navItems.splice(navItem, 1)"></div>
</div>
</div>
</div>
angular.forEach(item, function (value, key) {
var keepGoing = true;
if(keepGoing) {
if(value.gbn.indexOf("11600") != -1) {
$scope.initialized = true;
$scope.navItems = item;
$scope.curPage=0;
$scope.pageSize=20;
$scope.numberOfPage = Math.ceil(item.length / $scope.pageSize);
keepGoing = false;
}
else {
$scope.navItems.splice(key, 1);
}
}
});
ng-else-if does NOT EXIST. its neither angular or angularJS.
In the line
<div ng-else-if="navItems.splice(navItem, 1)"></div>
splice requires navItem to be an index/int, not an object.
As an alternative to ng-else-if you can simply do:
ng-if="navItem.gbn.indexOf('12345') == -1
Also, splice would return the removed array item when found, not sure if "ng-if" handles that as being TRUE.
Related
In my project I want to show images when I click the button. How I can configure the method openModal() on (click) to do this?
The IMG are taken from assets directory, and change based on your code (B1, B2) choice.
<!-- OPERATION -->
<div class="card my-5">
<div class="card-header" style="text-align: center; font-size: 20px;">
<p><strong>Operation</strong></p>
</div>
<div class="card-body">
<form>
<div class="form-group">
<tr *ngFor="let arr of varOperation; index as i">
<td>
Code: {{arr.operation.code}}
- {{arr.operation.description}}
</td>
<button style="margin-left: 20px; float: right;" class="btn btn-primary" (click)="viewCodeOperation(arr.operation.code)" (click)="openModal()">
Go
</button>
</tr>
</div>
</form>
</div>
</div>
<!-- IMAGES -->
<div class="overflow-auto">
<div *ngIf="imgB1">
<img src="assets\images\B1.jpg">
</div>
<div *ngIf="imgB2">
<img src="assets\images\B2.jpg">
</div>
</div>
viewCodeOperation(code: string) {
this.imgB1 = false
this.imgB2 = false
console.log(code)
if(code == "B1"){
this.imgB1 = true
}
else if(code == "B2"){
this.imgB2 = true
}
}
openModal() {
}
You can try using a library like this https://github.com/maximelafarie/ngx-smart-modal one to set up modals in angular.
I'm trying to create rows dynamically with razor but I can't figure out how to wrap this around. What I want is for every 5:e object in my model I want to create a new row/div <div class="row pics_in_a_row"> so that each row contains five or less images.
<section class="slice">
<div class="wp-section">
<div class="container">
<div class="row pics_in_a_row">
#{
int i = 0;
foreach (dbphoto.Models.Image s in Model.images.OrderByDescending(x => x.idimage))
{
if (i % 5 == 0 && i != 0)
{
<br />
}
i++;
if (1 == 1)
{
<div class="flxbox" style="flex: calc(1024/713)">
<a href="#s.HighResolution" data-fancybox="gallery" data-caption="xyz, Stockholm">
<img src="#s.LowResolution" class="img-fluid rounded flximg" />
</a>
</div>
}
}
}
</div>
</div>
</div>
</section>
Bonus question: how can I get the height and width of the image and add it to the calc() css?
You should add the div with class row pics_in_a_row inside your loop
This should create a a row for every 5 items (or less for the last row when the remaining items are less than 5).
<div class="container">
#{
var numberOfColsNeeded = 5;
var totalCounter = Model.images.Count();
var itemCounter = 1;
}
#foreach (var item in Model.images)
{
if (itemCounter % numberOfColsNeeded == 1)
{
#:<div class="row pics_in_a_row">
}
<div class="flxbox" style="flex: calc(1024 / 713)">
<img src="#item.LowResolution" class="img-fluid rounded flximg" />
</div>
if ((itemCounter % numberOfColsNeeded == 0) || ((itemCounter) == totalCounter))
{
#:</div>
}
itemCounter++;
}
</div>
I want to make a grid of categories images, select multiple categories from them and send their values to controller. I don't know how to achieve this with ionic framework.
Here is my view:
<ion-view cache-view="false">
<div class="bar login-bar bar-header bar-stable">
<h1 class="title ttl-log-bar">Welcome</h1>
</div>
<div class="bar bar-subheader welcome-subhead">
<h2 class="title welc-sub-h2">What do you like to shop for?</h2>
<h5 class="title welc-sub-h5">Pick at least one category</h5>
</div>
<ion-content scroll="true" class="has-header has-subheader">
<div class="row row-cat" style="flex-wrap: wrap;">
<div class="col col-cat col-50" ng-repeat="items in categoryList">
<img ng-src="{{items.image}}" width="100%" />
</div>
</div>
</ion-content>
</ion-view>
Add on img tag ng-click with function passing e.g. ID like this:
<img ng-src="{{items.image}}" ng-click="selectCategory(items.id)" />
Then in controller define this function
$scope.selectedCategory = [];
$scope.selectCategory = function(id){
var index = $scope.selectedCategory.indexOf(id);
if(index === -1){
$scope.selectedCategory.push(id);
}else{
$scope.selectedCategory.splice(index, 1);
}
}
Then you can us selectedCategory array as a collection of selected item's IDs
I am building a product grid built upon AngularJS data - where there will be images and product details (text)
The text sometimes extends to the 2nd line, causing havoc.
Here is my code:
<div class="row">
<div data-ng-repeat="s in Results">
<div class="col-xs-4">
<a href="#" class="thumbnail">
<div>
<img ng-src="{{s.ProductImage}}" class="img-responsive">
</div>
<div>
{{s.Store}} {{s.Category}} {{s.ProductName}}
</div>
</a>
</div>
<div class="clearfix visible-xs-block"></div>
</div>
</div>
This is what it looks like:
How do I fix it so that <div>s all have the same height?
I tried to look online for solutions, but I think I am 50% there. Please help.
Note: I Don't want to hide content.
This is what I ended up doing for anyone who stumbles on this again in the future.
Javascript
function ResizeToLargestElement(element) {
var maxHeight = -1;
if ($(element).length > 0) {
$(element).each(function () {
maxHeight = maxHeight > $(this).height() ? maxHeight : $(this).height();
});
$(element).each(function () {
$(this).height(maxHeight);
});
}
}
Without AngularJS
For those who aren't using AngularJS, just call ResizeToLargestElement() when data changes or the window is resized using
$(window).resize(function() {
ResizeToLargestElement('.AutoResizeToLargestElement');
});`
With AngularJS
The idea is to call the ResizeToLargestElement() function whenever $scope.Results changes or when the window resizes.
To know when $scope.Results changed is easy, but to know when elements (that are bound to it) finished rendering is not easy. To do that, you need a AngularJS directive.
To know when the window re-sizes, use angular.element($window).on('resize', function () {...});
HTML
<div class="row">
<div data-ng-repeat="s in Results" data-ng-repeat-finished> <!--ADDED Directive-->
<div class="col-xs-4">
<a href="#" class="thumbnail AutoResizeToLargestElement"> <!--ADDED Class-->
<div>
<img ng-src="{{s.ProductImage}}" class="img-responsive">
</div>
<div>
{{s.Store}} {{s.Category}} {{s.ProductName}}
</div>
</a>
</div>
<!--REMOVED clearfix-->
</div>
</div>
MyDirectives.js
angular.module('myApp').directive('ngRepeatFinished', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element, attr) {
if (scope.$last === true) {
$timeout(function () {
scope.$emit('ngRepeatFinished');
});
}
}
}
});
mycontroller.js
$scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
ResizeToLargestElement(".AutoResizeToLargestElement");
});
angular.element($window).on('resize', function () {
ResizeToLargestElement(".AutoResizeToLargestElement");
});
Note: this requires you to include $window in the AngularJS dependency list.
I.E. angular.module('myApp').controller('....', ['...., '$window', ....]);
If you want to keep the height of each product dynamic, you will need to split the results into columns. And then use ng-if to put the right items in the right column. Every 3rd item will go into the same column. To set them to different columns, just reduce the $index by 1 for each extra column.
<div class="row">
<div class="col-xs-4">
<div ng-repeat="s in Results"> <a href="#" class="thumbnail" ng-if="$index%3==0">
<div>
<img ng-src="{{s.ProductImage}}" class="img-responsive" />
</div>
<div>{{s.Store}} {{s.Category}} {{s.ProductName}}</div>
</a>
<div class="clearfix visible-xs-block"></div>
</div>
</div>
<div class="col-xs-4">
<div ng-repeat="s in Results"> <a href="#" class="thumbnail" ng-if="($index-1)%3==0">
<div>
<img ng-src="{{s.ProductImage}}" class="img-responsive" />
</div>
<div>{{s.Store}} {{s.Category}} {{s.ProductName}}</div>
</a>
<div class="clearfix visible-xs-block"></div>
</div>
</div>
<div class="col-xs-4">
<div ng-repeat="s in Results"> <a href="#" class="thumbnail" ng-if="($index-2)%3==0">
<div>
<img ng-src="{{s.ProductImage}}" class="img-responsive" />
</div>
<div>{{s.Store}} {{s.Category}} {{s.ProductName}}</div>
</a>
<div class="clearfix visible-xs-block"></div>
</div>
</div>
</div>
You can manually restrict the height of the div and use an overflow. Use something like
<div class="col-xs-4" style="height:200px; overflow: auto;">Content</div>
Or, you could always use a responsive table (<table class="table table-responsive">) for your layout.
The best solution for this problem is to add clearfix class to hidden divs for earch size of viewport. for example:
Large size: i want 6 columns (large desktops)
Medium size: i want 4 columns (Desktops)
Tablets: i want 3 coumns
phones: i want 2 columns
so i have the products list:
<?php $count=1; foreach($productList as $product) : ?>
<div class="item col-xs-6 col-sm-4 col-md-3 col-lg-2">
Picture and description
</div><!-- /item -->
<?php if( ($count%2)==0) {?><div class="clearfix visible-xs-block"></div><?php }?>
<?php if( ($count%3)==0) {?><div class="clearfix visible-sm-block"></div><?php }?>
<?php if( ($count%4)==0) {?><div class="clearfix visible-md-block"></div><?php }?>
<?php if( ($count%6)==0) {?><div class="clearfix visible-lg-block"></div><?php }?>
<?php $count++; endforeach?>
I have the below div tag which contains a dropdown and a carousel. Whenever I select from the dropdown I call a function in an AngularJS controller which calls golang at the backend using http.golang to generate the data to be displayed in the carousel and sends a response which is displayed in the carousel but creates a page refresh. I just want this div to be loaded again and not the entire page to load. How can I do this using AngularJS?
HTML:
<div class="col-xs-11" style="width:750pt; height:180pt; margin-top:30px; ">
<div class="row " style="margin-left:75pt;">
<div class="dropdown">
<label class="text-center" style="margin-top:10pt;font-size:14pt;"> Deals on</label>
<button data-toggle="dropdown" class=" btn dropdown-toggle dropdown-menu-hover-color text-center" style="padding:0px 0px;background-color:white;" href="#"><label style="font-size:14pt; color: #191970;" >{{currentCategory}}</label> <b class="caret"></b></button>
<ul class="dropdown-menu submenu-hover-color dropdown-menu-scroll" style="margin:0;">
<li>{{page.category_name}}</li>
</ul>
<div style="display: inline;" ng-hide="deals"> <label class="text-center" style="margin-top:10pt;font-size:14pt;color: #191970;;">No deals found, please select a different category. </label></div>
</div>
</div>
<div id ="carousel_category" class="carousel slide " ng-show ="deals">
<div class="carousel-inner text-center" style="margin-left:75px;">
<div class="item active">
<div class="carousel-width" ng-repeat="(key, value) in categoryList.slice(0, 5) track by $index">
<img ng-src= {{value.imageUrl}} alt="No Image"/>
<h6>{{value.productName}}</h6>
<b style="color:#990000;">{{value.price}}</b> <b style="color:green;">{{value.discount}}</b>
</div>
</div>
<div class="item" ng-repeat="page in category_products_pages">
<div class="carousel-width" ng-repeat="image in page">
<img ng-src= {{image.imageUrl}} alt="No Image" />
<h6>{{image.productName}}</h6>
<b style="color:#990000;">{{image.price}}</b> <b style="color:green;">{{image.discount}}</b>
</div>
</div>
</div> <!--End of carousel-inner-->
<a class="left carousel-control pull-left" href="#carousel_category" data-slide="prev"><i class="glyphicon glyphicon-chevron-left"></i></a>
<a class="right carousel-control pull-right" href="#carousel_category" data-slide="next"><i class="glyphicon glyphicon-chevron-right"></i></a>
</div><!--End of carousel_cartegory-->
</div>
Angularjs :(Below code is inside angularjs controller)
$scope.changeCategory = function(category_id) {
$http({
method : 'POST',
url : '/changeCategory',
data : {"category_id" : category_id},
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
// set the headers so angular passing info as form data (not request payload)
}).success(function(data, status, headers, config) {
$scope.deals = true;
$scope.products = JSON.parse(JSON.stringify(data));
$scope.currentCategory = $scope.products.categoryInfo.category_name;
$scope.dropdownCategoryList = $scope.products.dropdownCategoryList;
$scope.categoryList = $scope.products.categoryList;
if($scope.categoryList == undefined) {
$scope.deals = false;
return;
}
currList = $scope.categoryList;
for(var i=0;i<currList.length;i++){
product_type = currList[i].product_type;
if (product_type != "Price_With_Discount") {
currList[i].price = "";
currList[i].discount = "Save Upto "+currList[i].discount + "%";
} else {
currList[i].price = "$" + currList[i].price;
currList[i].discount = "(Save "+currList[i].discount+"%)"
}
}
//category_data
var category_products_page = [];
var category_products_pages = [];
var count_1 = 5;
$scope.category_products_pages = category_products_pages;
for(var j=5;j<$scope.categoryList.length;j++){
if(count_1 > 1){
category_products_page.push($scope.categoryList[j]);
count_1--;
if($scope.categoryList.length === j+1){
category_products_pages.push(category_products_page);
//return;
break;
}
}else{
category_products_page.push($scope.categoryList[j]);
category_products_pages.push(category_products_page);
category_products_page = [];
count_1 = 5;
}
}
}).error(function(data, status, headers, config) {
$scope.status = status;
$window.alert("error")
});
}
});