Wednesday, October 2, 2013

Add radio input dynamically to jQuery Mobile fieldset controlgroup

I’ve seen variations on this question yet not my specific case on SO, so here goes.

I’m having trouble adding a new radio input dynamically to an existing control group using jQuery Mobile. It will add, yet the style is not correct & it doesn’t appear to interact with the other inputs correctly.

Here’s my fiddle:
http://jsfiddle.net/cjindustries/3mQF6/

Can anyone see what I’m doing wrong? Or is is a case of regenerating the whole fieldset /controlgroup again? 🙁

Thanks! Code below too…

        <div id="testPage" data-role="page">        <div data-role="content">    <fieldset data-role="controlgroup">        <legend>Radio buttons, vertical controlgroup:</legend>            <input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked">            <label for="radio-choice-1">Cat</label>            <input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2">            <label for="radio-choice-2">Dog</label>            <input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3">            <label for="radio-choice-3">Hamster</label>            <input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4">            <label for="radio-choice-4">Lizard</label>    </fieldset>        </div>    </div>$('<input type="radio" name="radio-choice-5" id="radio-choice-5"><label for="radio-choice-5">Horse</label>')    .appendTo("fieldset");$("fieldset").trigger('create');

You need to use name="radio-choice-1" as you have used in above. This solves you behavior problem

Behavior problem solved with below change
Your modified code for dynamically adding radio.

 $('<input type="radio" name="radio-choice-1" id="radio-choice-5"><label for="radio-choice-5">Horse</label>').appendTo("fieldset");

Style problem solved with below change
Just recreate div instead of fieldset

 $("div").trigger('create');

Check Fiddle Demo

CSS alternating rowbased floating

Im not sure how exactly to call my problem, hence the cryptic title.

The situation I have is the following:

<div class="widget full">foobar</div><div class="widget half">left</div><div class="widget half">right</div><div class="widget half">left</div><div class="widget full">foobar</div><div class="widget full">foobar</div><div class="widget half">left</div><div class="widget half">right</div>

I let these widgets float left & right, based on the odd or even css:

#main > div.widget.full { display: block; width: 100%; }#main > div.widget.half { display: block; width: 49%; }#main > div.widget.half:nth-child(2n+1) { float: right; }

Works like a charm on the first 2 ‘half’ ones, yet the 2 after that obtain screwed over by the 3rd ‘half’. The last 2 are in ‘reversed order’. What I need is some sort of reset or other solution that will put the divs in the right place, regardless of what is between them.

If there is no CSS answer for this I can fix it generating them in PHP, yet CSS has my preference since its a style-issue.

Thanks in advance

Unfortunately, you will not able to achieve this via css only.

This selector:

#main > div.widget.half:nth-child(2n+1)

Will not look for 2n+1 occurrences of .widget.half children, yet instead will target only .widget.half elements that are children number 1, 3, 5, etc.

Tuesday, October 1, 2013

Hibernate: How to get full sql query for session.save method?

I’m saving new object via Hibernate. What I would like to obtain is full sql query which was use. In debug Im getting query with “?” .

 ... values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

How to obtain same query with values.

K.

This is from my log4j configuration (Hibernate 3.6.8).

log4j.logger.org.hibernate.SQL = DEBUG, C

log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder = TRACE, C

log4j.appender.C = org.apache.log4j.ConsoleAppender

log4j.appender.C.layout = org.apache.log4j.PatternLayout

log4j.appender.C.layout.ConversionPattern = [%d{dd MMM yyyy HH:mm:ss,SSS}] %-5p %C.%M : %m%n

SQL has the logger for the SQL statement (you need to set it to DEBUG level) & BasicBinder the logger for the actual parameters (TRACE level)

change date format Y-m-d H:i:s from json feed with jquery

I have received a problem with converting the date format from Y-m-d H:i:s to dd-MM-YYYY by using JQUERY.

My json looks like:

{  "status": "ok",  "posts": [    {       "id": "21",      "title": "Title",      "date": "2013-06-26 06:46:29"    }  ]}

