wrapping elements in a new div - jquery-selectors

I have the below html:
<div id="one">
<div class="two">
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
</div>
in jQuery if I am to wrap every 3 elemnts in a new div like below how could I do that?
<div id="one">
<div class="two">
<div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
<div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
</div>
</div>
I tried slice and wrap which did not really help me. Any ideas? appreciate your help on this.
Thanks,
L

var entries = $('#one > .two > div.entry');
entries.each(function(i) {
if( i % 3 == 0 ) entries.slice(i,i+3).wrapAll('<div>');
});
http://jsfiddle.net/PZkSL/

Refer to this How to wrap every 3 child divs with html using jquery? or Wrap every 3 divs in a div

How about getting the div element with class "two", and iterate through it's children. When you find the first child use $(firstChild).before('<div>'); and on the third child use $(thirdChild).after('</div>'); Just iterate all the way through using this method. Haven't used .before() or .after() so not sure how this will pan out. Good luck.

I would just write a simple for loop, something like:
var two = $(".two");
var elements = $(".entry");
var group = [];
for(var i=0;i<elements.length;i++) {
var mod = i % 3;
if(mod == 0 && group.length > 0) {
var container = $('<div></div>');
two.append(container);
for(var j=0;j<group.length;j++) {
container[0].appendChild(group[j]);
}
group = [];
}
group[mod] = elements[i];
}

Related

Button behaving globally and not for each item in Salesforce LWC

