How to dynamically change data-theme in JQM for collapsible? - jquery-mobile

I need to do this action on button click, which is in the collapsible, so i do it like:
my_button.closest('div[data-theme="b"]').find('a.ui-btn-up-b').toggleClass('ui-btn-up-b ui-btn-up-d');
But unfortunately there still remains some styles which needs to be changed, but i don't know which...

Updated answer
Since the dynamic solution doesn't work for collapsible, here is a manual solution.
Working demo
Code
$('#button').on('click', function () {
var oldclass = 'ui-btn-up-b ui-body-b';
var newclass = 'ui-btn-up-d ui-body-d';
$('[data-role=collapsible]').find('a').removeClass(oldclass + ' ui-btn-hover-b').addClass(newclass + ' ui-btn-hover-d');
$('[data-role=collapsible]').find('.ui-collapsible-content').removeClass(oldclass).addClass(newclass);
});
Why collapsible data-theme cant be changed dynamically?
Old answer
Unfortunately, the below dynamic solution surprisingly doesn't work.
Where .selector is the ID of the Collapsible.
$('button').on('click', function () {
// change the theme
$( ".selector" ).collapsible( "option", "theme", "a" );
// apply new styles
$( ".selector" ).collapsible().trigger('create');
});

Related

jQuery UI Selectable Without Selection Box

Given this example here: http://jqueryui.com/selectable/#display-grid I would like to make a selection without the "selection box" that appears when you click 'n drag. Namely, when I click on number 5 and drag to number 6 and then to 10 I get this:
where in fact what i would like to is drag from 5 to 6 to 10 and have only them selected without 9.
I searched the docs and couldn't find that option, and my google skills didn't bring me any luck, and I thought this must have been already done it's just so happens I can't grasp it on my own or find an existing solution, so any help here is appreciated (not saying you should do the research for me, am just hoping someone dealt with this before so he can point me to the right direction).
It also could be I'm missing the point in trying to accomplish this with jquery UI but this was the first such example I found that fits (kind of) what I want to accomplish.
First, you might want to hide .ui-selectable-helper or change the CSS:
.ui-selectable-helper{display:none;}
Next, do something like this:
$(function() {
var _mousedown = false;
$('#selectable').selectable({
start: function(event,ui){
_mousedown=true;
},
stop: function(event,ui){
_mousedown=false;
$('.ui-selected').removeClass('ui-selected');
$('.selecting').addClass('ui-selected');
},
selecting: function(event,ui){
if($('.ui-selecting').length == 1)
$(ui.selecting).addClass('selecting');
$('.ui-selecting').removeClass('ui-selecting');
$('.selecting').addClass('ui-selecting');
},
unselecting: function(event,ui){
if($(ui.unselecting).hasClass('selecting'))
$(ui.unselecting).removeClass('selecting');
}
});
$('#selectable').on('mouseenter', '.ui-selectee', function(){
if(_mousedown)
$(this).addClass('selecting');
});
});
DEMO:
http://jsfiddle.net/dirtyd77/7UVNS/5/ (HELPER HIDDEN)
http://jsfiddle.net/dirtyd77/7UVNS/6/ (HELPER VISIBLE)
Let me know if you have any questions!
***UPDATE:***
.selectable() is not able to do what you are looking for. However, here is something I created. Hope it helps!
JAVASCRIPT:
$(function() {
var _mousedown = false,
_last=null;
$('#selectable li').mousedown(function(){
_mousedown = true;
$('.ui-selected').removeClass('ui-selected');
$('#selectable li').attr('unselectable', 'on').css('user-select', 'none');
$(this).addClass('ui-selecting');
}).mouseup(function(){
_mousedown=false;
$('.ui-selecting').addClass('ui-selected').removeClass('ui-selecting');
$('#selectable li').removeAttr('unselectable style');
}).mouseenter(function(){
if(_mousedown){
if($(this).hasClass('ui-selecting'))
$(_last).removeClass('ui-selecting');
$(this).addClass('ui-selecting')
}
}).mouseleave(function(){
if(_mousedown){
_last = $(this)[0];
$(this).addClass('ui-selecting');
}
});
});
DEMO:
http://jsfiddle.net/dirtyd77/7UVNS/9/
***UPDATE #2:***
If you want to use this on a mobile device, I recommend changing up the format entirely to avoid any possible pitfalls. Here is how I would go about it:
JAVASCRIPT:
$(function() {
var _clicked = false;
$('#btn_select').click(function(){
_clicked = false;
$(this).hide();
$('#selectable li').removeAttr('unselectable style');
$('.ui-selecting').addClass('ui-selected').removeClass('ui-selecting');
});
$('#selectable li').click(function(){
if(!_clicked){
_clicked = true;
$('.ui-selected').removeClass('ui-selected');
$('#selectable li').attr('unselectable', 'on').css('user-select', 'none');
$('#btn_select').show();
}
if($(this).hasClass('ui-selecting')){
$(this).removeClass('ui-selecting');
}else{
$(this).addClass('ui-selecting');
}
});
});
*NOTE: you might want a $('body').click() to act as the end_selection button.
DEMO:
http://jsfiddle.net/dirtyd77/7UVNS/13/
This doesn't answer the original question, but it does show how to access the helper div of the selectable widget.
var helper = $( "#selectable" ).selectable('widget').data().uiSelectable.helper;
helper.css( { border:'none' } );

