send a list of ul to form in django - django

In a template I have two connected lists, one of which is initially empty. The user has to choose up to three options from the left list, add them to the right list, order them (according to his/her priorities) and then send it back.
it looks like:
<ul id="sortable1" class="connectedSortable">
<li value="1">Item A</li>
<li value="2">Item B</li>
<li value="3">Item C</li>
<li value="4">Item D</li>
<li value="5">Item E</li>
<li value="6">Item F</li>
<li value="7">Item G</li>
</ul>
</div>
<ul id="sortable2" class="connectedSortable">
<!--here goes the initially empty list-->
</ul>
and in {% scripts %} block:
<script>
$( function() {
$( "#sortable1, #sortable2" ).sortable({
connectWith: ".connectedSortable"
}).disableSelection();
} );
</script>
in models.py I have a field to store the selected info:
preferences = CommaSeparatedIntegerField()
The problem is how to POST info from the updated ul (sortable2) back to the database? I can add a hidden field preferences to the page and then fill it using jQuery, but it seems like an ugly solution. What is the right one?

If you want to POST the updated ul (sortable2) back to the database, then one of the possible solution is that the ul (sortable2) should be inside a form whose data you can submit using POST method.
Now when any of the li element is moved from the ul (sortable1) to ul(sortable2), then an input type element is also inserted in the ul (sortable2) along with the list element, where "value" attribute of the input will have to be the same as the li element in the ul (sortable1) was. Also the type of this input element will be hidden.
So now when you'll submit this form using a submit button, you'll get all the data back in your views and hence can make changes to your database accordingly.

Related

Remove accordion slide animation on page load

When my page loads the active accordion item slides open which causes the other elements on the page to also move for a second.
I'd like the active accordion item to render "already opened", but still retain the animation for when a user clicks on another accordion item, or even the originally opened item in cases where the user selects another item and then the original.
I cant find any css being used to do this, so maybe its something in the javascript? If so, how would I go about overriding this.
Any suggestions on how to accomplish this would be greatly appreciated!
Foundation Accordion: http://foundation.zurb.com/sites/docs/accordion.html
There's no simple solution from what I know, but you can override some attributes on load so that the active item is displayed by default. You'd have to begin with an accordion none of the items are active i.e. none of the accordion items have a class of .is-active:
Html:
<ul class="accordion" data-accordion data-allow-all-closed='true'>
<li class="accordion-item" data-accordion-item>
Accordion 1
<div class="accordion-content" data-tab-content>
Panel 1. Lorem ipsum dolor
</div>
</li>
<li class="accordion-item" data-accordion-item>
Accordion 2
<div class="accordion-content" data-tab-content>
Panel 2. Lorem ipsum dolor
</div>
</li>
<li class="accordion-item" data-accordion-item>
Accordion 3
<div class="accordion-content" data-tab-content>
Panel 3. Lorem ipsum dolor
</div>
</li>
</ul>
jQuery:
//Initialise the Foundation plugins
$(document).foundation();
function activateWithoutAnimation(itemIndex) {
//Get the accordion item according to its index (0 based)
var $accordionItem = $('.accordion-title:eq(' + itemIndex + ')');
//Set the aria attributes of the accordion item you want to appear
$accordionItem.attr('aria-expanded', 'true');
$accordionItem.attr('aria-selected', 'true');
//Set the attributes of the content.
$accordionItem.next().attr('aria-hidden', 'false');
$accordionItem.next().css('display', 'block');
//This is essential as it lets foundation know that the item is active (to re-allow toggling)
$accordionItem.trigger('click');
}
//Call the function.
activateWithoutAnimation(0);
There might be a better way to do the above, but from what I've read, there's no option that allows the animation to be disabled. I've tried overriding the defaults that Foundation provides to no avail.
Fiddle Demo
CSS Solution
If using the default helper class .is-active on initial load then some plain css can eliminate the slide open animation.
.is-active .accordion-content {
display: block;
}
https://jsfiddle.net/r6jvbohu/
CSS only (without losing the animation)
To always open the first item immediately while still keeping the animation, you can use the following code.
.accordion-item:first-of-type .accordion-content {
display: block;
}
The first item should still have the .is-active class to be consistent, but this removes the initial animation because we overwrite the display value from none to block. The JS still overwrites this state when another accordion item is clicked, resulting in the animation we want to keep.

Foundation 5 - New <li> items now showing in existing dropdown buttons

