jQuery - Append multiple elements one after the other - jquery-ui

I'm looking to create a nice effect when appending multiple paragraphs to a div. When I append I want to fadeIn the paragraphs one at a time so one shows after the other. At the moment they all fade in at the same time.
example html;
<div class="wrapper">
<p class="para">paragraph 1</p>
<p class="para">paragraph 2</p>
<p class="para">paragraph 3</p>
</div>
Here is the code used to append to the div
$(results).prependTo(".wrapper").hide().fadeIn('slow');
(Results) is simply the multiple paragraphs.
Thanks in advance for your help and advice.

You could try something like this:
$(results).bind('appear', function(){ // bind a custom event
$(this).fadeIn('slow', function(){
$(this).next('p.para').trigger('appear'); // recurse
});
})
.prependTo('wrapper')
.end() // back to "results"
.hide() // not necessary if already hidden by style rule
.first().trigger('appear'); // start the cascade
Here's an example: http://jsfiddle.net/redler/CDbEn/

You could use queue() for this:
$('p').prependTo("#wrapper").hide().each(function() {
var element = $(this);
$('#wrapper').queue(function() {
setTimeout(function() {
element.fadeIn('slow');
$('#wrapper').dequeue();
}, 700);
});
});
Example.

//assuming resuls is an array of strings containing your p's
var appear = function(index) {
if (results[index])
$(results[index]).prepend('.wrapeer').hide().fadeIn('slow', function(){ appear(index+1); });
}

What about
$('.para').each(function(index){
$(this).hide().prependTo('.wrapper').delay(500 * index).fadeIn('slow');
});

Related

How to show multiple entries in multiple bootstrap cards?