disappearing dates in jquery mobile datepicker (very weird)

I'm not sure if this is a bug or what, but I'm getting a very strange behavior with jQuery mobile's datepicker.
When clicking a disabled date, the enabled dates disappear! WTF?
Please check out my jsFiddle for a very simple example: http://jsfiddle.net/X5qp8/
I'm modeling this after jquery's recommendation for the mobile hack: http://jquerymobile.com/demos/1.0a4.1/experiments/ui-datepicker/
I'm also rendering it to a div because I need a persistent calendar. Maybe these things just do not jibe...
I've also found that the bug lies in line 33 of their mobile extension, but it is unclear what they are attempting to do there:
$( ".ui-datepicker-calendar .ui-btn", dp ).each(function(){
var el = $(this);
// remove extra button markup - necessary for date value to be interpreted correctly
el.html( el.find( ".ui-btn-text" ).text() );
});
Line 33 is stripping the extra markup so that the datepicker plugin can parse the date for the plugins various events. I have recently found a solution that worked for me, but I am unable to get things working on your jsFiddle. I have commented out some of the extra markup that was being done for mobile styling. Take a look at my version of the updateDatepicker function and hopefully it will be helpful.
function updateDatepicker(){
// $( ".ui-datepicker-header", dp ).addClass("ui-body-c ui-corner-top").removeClass("ui-corner-all");
$( ".ui-datepicker-prev, .ui-datepicker-next", dp ).attr("href", "#");
$( ".ui-datepicker-prev", dp ).buttonMarkup({iconpos: "notext", icon: "arrow-l", shadow: true, corners: true});
$( ".ui-datepicker-next", dp ).buttonMarkup({iconpos: "notext", icon: "arrow-r", shadow: true, corners: true});
// $( ".ui-datepicker-calendar th", dp ).addClass("ui-bar-c");
// $( ".ui-datepicker-calendar td", dp ).addClass("ui-body-c");
// $( ".ui-datepicker-calendar a", dp ).buttonMarkup({corners: false, shadow: false});
$( ".ui-datepicker-calendar a.ui-state-active", dp ).addClass("ui-btn-active"); // selected date
$( ".ui-datepicker-calendar a.ui-state-highlight", dp ).addClass("ui-btn-up-e"); // today"s date
$( ".ui-datepicker-calendar .ui-btn", dp ).each(function(){
var el = $(this);
// remove extra button markup - necessary for date value to be interpreted correctly
el.html( el.find( ".ui-btn-text" ).text() );
});
};
remove the <div id="datepicker"></div> and replace
$("#datepicker").datepicker({minDate : '9/20/2012'});
by
$("#date").datepicker({minDate : '9/20/2012'});
and it will work

Making a jQuery UI droppable only accept one draggable at a time