And ajax request:

       $.ajax({         url: ,        async: false,        callback: 'callback',        crossDomain: true,        contentType: 'application/json; charset=utf-8',        dataType: 'jsonp',        timeout: 2000,        success: function (data, status) {           if (data !== undefined && data.posts !== undefined) {            $('#news').append('<a class="item" href="single.html?type=news&id=' + item.id + '">' + item.title  + item.date + '</a>');           }            } });

Can anyone kindly assist me by using jsfiddle? I am new to jquery….

Utilize the split function in pure JavaScript, & toss the varaibles around.

var date = "2013-06-26 06:46:29";var dateSplit = date.split(" ");var dateSplit2 = dateSplit[0].split("-");var formattedDate = dateSplit2.reverse().join('-');   // 26-06-2013

This might seems a yet ugly, & it is. But it suits, as long as you don’t actually need to format the date in other ways (like timezone & such). Then you’d have to look into the Date() object.

EDIT: I want to encourage people to use as much pure JavaScript as possible, due to it’s speed. Often jQuery libraries & functions is filled with overhead, which causes your site to not only load slow, yet moreover process slow. There are no short-cuts to the perfect code, you’ll have to spend some time with it, & learn some tips & tricks. Good luck 🙂

Regex before a colon

I have this string:

[Provider].[Provider]=[ProviderArea].&[X12: WALES]

I want to grab just the X12 part.

I tried:

(?<=: )\w+(?=\]$)

However, this only gets WALES.

I moreover tried:

^[^:]+:\s?

But this gets the whole string before & including the colon.

Where am I going wrong?

If you wan’t to find word (\\w+) between &[ & : then try maybe

(?<=&\\[)\\w+(?=:)

Sum a multidimensional list in python

How can i compute this :

[["toto", 3], ["titi", 10], ["toto", 2]]

to obtain this:

[["toto", 5], ["titi", 10]]

thanks

You can use collections.defaultdict

>>> from collections import defaultdict>>> d = defaultdict(list)>>> for i, j in L:...     d[i].append(j)... >>> [[i, sum(j)] for i, j in d.items()][['titi', 10], ['toto', 5]]

Thanks @raymonad for the alternate, cleaner, solution:

>>> d = defaultdict(int)>>> L = [["toto", 3], ["titi", 10], ["toto", 2]]>>> for i, j in L:...     d[i] += j... >>> d.items()[('titi', 10), ('toto', 5)]

Monday, September 30, 2013

Loop through nested objects with jQuery

Hey everyone I am trying t find the most dynamic way to loop through an array & return specific values return specific values… The json is deeply structured & may change, could there be a $.each() formula that can help?

Example:

var myobj = {    obj1: { key1: 'val1', key2: 'val2' },    obj2: { key1: '2val1',            key2: { nest1: 'val1', nest2: 'val2', nest3: 'val3' },            key3: { nest1: 'K3val1', nest2: 'K3val2',                  nest3: [                         { nest1: 'val1', nest2: 'val2', nest3: 'val3' },                          { nest1: 'val1', nest2: 'val2', nest3: 'val3' }                        ]                 }          },    obj3: { key1: 'dddddval1', key2: 'val2' }    }

now lets say i want to retrieve “K3val2” value yet instead of hardcoding it like so: myobj.obj2.key3.nest2 is there a dynamic way I do this with $.each() mybe?

You can simply nest calls to $.each:

Live Example | Live Source

// Loop the top level$.each(myobj, walker);function walker(key, value) {    // ...do what you like with `key` & `value`    if (value !== null && typeof value === "object") {        // Recurse into children        $.each(value, walker);    }}

If you want to know how deep you are, you can do that too:

Live Example | Live Source

var path = "";// Loop the top level$.each(myobj, walker);function walker(key, value) {    var savepath = path;    path = path ? (path + "." + key) : key;    // ...do what you like with `key` & `value`    if (value !== null && typeof value === "object") {        // Recurse into children        $.each(value, walker);    }    path = savepath;}