Highlighting a field in angularJS if different value is present after comparison - html

I have rows of data and each row has 2 columns.
<div class="row">
<div class="col-md-2"><strong>Natural ID:</strong></div>
<div class="col-md-4">{{ctrl.firstobject.naturalId}}</div>
<div class="col-md-2"><strong>ManulaID:</strong></div>
<div class="col-md-4">{{ctrl.firstobject.manualID}}</div>
</div>
Now, I would like to compare the value of the field called "NaturalID" with another Natural ID (accessed by ctrl.CustomObject.naturalid) and if these two values are different, I would like to highlight in red the field corresponding to Natural ID in the above HTML. How can I do this?

You can use ng-class something like
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl',function($scope, $timeout) {
$scope.firstobject={
naturalId:1,
manualID:2
}
$scope.customObj={
naturalId:2
}
});
.red{
color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl" >
<div class="row">
<div class="col-md-2"><strong>Natural ID:</strong>
</div>
<div class="col-md-4" ng-class="{'red':firstobject.naturalId !=customObj.naturalId}">{{firstobject.naturalId}}<span ng-if="firstobject.naturalId !=customObj.naturalId" class="glyphicon glyphicon-alert"></span>
</div>
<div class="col-md-2"><strong>ManulaID:</strong>
</div>
<div class="col-md-4">{{firstobject.manualID}}</div>
</div>
</div>

Related

JQuery for multiple div id's that are the same

I have multiple divs with id='title'.
When that div is hovered over I want a hidden div with id="source" to appear.
I am using the following code but only the first div with id='title' on the page works.
<!-- Show Source / Date on mouse hover -->
<script>
$(document).ready(function(){
$("#title").mouseenter(function(){
$("#source").fadeIn( 200 );
});
$("#title").mouseleave(function(){
$("#source").fadeOut( 200 );
});
});
</script>
HTML Example:
<div id="title" class="col-md-3">
<div id="source" style="display:none">Hidden Text</div>
</div>
<div id="title" class="col-md-3">
<div id="source" style="display:none">Hidden Text</div>
</div>
<div id="title" class="col-md-3">
<div id="source" style="display:none">Hidden Text</div>
</div>
Use classes instead of IDs
use .find() to search for descendants of an element
jQuery(function($) { // DOM ready and $ alias in scope
$(".title").on({
mouseenter() {
$(this).find(".source").fadeIn(200);
},
mouseleave() {
$(this).find(".source").fadeOut(200);
}
});
});
.title {
display: flex;
}
/* Utility classe */
.u-none {
display: none;
}
<div class="title col-md-3">
TITLE 1
<div class="source u-none">Visible Text</div>
</div>
<div class="title col-md-3">
TITLE 2
<div class="source u-none">Hidden Text</div>
</div>
<div class="title col-md-3">
TITLE 3
<div class="source u-none">Hidden Text</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
Hello Δce Let's start by modifying the source code to use classes for you to pick. This helps us describe the functionality and allow the ID's to serve their purpose.
we'll take your
<div class="col-md-3 show-source-on-hover">
<div class="source" style="display:none">Hidden Text</div>
</div>
<div class="col-md-3 show-source-on-hover">
<div class="source" style="display:none">Hidden Text</div>
</div>
<div class="col-md-3 show-source-on-hover">
<div class="source" style="display:none">Hidden Text</div>
</div>
and we can update the jQuery code
<script>
$(function() {
$(".show-source-on-hover")
.mouseenter(function() {
$(this).find('.source').fadeIn( 200 );
})
.mouseleave(function() {
$(this).find('.source').fadeOut( 200 );
});
});
</script>
You can see here there's the use of this wrapped in the $() jquery object function. and the new use of .find to get get the correct source class.
For a working demo please see the following link: https://codepen.io/jessycormier/pen/GRmaaEb
Note: your use of ID's should change and always try and be unique like others have suggested. MDN (Mozilla Developer Network) has some great documentation on the standards of HTML, CSS, and JavaScript which can help give you.
Also, in the demo I've given your column divs some default size otherwise there is nothing to trigger the mouse over events. Good luck and happy coding 🚀
IDs are IDENTIFIERS. What that means is that there can only be one per document. Classes are not unique, so you can use them.
✅ $(".title")
❌ $("#title")

Vuejs background image not work

Hello guys,
I have a problem with back slash in background-image style in Vuejs and string replace in HTML not work and solution.
URL from inspecting element
http://localhost:8000/storage/banar-pages\July2018\WPNrFE6eXopKnjMqjNgW.jpg
From DB
banar-pages\July2018\WPNrFE6eXopKnjMqjNgW.jpg
Code from component file
<div class="block-entry fixed-background" :style="'background-image: url(' +link + '/storage/' + about.image +');'">
<div class="container">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<div class="cell-view simple-banner-height text-center">
<div class="empty-space col-xs-b35 col-sm-b70"></div>
<h1 class="h1 light">{{ about.name }}</h1>
<div class="title-underline center"><span></span></div>
<div class="simple-article light transparent size-4">{{ about.details }}</div>
<div class="empty-space col-xs-b35 col-sm-b70"></div>
</div>
</div>
</div>
</div>
</div>
Thank you
I need to replace image link it but flage not work image.replace('/\\/g', '/')
It looks like you're incorrectly passing a string as the first argument to String#replace(), which would result in a literal replacement (i.e., it would replace the first occurrence of /\/g in the string):
console.log('XX/\\/gXX/\\/gXX'.replace('/\\/g', '/'))
Remove the quotes from the first argument to make it a regular expression:
console.log('\\path\\to\\foo.png'.replace(/\\/g, '/'))
Then, your Vue template could be similar to this:
<div :style="'background-image: url(' +link.replace(/\\/g, '/') + '/storage/' + about.image.replace(/\\/g, '/') +');'">
new Vue({
el: '#app',
data() {
return {
link: 'http:\\placekitten.com',
about: {
image: '\\100\\100'
}
}
}
})
.dummy {
width: 100px;
height: 100px;
}
<script src="https://unpkg.com/vue#2.5.16"></script>
<div id="app">
<div class="dummy"
:style="`background-image: url(${link.replace(/\\/g, '/')}${about.image.replace(/\\/g, '/')})`">
</div>
</div>
You could have a method for processing that string and returning a revised URL:
methods: {
prepareURL(string) {
return string.replace('/\\/g', '/');
}
}
Component code:
<div class="block-entry fixed-background" :style="'background-image: url(' +link + '/storage/' + prepareURL(about.image) +');'">
<div class="container">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<div class="cell-view simple-banner-height text-center">
<div class="empty-space col-xs-b35 col-sm-b70"></div>
<h1 class="h1 light">{{ about.name }}</h1>
<div class="title-underline center"><span></span></div>
<div class="simple-article light transparent size-4">{{ about.details }}</div>
<div class="empty-space col-xs-b35 col-sm-b70"></div>
</div>
</div>
</div>
</div>

Multiple selection from grid in ionic framework

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

AngularJS Filter is not working as expected

Right side Bajra atta, Besan, Corn Flour .. are subcategories.
Index.html
<body>
<div class='container'>
<div class='row'>
<div class='col-md-4'></div>
<!-- Serach Area -->
<div class='col-md-5'>
<input style="margin-top:5px;" type="text" ng-model="SearchText" placeholder="Search for more than 10,000 products..." class='form-control' size="50">
</div>
<div class='col-md-1'></div>
<div class='col-md-2'>
<a id="top_login" href="#Login" style="position:absolute; margin-top:15px; color:#FFFCBA">
<strong>LOGIN </strong>
</a>
</div>
</div>
</div>
<div ng-controller="MainCtrl">
<div class='container'>
<div class='row'>
<div class='col-md-2 Name'>
<!-- Super Categories-->
</div>
<div class='col-md-7'> </div>
<div class='col-md-2'>
<!--SC Cart-->
</div>
<div class='col-md-1'></div>
</div>
</div>
<div id="middle" class='container'>
<div class='row'>
<div id="left" class="col-md-2">
<!-- categoris and sub categories -->
</div>
<div id="right" class="col-md-6">
<div ng-view></div>
</div>
</div>
</div>
</div>
<div id="footer">
<!-- Footer -->
</div>
</body>
products.html
<div class='box' ng-repeat="product in ProductService.Products | filter:SearchText | filter:FilterExpr:true| orderBy:'ProductName'">
<!-- Display filtered Products -->
</div>
ProductsController
sampleApp.controller('ProductsController', function ($scope, $routeParams, $log, $rootScope, ProductService) {
$scope.SCId = $routeParams.SCId;
$scope.ChangeFilter = function() {
if ($routeParams.SCId) {
$scope.FilterExpr = {'SubCategoryID': $routeParams.SCId.toString()};
}
};
$scope.ChangeFilter();
});
config Code
sampleApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/Login', {
templateUrl: 'templates/ShowLogin.html',
controller: 'LoginController'
}).
when('/ShoppingCart', {
templateUrl: 'templates/ShoppingCart.html',
controller: 'ShoppingCartController'
}).
when('/Products/:SCId', {
templateUrl: 'templates/Products.html',
controller: 'ProductsController'
}).
otherwise({
redirectTo: '/Products/38'
});
}]);
When user clicks on subcategory, products of that subcategory is listed.
As otherwise is redirectTo: '/Products/38' products of subcategory 38 is listing initially.
I'm facing the following problems:
when user start typing in search box, filter:SerachText is applied on displayed subcategory products. But at this moment i want filter:SerachText to be apply on all the products (Not only on selected subcategory products)
Filter is not applying, if we are in some other routed page say login page. I want as soon as user start typing in search Box products.html and productsController to be activate.
When user has inputted something in search Box, and clicks on sub category, not all the products of that subcategory is listing because filter:SerachText is applied here. In this case i dont want to apply filter:SerachText filter
Can please some one help me to fix all these issues. I've tried all the combinations and permutations, but no luck. I'm also Ok if any architecture change is required that can make the things better.
Have you ever tried to hoist the SearchText filter scope to $parent.SearchText??
I guess, a superior scope ($parent or $rootScope) in your filter could work.
BTW: It works for me, 'cause it seems that ng-include does a local scope for each HTML to render, even in the same page.
P.D: Sorry for my English, greetings from Venezuela :D