I have seen only a couple variants on this question asked a couple other places, notably here and here.
Basically, I have a checkers gameboard where each square on the board is a droppable and each gamepiece is a draggable. Each square can only have one piece on it at a time, and I am trying to toggle the enable/disable method depending on whether there's a piece on the square or not.
Here's a link to what I've got so far: http://jsbin.com/ayalaz, and below is the most pertinent code.
function handleDrop(e, ui) {
var tileNumber = $(this).data('tile');
// Make the gamepiece snap into the tile
ui.draggable
.data({ // WHAT IF PIECE IS SET BACK DOWN IN SAME TILE... CHECK FOR IT!
'preRow': ui.draggable.data('curRow'),
'preCol': ui.draggable.data('curCol'),
'curRow': $(this).data('row'),
'curCol': $(this).data('col')
});
$(this).append($(ui.draggable));
ui.draggable
.position({
of: $(this),
my: 'left top',
at: 'left top'
});
$(this).droppable('disable');
//console.log("Gamepiece set down at: (" + $(this).data('row') + "," + $(this).data('col')+ ")");
}
function handleOut(e, ui) {
// HOW TO TOGGLE DROPPABLE??
$(this).droppable('enable');
}
Any advice?
Thanks in advance!
Jeremy
It looks like you are successfully disabling droppable on a tile after the first drop event. Your problem is that you are not initially setting droppable to disabled on the initial set of occupied tiles.
After you've created your game board and pieces, this should accomplish that, assuming my CSS selector accurately reflects your structure:
$('div.gamePiece').parent().droppable("option", "disabled", true);
Note that this is different from the syntax to change droppability in the initial options. From the jQuery UI droppable documentation:
//initialize
$( ".selector" ).droppable({ disabled: true });
//setter
$( ".selector" ).droppable( "option", "disabled", true );
Edit
It appears I was wrong about $(this).droppable('disable'); and $(this).droppable('enable'); jquery-ui does have alias functions for enable and disable.
enable: function() {
return this._setOption( "disabled", false );
},
disable: function() {
return this._setOption( "disabled", true );
},

jQuery combobox: standard script to catch selected value of combobox does not work

I'm using jqueryui combobox example at http://jqueryui.com/demos/autocomplete/combobox.html
I added the script seen below to catch the selected value of combobox:
<div id="selectedOpt">
</div>
<script>
$(document).ready(function() {
$("#combobox").change(function() {
var retval = $(this).val();
$("#selectedOpt").html("retval=" + retval);
});
});
</script>
However, it does not work as expected:
the div selectedOpt does not show selected value of combobox each time
the change event occurs
If "show underlying effect" is selected (pls try at url above), a standard dropdown list
appear. When trying to change value of
that dropdown list, then the div
selectedOpt is able to show value
correctly.
The goal is to have div selectedOpt display the selected option of the combobox.
Please advise and please explain why (1) does not work while (2) works.
PS: all neccessary js, css are correctly included.
Thanks for your kind attention.
SOLUTION FOUND:
http://robertmarkbramprogrammer.blogspot.com/2010/09/event-handling-with-jquery-autocomplete.html
Please change your script to match the code below:
<script>
function test()
{
var retval = $("[id *=dropdown] :selected").val();
$("#selectedOpt").html("retval=" + retval);
}
</script>
And call this script from the server side like this:
dropdown.Attributes.Add("onchange","javascript: return test();")
To show label or value of combobox in your div you have to include your function as an option. Something like this:
$( ".selector" ).autocomplete({
change: function(event, ui) {
$("#selectedOpt").html("retval=" + ui.item.value);
}
});
Use ui.item.label if you want a label instead.

jquery UI : how to define icon for button in HTML

I can define icon for a jquery ui button in code like this;
$( ".selector" ).button({ icons: {primary:'ui-icon-gear'} });
but I would like to define button icon in HTML code. for example
button
this way I can only call..
$( ".selector" ).button();
in onready event and define icons in code. otherwise I need to call button() method for every button that have different icon.
You could use the Metadata Plugin.
button
And the script
$(".jqbutton")
.each(function(){
var data = $(this).metadata();
$(this).button({ icons: {primary:data.icon} });
});
I've never used this directly, but I have used it through its support in the jquery validation plugin.
The Metadata Plugin works fine, and you can even do more: you can also set ALL the init properties of a jQuery UI button (other widgets too):
<script type="text/javascript">
$(document).ready(function(){
$('.jq-button').each(function(){
var meta=$(this).metadata();
$(this).button(meta);
});
});
</script>
New item
Thanks, TJB - great idea! :)
You can set the icon after initializing the button using the "options" parameter
$(".jqbutton").button();
$(".jqbutton.ui-icon-gear").button( "option", "icons",
{primary:'ui-icon-gear'} );
http://jqueryui.com/demos/button/#option-icons
EDIT: the link isn't direct, so just look for the Options tab and select 'icons' then look # the section that says 'Get or set the icons option, after init'
The following code looks for "ui-icon-[icon-name]" in the class-attribute of all elements containing the class "jqbutton" and creates a button with an "ui-icon-[icon-name]" icon.
$(function() {
$(".jqbutton").each(function() {
var obj = $(this);
var icon = false;
var c = obj.attr('class');
var i1 = c.indexOf('ui-icon-');
if (i1 != -1) {
var i2 = c.indexOf(" ", i1);
icon = c.substring(i1, i2 != -1 ? i2 : c.length);
obj.removeClass(icon);
}
obj.button({
icons : {
primary : icon
}
});
});
});

Resources