I am quite new to Lighting Web Component. However, I have some experience with other component base frameworks.
I'm creating a button for each item and when the button is clicked for each item. It applies to all items. In other words, when one clicks 'Extend' it is meant to show a date input for that specific item but it shows too all of them. This is how my code looks like:
<template>
<template if:true={subscriptionData}>
<template for:each={subscriptionData} for:item="subscriptionItem">
<div key={subscriptionItem.Id}>
<div>
<div class="slds-grid slds-gutters slds-grid_vertical-align-center">
<div class="slds-col custom-card-title-container">
<h2 class="custom-title">{subscriptionItem.SBQQ__ProductName__c}</h2>
</div>
<div class="slds-col button-custom">
<lightning-button label="Extend" title="Non-primary action" onclick={handleShowEditEndDate} class="slds-m-right_none custom-margin"></lightning-button>
</div>
</div>
<div class="slds-grid slds-m-top_xx-small">
<div class="slds-col">
<div class="slds-grid slds-grid_vertical">
<div class="slds-col slds-m-bottom_xx-small">
<span>Start Date</span>
</div>
<div class="slds-col">
<span>{subscriptionItem.SBQQ__StartDate__c}</span>
</div>
</div>
</div>
<div class="slds-col">
<div class="slds-grid slds-grid_vertical">
<div class="slds-col slds-m-bottom_xx-small">
<span>End Date</span>
</div>
<div class="slds-col">
<span>{subscriptionItem.SBQQ__EndDate__c}</span>
</div>
</div>
</div>
<div class="slds-col">
<div class="slds-grid slds-grid_vertical">
<div class="slds-col slds-m-bottom_xx-small">
<span>Quantity</span>
</div>
<div class="slds-col">
<span>{subscriptionItem.SBQQ__Quantity__c}</span>
</div>
</div>
</div>
</div>
<template if:true={showEditField}>
<lightning-layout multiple-rows class="slds-m-top_xx-small">
<lightning-layout-item size="8">
<lightning-input
type="date"
name="endDateInput"
label="End Date"
variant="label-inline"
>
</lightning-input>
</lightning-layout-item>
</lightning-layout>
</template>
<hr class = "custom-line">
</div>
</div>
</template>
</template>
export default class ExtendCPQSubscriptionLWC extends LightningElement {
#api recordId;
#track subscriptionData;
#track columns = columns;
showEditField = false
#wire(getSubscriptionsLWC, { recordId: '$recordId'})
loadFoundSubscriptions({error, data}) {
if (data) {
console.log('OK');
console.log(data);
this.subscriptionData = data;
} else if (error) {
console.log('ERR ' + JSON.stringify(error));
}
}
I would appreciate any input thanks in advance.
You have common showEditField used to control visibility/addition of date input. My recommendation is to move that information inside subscriptionData and update the track variable. This would trigger re-render of UI and only show that field for item, for which Extend button was clicked

Ionic Text color change onclick

I'm a beginner in Ionic 3.
I have a 4 text in same color, I'm trying to make when I click on the 4 text at for time I want to change color.
Example
I click on 1st text, then I want to change it black color
I click on text 2, then I want to change black color and 1st text is set a default color,
Please help me to fix this issue
<div class="row">
<div class="col right-border">
<div text-center>
<h2 class="main-one" >$ 2,300</h2> <p class="main-txt-home">Today's Revenue</p>
</div>
</div>
<div class="col bottom-border">
<div text-center>
<h2 class="main-one">$ 53,100</h2><p class="main-txt-home">Expected Revenue for this month</p>
</div>
</div>
</div>
<div class="row">
<div class="col top-border">
<div text-center>
<h2 class="main-one">12</h2><p class="main-txt-home"> Bookings taken today</p>
</div>
</div>
<div class="col left-border">
<div text-center>
<h2 class="main-one">68%</h2><p class="main-txt-home">Total Monthly occupancy</p>
</div>
</div>
</div>
</div>
You can use [ngClass] attribute to achieve this. This can be used to dynamically return the class name as per the last clicked text. The code will be something like below.
<div class="row">
<div class="col right-border">
<div text-center [ngClass]="getTextColor('text1')" (click)="setSelectedText('text1')">
<h2 class="main-one" >$ 2,300</h2> <p class="main-txt-home">Today's Revenue</p>
</div>
</div>
<div class="col bottom-border">
<div text-center [ngClass]="getTextColor('text2')" (click)="setSelectedText('text2')">
<h2 class="main-one">$ 53,100</h2><p class="main-txt-home">Expected Revenue for this month</p>
</div>
</div>
</div>
<div class="row">
<div class="col top-border">
<div text-center [ngClass]="getTextColor('text3')" (click)="setSelectedText('text3')">
<h2 class="main-one">12</h2><p class="main-txt-home"> Bookings taken today</p>
</div>
</div>
<div class="col left-border">
<div text-center>
<h2 class="main-one">68%</h2><p class="main-txt-home">Total Monthly occupancy</p>
</div>
</div>
</div>
In controller
private selecteTextId :string;
setSelectedText(textId:string) {
this.selecteTextId = textId;
}
getTextColor(textId:string):string{
return this.selecteTextId == textId? "highlight-color" : "";
}
In scss file
.highlight-color {
color:blue;
}

Is it ok to put Bootstraps' col-*-* class on input elements?

I'm trying to make a responsive form using Bootstrap. Is it correct to use col-**-* classes like this:
<div class="row">
<input class="col-*-blabla" ... ... >
<input class="col-*-blabla" ... ... >
</div>
or should I nest them inside of a div with the col class like this:
<div class="row">
<div class="col-*-*">
<input .. ... >
</div>
<div class="col-*-*">
<input .. ... >
</div>
</div>
Thank you.
You should nest them. There is one example in Bootstrap documentation that shows use of col- classes on label elements (Horizontal forms).
Look at this example, red block is a div with default col-xs-12 class inside row, as you can see below this container, when you use col- classes on input elements they will be misaligned (that's because it applies padding to input elements itself).
.div {
height: 200px;
background: red;
}
.row {
margin-bottom: 20px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="div"></div>
</div>
</div>
<div class="row">
<input type="text" class="col-xs-4">
<input type="text" class="col-xs-4">
</div>
<div class="row">
<div class="col-xs-4">
<input type="text">
</div>
<div class="col-xs-4">
<input type="text">
</div>
</div>

form > .panel > .panel-body > form-group content is not inside the .panel-body

Here is my use case: I want to create panels inside a form in order to distinguish different criteria.
My problem is that when I create a <div class="form-group"> inside my <div class="panel-body">, the content of the form-group goes out of its boundaries: in the screenshot, my checkboxes are out of the panel-body boundaries.
Here is my DOM tree:
<form class="form-horizontal">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox"
Can somebody tells me how to do that in the good way?
The solution I found is to add this to my CSS:
.form-horizontal .panel-body .form-group {
margin-right: 0;
margin-left: 0;
}

Getting index of a clicked element

Hi I have this code. I need to get the 0 based index of the clicked element.
This is my code. I always get -1 as the index.
Ideally clicking first link would print 0 and second would print 1.
I am using jquery 1.3.2. JavaScript code is fine too.
What I am missing?
<script type="text/javascript">
function handleClick(id) {
var par = document.getElementById('par'+id);
alert('Index= ' + $('#clips').index(par));
}
</script>
<div id="clips" style="clear:both">
<div id="par30" class="alb rc32">
<div class="fl rc32 thm">
<a id="30" onclick="handleClick(30);">Link 1</a>
</div>
</div>
<div id="par40" class="alb rc32">
<div class="fl rc32 thm">
<a id="40" onclick="handleClick(40);">Link 2</a>
</div>
</div>
<div id="par50" class="alb rc32">
<div class="fl rc32 thm">
<a id="50" onclick="handleClick(50);">Link 3</a>
</div>
</div>
</div>
This works:
<script type="text/javascript">
function handleClick(id) {
var $par = $('#par' + id);
alert($par.index());
}
<div id="clips" style="clear:both">
<div id="par30" class="alb rc32">
<div class="fl rc32 thm">
Link 1
</div>
</div>
<div id="par40" class="alb rc32">
<div class="fl rc32 thm">
Link 2
</div>
</div>
<div id="par50" class="alb rc32">
<div class="fl rc32 thm">
Link 3
</div>
</div>
</div>
You are using the index function wrong.
Try
<script type="text/javascript">
function handleClick(id) {
var par = document.getElementById('par'+id);
alert('Index= ' + $('#clips > div').index(par));
}
</script>
see http://api.jquery.com/index/
If .index() is called on a collection
of elements and a DOM element or
jQuery object is passed in, .index()
returns an integer indicating the
position of the passed element
relative to the original collection.