Is there a way to dynamically wrap two elements into a div IF they exist?

I have an Angular application that renders spans based on whether they're needed or not (many ng-if's). I need to wrap these spans in a div based on their content/class names.
So, for example:
<div class="1"></div>
<div class="2"></div>
<div class="3"></div>
I don't want to do anything. But with
<div class="1"></div>
<div class="2"></div>
<div class="3"></div>
<div class="4"></div>
I DO want to wrap these 4 divs in a parent div <div class="parent"></div>, but only if the four appear one after the other. Is there a way to do this in CSS? Can I just use a combo of selectors to manipulate the four elements if they appear consecutively?
so you cant do this using just css and html, like the comments above, you need to use some form of javascript to manipulate the dom. heres an example using jQuery. hope it helps!
$(document).ready(function() {
$('.wrapme').each(function() {
var myElement = $(this);
var next1 = $(this).next();
var next2 = $(this).next().next();
var next3 = $(this).next().next().next();
if (next1.hasClass('wrapme') && next2.hasClass('wrapme') && next3.hasClass('wrapme')) {
var html = '<div class="parent"></div>';
next3.after(html);
var parent = next3.next('.parent')
parent.append(myElement);
parent.append(next1);
parent.append(next2);
parent.append(next3);
}
});
});
.parent {
background-color:lightgray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapme">1</div>
<div class="wrapme">2</div>
<div class="wrapme">3</div>
<span>BREAKKKKKK</span>
<div class="wrapme">1</div>
<div class="wrapme">2</div>
<div class="wrapme">3</div>
<div class="wrapme">4</div>
<span>BREAKKKKKK</span>
<div class="wrapme">1</div>
<div class="wrapme">2<div>
<span>BREAKKKKKK</span>
<div class="wrapme">1</div>
<div class="wrapme">2</div>
<div class="wrapme">3</div>
<div class="wrapme">4</div>
<span>BREAKKKKKK</span>
<span>BREAKKKKKK</span>
<div class="wrapme">1</div>
<div class="wrapme">2</div>
hey hey
<div class="wrapme">3</div>
<div class="wrapme">4</div>
<span>BREAKKKKKK</span>