JADE multi array - html

I have so many arrays and want to use it
- list = ['index1', 'index2', 'index3'];
- list2 = ['list2', 'list21', 'list22'];
each item in list
div #{item}
each item2 in list2
span #{item2}
but it compiled this:
index1list2list21list22
index2list2list21list22
index3list2list21list22
I want this result:
index1list2
index2list21
index3list22
Are any solution for this? Can jade do this?

It seems like you want to iterate through list and get the element at the same index in list2.
What you probably want to do is something like the following:
- list = ['index1', 'index2', 'index3'];
- list2 = ['list2', 'list21', 'list22'];
each item, index in list
div #{item}
span #{item2[index]}
This will give you
index1list2
index2list21
index3list22
You'll want to ensure that list.length = list2.length and handle the case where they aren't (more specifically, when list.length > list2.length). Otherwise, you'll likely get something like the following:
- list = ['index1', 'index2', 'index3', 'index4'];
- list2 = ['list2', 'list21', 'list22'];
each item, index in list
div #{item}
span #{item2[index]}
Result:
index1list2
index2list21
index3list22
index4undefined

Related

Display a value in HTML using MVC

I have a list held by Session's variable. Suppose: Session["SOF"].
Within this list, there are several items. Suppose each item is an integer. Let's say I want to find the item which has the value 9 and then display the item's value in HTML (this thing is clearly stupid but it is just example).
Here is what I tried in my HTML code in order to display the number:
#(List<int>(Session["SOF"]).FirstOrDefault(x => x.value == 9).ToString());
obviously it didn't work. So how can I do it?
I would use foreach in this case
Try this:
#foreach(var item in Session["SOF"])
{
if(item == 9)
{
<span>#item.ToString()</span>
}
}
Maybe casting to a List<int> will be needed for Session["SOF"], but give it a try.

Sort HTML emlements by child contents

I have these elements in a list. I want to sort the list alphabetically but how do i do that if the text that i want to sort it by is in a child element?
<div class="entry">
<button class="title btn btn-primary">Tale of Memories</button>
</div>
Without seeing your code, it is difficult to know exactly how things will work. Here is a generic solution:
Select all the elements in the list (eg: using querySelectorAll).
Transform the result of step 1 into an array (eg: with this solution from andrewmu)
Use the native sort() method to sort the array (providing your own function to compare values based on whatever you want).
Rewrite the content of the list with the content of the array.
Here is a demo. In it, we sort the list not based on the text directly in each item, but on the text from the span within each of them (for that reason the FA item goes first instead of last). In your case, you will want to change the compare function to get whichever element you want the items to be compared by:
// step 1: select the list items
var items = document.querySelectorAll("#mylist li");
// step 2: convert the node list into an array
var itemsarray = Array.prototype.slice.call(items, 0)
// step 3: sort the array using your own compare function
itemsarray.sort(function(a,b) {
return a.querySelector("span").innerHTML > b.querySelector("span").innerHTML;
});
// step 4: empty the list, and insert the sorted items
var ml = document.getElementById("mylist");
ml.innerHTML = "";
for (var x = 0; x < itemsarray.length; x++) {
ml.appendChild(itemsarray[x]);
}
<ul id="mylist">
<li><span>E</span></li>
<li><span>D</span></li>
<li><span>B</span></li>
<li>F<span>A</span></li>
<li><span>C</span></li>
</ul>

In linq.js in need a query for list inside list

I am having a list like:
var a=[
{col1:"a", list:[{subCol:"s1"},{subCol:"s2"}]},
{col1:"b", list:[{subCol:"s1"},{subCol:"s2"}]}
]
Here am having list inside list. For this I want a query like enter code here
linq(a).where("$.col1=='a' && $.list.subCol=='s1'")
I tried this syntax but gives empty list as output. Is this correct?
list is an array of objects. I suppose you're trying to check if any item in the list has a subCol s1. You'd have to check the items.
var query = Enumerable.From(a)
.Where("$.col1 === 'a'")
.Where("Enumerable.From($.list).Any(\"$.subCol === 's1'\")")
.ToArray();

Sort XMLListCollection for Spark MobileGrid then Take x elements

The spark list and grid components must take an iList as the dataprovider. Instead of providing them with an XML list, we must now use XMLListCollection. Sorting an XMLListCollection is easy, but when I attempt to then take to first x number of elements, they are returned unsorted.
How can one first sort, then take the first x elements from the XMLListCollection?
Example sort code:
var nameSort:Sort = new Sort();
var nameSortField:SortField = new SortField( sortField, true);
nameSort.fields = [nameSortField];
pDataCollection = new XMLListCollection(filteredData);
pDataCollection.sort = nameSort;
pDataCollection.refresh();
The sort is easy now to splice from the first element.

adding elements to empty json array and removing them

I have created an empty json object having an array itemlist(which further contains itemid and title of an item) by this:
var jsonObj = {};
jsonObj.itemlist=[];
jsonObj.itemlist.push({});
Firstly, have i done the declaration correctly?
Secondly, the title and itemid are generated dynamically so i need to add them to the itemlist array. I tried this but it keeps only one array element:
jsonObj.itemlist['title']=gentitle;
jsonObj.itemlist['itemid']=genitemid;
How can i add multiple elements (not all at once) if i have an empty array of itemlists?
Also, i also need to remove a particular array element based on the title of the element. How can that be done? I think the splice and delete function can be used for this, but how can i find the index of that element?
since you already pushed a empty object into the array, you need to modify that object:
jsonObj.itemlist[0]['title']=gentitle;
jsonObj.itemlist[0]['itemid']=genitemid;
To add more objects, you can do the same thing: push in an empty object, then modify that object. Or, you can create an object, modify it, then push it into the list.
var new_obj = {'title':gentitle, 'itemid':genitemid};
jsonObj.itemlist.push( new_obj );
To delete objects with certain attribute value:
for (var i = jsonObj.itemlist.length-1; i >= 0; i--)
if(jsonObj.itemlist[i]['title'] == "to-be-removed")
jsonObj.itemlist.splice(i,1);
Note that you need to go backward, otherwise the splice will mess up the array indexes