How can I change icon class based the value of a field - javascript

How can I change a icon class based on a field value? I have a field name called flag, this field contains two value On and Off.
How can i change icon class based on the value:
When flag="On" show this icon fa-check-circle green
When flag is off show k-i-close-outline
<ul id="treeview" >
#foreach (var item in rpInfo)
{
<li data-expanded="true" class="panel-handler" data-id="#item.ID">
<i class="far fa-check-circle" data-id="#item.Flag" style="padding-right: 10px;"></i>#item.CompanyName
<ul>
<li data-expanded="true">
<i class="far fa-check-circle"></i>#item.flag #item.ContactName
</li>
</ul>
</li>
}
</ul>
To make it easier i added data-id as and in browser looks like this <i class="far fa-check-circle" data-id="Off" style="padding-right: 10px;"></i>.
may be if that helps
Thank you!

I'm having trouble to understand what you do and what you need but it sounds like you need a simple if statement.
Maybe something along the lines?
<ul id="treeview" >
#foreach (var item in rpInfo)
{
var flag = #item.Flag;
if (flag = 'On') { '
<li data-expanded="true" class="panel-handler" data-id="#item.ID">
<i class="far fa-check-circle" data-id="#item.Flag" style="padding-right: 10px;"></i>#item.CompanyName
<ul>
<li data-expanded="true">
<i class="far fa-check-circle"></i>#item.flag #item.ContactName
</li>
</ul>
</li>
'} else{ '
<li data-expanded="true" class="panel-handler" data-id="#item.ID">
<i class="k-icon k-i-close-outline" data-id="#item.Flag" style="padding-right: 10px;"></i>#item.CompanyName
<ul>
<li data-expanded="true">
<i class="k-icon k-i-close-outline"></i>#item.flag #item.ContactName
</li>
</ul>
</li>
'}
};
</ul>

Related

Angular ngIf condition breaking javascript logic

I have the following nested dropdown list. The child items are initially hidden and clicking on the parent toggles an "open" class to display the children.
I am trying to conditionally display the entire list with an ngIf, which works correctly. However, when my condition is true the open class toggle no longer works. Any reason why simply adding the ngIf condition breaks this functionality?
<li class="nav-item dropdown" *ngIf="isMHQEnabled"> //toggles correctly without the if condition
<a class="dropdown-toggle">
<i class="fal fa-exchange"></i>
<i class="fas fa-exchange"></i>
<span class="title">Broker Transfers</span>
<span class="arrow">
<i class="fas fa-chevron-down"></i>
</span>
</a>
<ul class="dropdown-menu sub-down">
<li class="nav-item dropdown">
<a class="dropdown-toggle" (click)="initiateBrokerTransfer()">
<i class="fal fa-message-dots"></i>
<i class="fas fa-message-dots"></i>
<span class="title">Transfer Request</span>
</a>
</li>
<li class="nav-item dropdown">
<a class="dropdown-toggle" (click)="navigate('brokertransfer')">
<i class="fal fa-list-check"></i>
<i class="fas fa-list-check"></i>
<span class="title">Transfer Status</span>
</a>
</li>
</ul>
</li>

listener on UL iterate through li

I have the following html:
<ul class="collection">
<li>a
<a class=" delete-item secondary-content">
<i class="fa fa-remove"></i>
</a>
</li>
<li>s
<a class=" delete-item secondary-content">
<i class="fa fa-remove"></i>
</a>
</li>
<li>w
<a class=" delete-item secondary-content">
<i class="fa fa-remove"></i>
</a>
</li>
</ul>
I have an onclick listener on the UL (called taskList). console.log says the element UL (taskList) is an object. Can I iterate through the li's as follows:
for (const task in taskList) {
console.log(task);
}
No, for ... in will loop over the properties of the object. You could use for ... of to loop over the children.
const taskList = document.querySelector('ul.collection');
for (const task of taskList.children) {
console.log(task.outerHTML);
}
<ul class="collection">
<li>a
<a class=" delete-item secondary-content">
<i class="fa fa-remove"></i>
</a>
</li>
<li>s
<a class=" delete-item secondary-content">
<i class="fa fa-remove"></i>
</a>
</li>
<li>w
<a class=" delete-item secondary-content">
<i class="fa fa-remove"></i>
</a>
</li>
</ul>

Add active class to li and parents through jquery

I'm a beginner to javascript, I've been trying to add the active class to a li using jquery i.e. When its clicked, expand li and make it active:
<script>
$(document).ready(function() {
var url = window.location['pathname'];
jQuery('li a').each(function() {
if($(this).attr('href') == url)
$(this).parent().addClass('active');
});
});
</script>
This only makes the sub-menu active but not expanded. But when I manually added the active class (as shown below) to treeview and li, it expanded and became active. How to dynamically add the active class through jquery?
<aside class="main-sidebar">
<section class="sidebar">
<ul class="sidebar-menu" data-widget="tree">
<li class="header">MENU</li>
<li class="treeview active">
<a href="#">
<i class="fa fa-table"></i> <span>USER</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<li><i class="fa fa-plus"></i> Add</li>
<li class="treeview active">
<a href="#"><i class="fa fa-circle-o"></i> Profile
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<li class="active"><i class="fa fa-circle-o"></i> View </li>
<li><i class="fa fa-circle-o"></i> Settings </li>
</ul>
</li>
</ul>
</li>
</ul>
</section>
</aside>
So, you need to apply active class on all .treeview using parents:
$(this).parents('.treeview').addClass('active');
Actually, changing parent() to parents() fixed it:
$(this).parents().addClass('active');

li elements act as an anchor in html

I have a code like below in my html
<ul class="nav">
<li class="no-bullet " onclick="window.location = 'ask_question.html';">
<button type="button" class="signout_btn line_height">
<i class="fa fa-home " aria-hidden="true"></i>
Home</button>
</li>
<li class="sub-menu"><a href="#">
<i class="fa fa-tags" aria-hidden="true"></i>
Search by Category</a>
<ul class="" id="loadCategories">
<li>Food</li>
<li>Sports</li>
<li>Personal</li>
</ul>
</li>
<li class="sub-menu"><a href="#">
<i class="fa fa-map-marker" aria-hidden="true"></i>
Switch City </a>
<ul class="" id="loadCities">
<li>Mumbai</li>
<li>Bangalore</li>
<li>Personal</li>
</ul>
</li>
<li class="no-bullet " onclick="window.location = 'user_profile.html';" >
<button type="button" class="settings_btn line_height">
<i class="fa fa-user" aria-hidden="true"></i>
My Profile</button>
</li>
<li class="no-bullet " onclick="window.location= 'all_users.html';">
<button type="button" class="signout_btn line_height" id="btnFindAFriend">
<i class="fa fa-users" aria-hidden="true"></i>
Find A Friend</button>
</li>
</ul>
If you notice the above code, I have li which on click I am directing to a certain html page. Inside the li I have put a button there in order to make all my menu option look uniform. In order to achieve that I did some make and break to make it look exact similar.
But I am unable to make all of them look exactly the same. I want to have the same height and size of all the menu option.
Can someone recommend a correct way to solve this.
Here's the fiddle for the same.

Search in menu & submenu with JQuery Searchable plugin - multilevel

I want to search in menu title and submenu, but in JQuery Searchable Plugin can't find this option. When I type characters, I can find menu but can't find submenu. Searchable plugin has childSelector option but this option is searching just the first level.
$('#menu-list').searchable({
searchField: '#menu-list-search',
selector: 'li',
childSelector: '.menu-text',
show: function( elem ) {
elem.slideDown(100);
},
hide: function( elem ) {
elem.slideUp( 100 );
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://raw.githubusercontent.com/stidges/jquery-searchable/master/dist/jquery.searchable-1.0.0.min.js"></script>
<input id="menu-list-search" placeholder="Search Menu...">
<ul id="menu-list" class="nav nav-list">
<li class="">
<a href="index.html">
<i class="menu-icon fa fa-tachometer"></i>
<span class="menu-text"> menu1 </span>
</a>
<b class="arrow"></b>
</li>
<li class="">
<a href="#" class="dropdown-toggle">
<i class="menu-icon fa fa-list"></i>
<span class="menu-text"> menu2 </span>
<b class="arrow fa fa-angle-down"></b>
</a>
<b class="arrow"></b>
<ul class="submenu">
<li class="">
<a href="tables.html">
<i class="menu-icon fa fa-caret-right"></i>
submenu1
</a>
<b class="arrow"></b>
</li>
<li class="">
<a href="jqgrid.html">
<i class="menu-icon fa fa-caret-right"></i>
submenu2
</a>
<b class="arrow"></b>
</li>
</ul>
</li>
</ul>
Changes In HTML
Wrap all text (including submenu items) with
<span>text_here</span>
Changes in JS
and simply change the value of childSelector to 'span'
...
...
childSelector: 'span',
...
...
My problem solved with wrap all text in <span class="menu-text"> menu1 </span>

Categories

Resources