Ajax loaded nested list Can't get event from inner - jquery-ui

We have this nested list:
<ul id="AllTopics" data-role="listview" data-inset="true" data-filter="true" data-filter-placeholder="Search topic...">
<li>Polite Phrases<span class="ui-li-count">101</span>
<ul data-role="listview" data-inset="true" >
<li >Polite Phrases<span class="ui-li-count">101</span></li>
<li >At The End Of A Letter/Email<span class="ui-li-count">101</span></li>
</ul>
</li>
</ul>
The first <ul> with the id="AllTopics" is in the html documnet itself,
The inner <li><ul><li>... are loaded from Ajax call like:
on('pageinit'... event
... $("ul").append(...
I can get the event from the first new born <li> , like:
$('#AllTopics').on('click','li',function (event){...
But the inner <li> or <a> do not seem to fire events :-(
Ani ideas ?
Thank's in advance

The problem would be that the new content is loaded using an Ajax call, and you just setup the events before, so the new elements won't have them.
Try using delegate JQuery function:
$('#AllTopics').delegate('li a','click',function(){
alert('How you dare to click on my links?!');
});
EDIT
Other solution is to assign the events once elements are added via Ajax call.
// This is an example, retrieve the data on your own form
$.get('mypage.php',function(data){
// Add content to your DOM elements
$('#AllTopics').append(data);
// Assign events
$('#AllTopics').on('click','li',function (event){
alert('Holy moly, you keep clicking on my links!');
});
});
Note: I just realized that your HTML elements have this composition:
<ul>
<li>
<a/>
</li>
<span/>
<ul>
<li>
</a>
</li>
</ul>
</ul>
So, OF COURSE that is only affecting the first one because there are two levels of li - a (you have an ul element inside another one). You can define an ID for your second group of li - a elements to trigger events successfully or keep doing it in this way:
$('#AllTopics').delegate('li a','click',function(){
alert('How you dare to click on the first link?!');
});
$('#AllTopics').delegate('ul li a','click',function(){
alert('Stop clicking my children links!');
});

Related

parse nested li inside ul and ol

I have a scenario in which when li comes under ul I need to replace it with a dot(.) and when li comes and ol I need to replace it with a number.
But the problem is-
1) It is not doing for nested li
2) It is appending at the same level. Same level means as soon as it finds li it will first add dot(.) and then it will add number.
What I want
1) Whenever li comes inside ul it should add dot(.).
2) Whenever li comes inside ol it should add a number.
data = "<ol>\n<li>Introduction\n<ol>\n<li>hyy ssss</li>\n</ol>\n</li>\n<li>Description</li>\n<li>Observation</li>\n<li>Results</li>\n<li>Summary</li>\n</ol>\n<ul>\n<li>Introduction</li>\n<li>Description\n<ul>\n<li>Observation\n<ul>\n<li>Results\n<ul>\n<li>Summary</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n</li>\n<li>Overview</li>\n</ul>\n<p>All the testing regarding bullet points would have been covered with the above content. Hence publishing this content will make an entry in in the selected page, cricket page and so on.</p>\n"
content = Nokogiri::HTML.parse(data)
content.at('ul').children.xpath("//li").each { |li| li.inner_html="\u2022 "+li.inner_html }
content.at('ol').children.xpath("//li").each_with_index { |li,index| li.inner_html="#{index} "+li.inner_html }
Perhaps you need this:
content.css('ol').reverse.each do |ol|
ol.css('> li').each_with_index { |li,index| li.inner_html="#{index + 1} "+li.inner_html }
end
content.css('ul > li').reverse.each { |li| li.inner_html="\u2022 "+li.inner_html }
puts content
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<ol>
<li>1 Introduction
<ol>
<li>1 hyy ssss</li>
</ol>
</li>
<li>2 Description</li>
<li>3 Observation</li>
<li>4 Results</li>
<li>5 Summary</li>
</ol>
<ul>
<li>• Introduction</li>
<li>• Description
<ul>
<li>• Observation
<ul>
<li>• Results
<ul>
<li>• Summary</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>• Overview</li>
</ul>
</body></html>
Reason of doing reverse -
Consider the dom:
<ul>
<li>Description
<ul>
<li>Observation</li>
</ul>
</li>
</ul>
When you do content.css('ul > li'), you get in order of [description, observation]. Without reverse, when you run the snippet, you change the description, but doing so will also change the object_id of observation node. Then you changed the observation node which is not referenced anywhere in content. That's why, I reversed it and acquired children before parents. By doing this, I made sure I'm changing the child first and then changed the parent so parent was aware of the change in child and there is no unreferenced node anywhere.
Suppose description's node id is 1234 and observation node_id is 2345. When you mutated description, it changed itself but also changed it's child(2345). New object id can be 3456 and 4567 respectively. Then you changed 2345 (by iteration), but it makes no effect because your content is showing 3456 -> 4567
Hope this makes sense.

Change previous-text and next-text from Pager (Angular & UI-Bootstrap)

I want to change the button texts from Pager UI-Bootstrap in Angular.
I've this array:
categories = ["Standard", "Premium"];
But this code show the variable's name and with moustache doesn't work.
<uib-pager total-items="categories.length" items-per-page=1 ng-model="page" previous-text=categories[page-2] next-text=categories[page] ></uib-pager>
This works but I would prefer use uib-pager:
<ul class="pager">
<li class="previous" ng-class="{'disabled':!categories[pag-2]}" ng-click="pag=pag-1">{{categories[pag-2]}}</li>
<li class="next" ng-class="{'disabled':!categories[pag]}" ng-click="pag=pag+1">{{categories[pag]}}</li>
</ul>

How do I have an Angular.dart filtered list update automatically

I have an html template that filters a list by the column property of the objects of that list like so:
<ul>
<li card-view
card-id="state.card"
ng-repeat="state in ctrl.game.states | filter:{column:'backlog'} "
ng-include="cardview.html">
</li>
</ul>
If I modify the column property in one of the elements of that list, the display does not update.
How can I make that happen?
Here's one option that uses an imaginary placeholder tag and avoids the |filter replacing it with an ng-if, but I hope someone has a better answer than this one.
<ul>
<xx ng-repeat="state in ctrl.game.states">
<li card-view
card-id="state.card"
ng-if="state.column == 'backlog'"
ng-include="cardview.html">
</li>
</xx>
</ul>
Doing the ng-if and ng-repeat on the same element didn't work.

Grails: g:remotelink and one request only

I am using g:remotelink from Grails' tags to do one ajax request in a dropdown menu:
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<g:remoteLink class="dropdown-toggle" data-toggle="dropdown"
controller="mystuff" action="items" update="itemsListMenu">Items<b class="caret"></b></g:remoteLink>
<ul id="itemsListMenu" class="dropdown-menu">
</ul>
</li>
</ul>
it's working fine but I want to start a request only the first time the dropdown menu is pressed and not every time the menu is open or closed.
I read on the Grails' remotelink docs that it is possible using onSuccess/onLoaded/onComplete/etc functions but I am not sure what is the best way to do it.
Any idea?
UPDATE:
Following john smith suggestion I have added this:
<g:remoteLink id="myButton" onSuccess="jQuery('#myButton').attr('onclick',' ')" ... >
if you want to use the onsuccess event handler with style, and if youre including jquery
here you go
function killLink(element) {
element.attr("onclick"," ");
}
This function will simply remove the onclick ajax handler that grails creates from the tag,
your gsp would look like this
<g:remoteLink class="dropdown-toggle" data-toggle="dropdown"
controller="mystuff" onsuccess="killLink(this)" action="items" update="itemsListMenu">Items<b class="caret"></b></g:remoteLink>
grails remoteLink create a JQuery Ajax request, so with jquery I would do something like this:
// first set loaded to false
var loaded = false;
// if not loaded execute ajax request
if(!loaded){
$.ajax({
type:'POST',
url:"/mystuff/items",
success:function(data){
$("#itemsListMenu").html(data);
}
}).done(function(){
// when done set loaded to true.
loaded = true;
});
}

Simple multi tier accordion list with jquery

I am trying to simplify a navigation tree with a jquery accordion style menu. With some help from other posts i feel like im missing something simple. What is getting me is that one category has a second sub list. I cant get it to to open.close correctly. the sample code I have here just does not expand "T1 sub b" item. What am I missing?
http://jsfiddle.net/9uvgs/203/
html:
<ul class='menu'>
<li>Tier1</li>
<ul>
<li>T1 sub a</li>
<li>T1 sub b</li>
<ul>
<li>T1 sub i</li>
</ul>
</ul>
<li>Tier 2</li>
<ul>
<li>T2 sub a</li>
<li>T2 sub a</li>
<li>T2 sub a</li>
</ul>
</ul>
Jquery
$(document).ready(function(){
$('ul.menu ul').hide();
$('ul.menu>li').click(function(){
$(this).next('ul').slideToggle();
});
});
$(document).ready(function(){
$('ul.menu ul').hide();
$('ul.menu li').click(function(){
$(this).next('ul').slideToggle();
});
});
I've done this and it looks like working just fine.
You are only applying the click event to the direct children of ul.menu a simple solution would be to change you selector to ul.menu li instead of ul.menu>li
$(document).ready(function(){
$('ul.menu ul').hide();
$('ul.menu li').click(function(){
$(this).next('ul').slideToggle();
});
});

Resources