Using jQuery TinySort plugin to sort by attribute - javascript

I have an unordered list with class name .summary_ul with a number of list items of class .summary_ul_li, each of which has a 'val' attribute whose value is a string of letters. I am calling this function from the javascript file:
function sort_by_val(){
var $tosort = $('.summary_ul_li');
$tosort.tsort({attr:"val"});
}
However, $tosort does not sort, and remains in its original order. I am trying to use this plugin:
http://tinysort.sjeiti.com/
Is there something I'm missing here?

Related

Adding class to filtered Isotope items

My goal is to add a class to all filtered Isotope items.
I am using the getFilteredItemElements functionality from the Isotpe documentation.
I set a variable equal to all the filtered items as below:
var filteredItems = $grid.isotope('getFilteredItemElements');
This returns me an array of elements:
[div.grid-item.p-1.summer.value1.none, div.grid-item.p-1.summer.value1.none, div.grid-item.p-1.summer.value3.none]
I then wanna add a class to the items in the array by using jQuery .each.
filteredItems.each(function(){
this.addClass('');
});
But the browser returns Uncaught TypeError: filteredItems.each is not a function
Am I writing a each function incorrect or is it not possible to use .each in this case?
You can also do
[...filteredItems].forEach(elm => elm.classList.add(''));

knockout js, add new item to an array

So this follows on from my previous question:
knockout js, add additional elements to an array
Basically I have an app where a user fills in certain data, clicks next and this is added to an array. However, what I'd like to do is add some items into the array before the user even begins using the app (these items I get from a database). The idea being that at the start they can view each item in the array and then choose and an item and edit this item. I've got a feeling I'm missing something blindingly obvious but I cannot seem to figure it out
Knockout observable arrays have equivalent functions to native JavaScript arrays. See: http://knockoutjs.com/documentation/observableArrays.html
So you need just to use arr.pop(item) or arr.push(item).
In case you need to replace all items and want to avoid multiple events to raise, use observableArray.valueWillMutate() and valueHasMutated() functions. See sample where I do swap the entire array:
ko.observableArray.fn.replaceWith = function (valuesToPush) {
// NOTE: base on - ko.observableArray.fn.pushAll
var underlyingArray = this();
var oldItemcount = underlyingArray.length;
this.valueWillMutate();
// adding new items to obs. array
ko.utils.arrayPushAll(underlyingArray, valuesToPush);
// removing old items (using KO observablearray fnc.)
if (oldItemcount > 0)
this.removeAll(underlyingArray.slice(0, oldItemcount));
this.valueHasMutated();
return this; //optional
};

How to grab a class name from a specific ID using jQuery

I have an ID that has a class name such as...
<div id="nav" class="style">
I have an array containing all of my IDs called allIds. I'm trying to select an ID from it and then grab its class name. Here is what I have.
var grabClass = $("#"+allIds[0]).map(function()
{
return this.class;
});
I would expect var grabClass to be equal to style. However, if I console log grabClass it says...
[prevObject: b.fn.b.init[1], context: document]
Not exactly sure how to make grabClass equal to ID nav's class style.
Use the below to get class name
var grabClass = $("#"+allIds[0]).attr("class")
Please try this
$("#"+arr[0]).map(function(item){
console.info(this.className);
});
})();
We usually use map to iterate over a set of values in an array. However in this case if you iterate over set of arrays you will be getting the class of last id.
And the above approach is not suitable for iterating over multiple values.
You may want to have a look at this
http://jsbin.com/viyizohexa/edit?html,js,console,output
Hope this be of some help.
Happy Learning

IN CQ, how to set value of all the items in Panel to blank

In ExtJS panel I need to set value of all items (e.g. textfield, pathfield) to blank. I don't want to set value of each individual item to blank but of whole panel in one go.
I am able to get list of items
function getAllChildren (panel) {
/*Get children of passed panel or an empty array if it doesn't have thems.*/
var children = panel.items ? panel.items.items : [];
/*For each child get their children and concatenate to result.*/
CQ.Ext.each(children, function (child) {
children = children.concat(getAllChildren(child));
});
return children;
}
but how to set to blank for whole panel? Please suggest what need to be done in this case.
Actually, it's not possible to do it with one liner - all at the same time. What your method returns is purely an array of objects. In fact if such syntax existed, it would iterate over all fields anyway.
Though clearing all fields, having the method you've proposed is very trivial to do. Just iterate over them all and call reset method. Mind some (especially custom) widgets might not handle it.
var fields = getAllChildren(panel);
CQ.Ext.each(fields, function(field) {
if (child.reset) {
child.reset();
}
});
You've got similar loop in your getAllChildren code - you might reset field at the same place.
The method is defined in Field type which is usually a supertype of each dialog widget. You can read more here.

Bootstrap's Typeahead with two sources and custom separator

Is it possible to add two sources to the Bootstrap Typeahead with custom separator?
Currently I have
source: function (query, process) {
...
process(data.names.merge(data.areas));
...
}
However I would very much like to add custom HTML between the results from the two. Right now they are also mixed together when shown, where I want them in two separate lists with the custom HTML being the separator.
Is it possible?
The answer is yes. You will need to know where the separator should occur in the combined list, which will be widdled down to what matches the user's typed-in "query" (this.query).
You can change the generated HTML by overriding the render method, which you need direct access to the typeahead object to do:
var typeahead = $("#myTypeahead").typeahead(/* ... */).data('typeahead');
typeahead.render = function(items) {
var that = this
// this "map"s the items, which iterates over them and generates a new
// li for each item _by default_; I have modified it
items = $(items).map(function (i, item) {
// return an array containing raw HTML Elements
var elements = []
// determine if separator is necessary, but make sure that it
// is not the first li (which this would be if it matched the
// i === 0 item!)
if (item === "worthSeparating") {
// put whatever you want as the separator in the elements array,
// which will appear in the position that you return it
// you probably don't want text, rather you want some CSS class
elements.push($("<li/>").addClass("menu-separator")[0])
}
// ordinary li that is displayed:
i = $(that.options.item).attr('data-value', item)
i.find('a').html(that.highlighter(item))
elements.push(i[0])
return elements
});
items.first().addClass('active')
this.$menu.html(items)
return this
};
The render method above is modified from the default one. You have complete control over what happens. In fact, if you don't like the default menu, then you can convert the menu by passing it different options that are supplied by default as:
{
menu: '<ul class="typeahead dropdown-menu"></ul>',
item: '<li></li>'
}
Changing those will require different changes to the render method.

Categories