When I add new list items to an existing dropdown button they do not show up on the front end. The dropdown just continues to show the original list items. All my caching systems are off and I cleared the cache locally, but I'm still getting the same issue. Anyone know what could be causing this?
<div class="sidebar-dropdown">
<button data-dropdown="drop" aria-controls="drop", aria-expanded="false" class="large alert round button dropdown">Select Type</button>
<br>
<ul id="drop" data-dropdown-content class="f-dropdown" role="menu" aria-hidden="false" tabindex="-1">
<li>All-Purpose (USA)</li>
<li>Mobile</li>
<li>E-Commerce</li>
<li>All-Purpose (CAN)</li>
<li>Tablet POS</li>
<li>High Risk</li>
</ul>
</div>
The Foundation-generated dropdown needs to be refreshed when new content is added to it. This can be done by adding this to your javascript, which adds a listener to the dropdown element(s):
$(document).foundation('dropdown', 'reflow');
See the docs for more info

ember.js filter items in array in view

I have a site I am working on that contains minimal data and am fine with making very few actual DB calls to fetch the data. I have a view the separates items out by type and would like to only display the items that match that type in the corresponding list.
I have a few different divs that contain the item types and would like to display a list of items that correspond to that type.
<div>
<p>hammers</p>
<ul>
li>hammer 1</li>
</ul>
</div>
<div>
<p>pencils</p>
<ul>
<li>graphite</li>
</ul>
</div>
So basically anything matching the type of hammers would be displayed in that list and enumerated through the end of all the matches to that type.
You can filter using Ember.computed.filter or Ember.computed.filterBy.
var view = Ember.View.extend({
items: [],
hammers: Ember.computed.filterBy('items', 'type', 'hammer'),
pencils: Ember.computed.filterBy('items', 'type', 'pencil')
});

how to create auto list dividers when dynamically creating a list in jquery mobile

I've created a list of messages dynamically. I have a <ul> tag and I'm generating a list inside dynamically. However, I don't know how to add list dividers to this. Would there be a refresh method in js that I can call which would add dividers after I have my list?
This is what I have:
<ul id="messages" data-autodividers="true" data-role="listview" data-theme="c">
//a list of <li> are dynamically generated here
</ul>
This only appends on list divider to the very top, I need several of them within the <li> tags.
My list of <li>'s are messages. I want to be able to have dividers by Date so it makes it much easier to use.
I've a jsfiddle http://jsfiddle.net/yVtVE/2/
The code is a bit different but it works
assuming a list like this
<ul data-role="listview" id="MessagesList" data-autodividers="true">
<li date="2013-03-20">Event 1</li>
<li date="2013-03-20">Event 2</li>
<li date="2013-03-19">Event 3</li>
</ul>
Your code would be
$("#MessagesList").listview({
autodividers: true,
autodividersSelector: function (li) {
var out = li.attr('date');
return out;
}
}).listview('refresh');
ORIGINAL
From the JQM docs http://api.jquerymobile.com/listview/
Autodividers
A listview can be configured to automatically generate dividers for its items. This is done by adding a data-autodividers="true" attribute to any listview.
By default, the text used to create dividers is the uppercased first letter of the item's text. Alternatively you can specify divider text by setting the autodividersSelector option on the listview programmatically. For example, to add a custom selector to the element with id="mylistview":
$( "#mylistview" ).listview({
autodividers: true,
// the selector function is passed a <li> element from the listview;
// it should return the appropriate divider text for that <li>
// element as a string
autodividersSelector: function ( li ) {
var out = /* generate a string based on the content of li */;
return out;
}
});
Let's say you have a date in your message your code would work something like this
$( "#mylistview" ).listview({
autodividers: true,
autodividersSelector: function ( li ) {
var re = /\/\d{4}\/\d{2}\/\d{2}/i;
var out = $(this).html().match(re);
return out;
}
});
I have not tested this code, it might have bugs, but it will give you an idea to try.

Passing data to Ember JS from Handlebars

I have a menu bar whose status/color etc changes when clicked. These belong to different JSP/HTML pages. When I click on the page, there is a handlebar #bindAttr which decides the class of the each tabbed menuitem. For e.g.
<li id="index" {{#bindAttr class="active"}}>
<a href="./index.jsp">
<i class="icon-dashboard"></i>
<span>Dashboard</span>
</a>
</li>
What I need here is also to pass something else so that the computed property can find out based on the location.href whether this class should be true. Is there any way to pass the id of the "li" tag to the computed property "active"
Solved it. Custom HandleBar Helper Function
<li {{activetab "index.jsp"}}>
Handlebars.registerHelper("activetab", function(tabname) {
var currentPage = window.location.href; console.log(currentPage); if(currentPage.indexOf(tabname)!=-1){
return "class=active";
}
else{
return "";
}
});