I want to show my tasks like this in bootstrap cards -
Picture for reference
Code that I'm using right now -
HTML
<div class="card" id="todo"></div>
AJAX
var data = JSON.parse(httpRequest2.response);
console.log(data);
$.each(data, function(index, itemData) {
$("#todo").append('<div class="row"><div class="col-9">'+
itemData.Todo.task+
'</div></div></div>'+
'<div class="col-3"><p>'+
Some Data
</div>'
);
});
It is showing multiple to-do in one card. How can I show one to-do in one card?
Please help!
The problem is that you add all the todo in one card (div#todo). So what you need to do is juste to make a div that will contains the card.
<div id="todos"></div>
const data = JSON.parse(httpRequest2.response);
console.log(data);
$.each(data, function(index, itemData) {
$("#todos").append('<div class="card"><div class="row"><div class="col-9">'+
itemData.Todo.task+
'</div></div></div>'+
'<div class="col-3"><p>'+
Some Data
</div></div>'
);
});
It should work as you want. And you can add css to improve style.

siblings not hiding on a jqueryui slide

here's the function:
$('.popoutlink').on('click', function() {
var box = $('#' + $(this).data('box'));
box.siblings().hide();
box.toggle("slide", { direction: "left" }, 500);
box.siblings().hide();
});
the two siblings.hide statements are because I'm in the middle of trying to figure out why I'm left with two slideouts on screen if I click on two buttons in rapid succession.
The html is:
<div class="col-md-2">
<div class="popoutlink" data-box="p1">1</div>
<div class="popoutlink" data-box="p2">2</div>
<div class="popoutlink" data-box="p3">3</div>
<div class="popoutlink" data-box="p4">4</div>
<div class="popoutlink" data-box="p5">5</div>
</div>
<div class="col-md-10 bb" style="height: 400px;">
<div class="popout" id="p1"><h1>panel 1</h1></div>
<div class="popout" id="p2">
<h1>panel 2</h1>
</div>
If I click on two buttons quickly then two windows are left on screen. I would like the siblings to hide before the selected div appears.
I have tried using promise.done():
box.siblings().hide(200).promise().done(function(){
box.toggle("slide", { direction: "left" }, 500);
});
to no effect. Adding box.toggle to hide as a callback:
box.siblings().hide(200, function(){
box.toggle("slide", { direction: "left" }, 500);
});
was very funny but not useful.
How do I get the siblings to go away reliably before I show the selected div no matter how quickly I click the buttons?
You see it here just click on the numbered boxes quickly
Thanks
If I understand your question, this should help:
https://jsfiddle.net/Twisty/3mbh5p0r/
jQuery UI
$(function() {
$('.popoutlink').on('click', function() {
var box = $('#' + $(this).data('box'));
$(".popout").hide();
//box.siblings().hide();
box.toggle("slide", {
direction: "left"
}, 500);
//box.siblings().hide();
});
});
When any of the "links" are clicked, they are all hidden and then only the one whom was clicked is toggled.
Update
A little more testing of .hide() versus .css("display", "none") revealed that changing the CSS was a faster method. This page talks about how it's immediate but I found that it wasn't as fast.
The matched elements will be hidden immediately, with no animation.
This is roughly equivalent to calling .css( "display", "none" ),
except that the value of the display property is saved in jQuery's
data cache so that display can later be restored to its initial value.
If an element has a display value of inline and is hidden then shown,
it will once again be displayed inline.
And:
Note that .hide() is fired immediately and will override the animation
queue if no duration or a duration of 0 is specified.
I did try using the callback, which made it worse. The callback should be triggered when the hide animation is complete, yet it added the element of animating the hide operation. Even when the speed was 0, it was slower.
So I advise this:
$(function() {
$('.popoutlink').on('click', function() {
$(".popout").css("display", "none");
$('#' + $(this).data('box')).show("fast");
});
});

Jquery data-role collapsible events aren't being captured in Jquery Mobile

Could anybody please let me know why the following code isn't working when i am using with Jquery mobile JS
http://jsfiddle.net/znz17ctm/7/
This is my code
<div role="main" class="ui-content oms-content" id="dd">
<div class="myactivelabelsWrap" id="result"></div>
</div>
var response = {
"Restaurants": [{
"RestrntArea": "Haii",
"cust_loc_id": "374"
}, {
"RestrntArea": "rerrrwe",
"cust_loc_id": "373"
}]
}
showLabels();
function showLabels() {
//$("#result").html("");
var favoriteresultag = '';
for (var i = 0; i < response.Restaurants.length; i++) {
var name = response.Restaurants[i].RestrntArea;
if (name) {
favoriteresultag +=
'<div data-role="collapsible" data-inset="false" class="my-collaspible"><h3>' +
name +
' <a class="icon-pencil-1 labelEditIcon "></a></h3></div>';
}
}
$("#result").append(favoriteresultag).trigger("create");
}
$(document).ready(function() {
$('.my-collaspible').bind('expand', function() {
alert('Expanded');
});
$('.my-collaspible').bind('collapse', function() {
alert('Collapsed');
});
});
Why the collapse and expand even'ts are being captured ??
Instead of document ready i tried with al the page events of mobile . But no luck .
From your fiddle I can't tell which version of jQM you are using. You have checked version 1.3 but then added the 1.4 css. Assumin version 1.4, I have updated your fiddle:
FIDDLE
Basically, you need to use event delegation to attach the events because the collapsibles do not exist at the time of the bind. Also the event names are actually collapsibleexpand and collapsiblecollapse.
So use on() instead of bind() by handling the event on the parent div and delegating it to all items with class my-collapsible that exist now or added dynamically:
$("#result").on('collapsibleexpand', '.my-collaspible', function () {
alert('Expanded');
});
$("#result").on('collapsiblecollapse', '.my-collaspible', function () {
alert('Collapsed');
});

auto-scroll grid in jqm

I have a simple webapp consisting of a main page that contains a grid with 3 columns.
<div data-role="content">
<div class="ui-grid-b" id="main">
</div>
</div>
At runtime this grid is expanded with new rows by clicking a button.
$(document).on('pageinit', '#page-home', function() {
$('button').on('click', function() {
var table = $('#main');
var blocka = $('<div class="ui-block-a">One</div>');
var blockb = $('<div class="ui-block-b">Two</div>');
var blockc = $('<div class="ui-block-c">Three</div>');
blocka.appendTo(table);
blockb.appendTo(table);
blockc.appendTo(table);
});
});
Now I have the problem that if there are more rows added (let's say 10) then the last rows run out of screen.
Is there a simple way to automatically scroll to the last row everytime a new one is created?
I know that I can get the element by calling table.find('.ui-block-a:last') but I don't know how to scroll to the found element.
Thanks in advance!
As suggested in this answer: How to go to a specific element on page?
Add the following after blockc.appendTo(table) in your click handler:
$('html, body').animate({
scrollTop: blocka.offset().top + 'px'
}, 'fast');
Here is fiddle demo: http://jsfiddle.net/ezanker/5ap48/

Optimal way to save order of jQueryUI sortable list with Meteor

I need some guidance/suggestions for an optimal way to save the order of a sortable list that takes advantage of Meteor.
The following is a scaled down version of what I'm trying to do. The application is a simple todo list. The end goal for the user is to sort their list where the data is picked up from the database. As the user sorts tasks, I would like to save the order of the tasks.
I've implemented this application without Meteor using php/ajax calls using sortable's update event that would delete the entry in the database and replace it with what was currently in the DOM. I'm curious to know if there are a better ways to do this taking advantage of Meteor's capabilities.
The following sample code is straight off of a live demo.
HTML:
<template name="todo_list">
<div class="todo_list sortable">
{{#each task}}
<div class="task">
<h1>{{title}}</h1>
{{description}}
</div>
{{/each}}
</div>
</template>
JS(Without the Meteor.isServer that simply populates the database.):
if (Meteor.isClient) {
//Populate the template
Template.todo_list.task = function () {
return Tasks.find({});
};
//Add sortable functionality
Template.todo_list.rendered = function () {
$( ".sortable" ).sortable();
$( ".sortable" ).disableSelection();
};
}
Sample data (Output of Tasks.find({})):
[{
title:"CSC209",
description:"Assignment 3"
},
{
title:"Laundry",
description:"Whites"
},
{
title:"Clean",
description:"Bathroom"
}]
You'd probably want to first sort your items by a new field on you collection then, you'll want to hook into the jQuery sortable update event:
if (Meteor.isClient) {
// Populate the template
Template.todo_list.task = function () {
return Tasks.find({}, { sort: ['order'] });
};
// Add sortable functionality
Template.todo_list.rendered = function () {
$('.sortable').sortable({
update: function (event, ui) {
// save your new list order based on the `data-id`.
// when you save the items, make sure it updates their
// order based on their index in the list.
some_magic_ordering_function()
}
});
$( ".sortable" ).disableSelection();
};
}
You template would look a bit like this:
<template name="todo_list">
<div class="todo_list sortable">
{{#each task}}
<div class="task" data-id="{{_id}}">
<h1>{{title}}</h1>
{{description}}
</div>
{{/each}}
</div>
</template>
And when that event is triggered, it would determine the order of the list and save the new order in the documents for the collection.
This isn't really a complete answer, but hopefully it helps a bit.

Resources