Categories
discuss

jqGrid highlight the new added row

Is that possible to highlight the new added row in jqGrid. The highlight effect is something like this Highlight Effect

So, when the new row is added, the row will be highlighted, that will make clear for user which record is the new one.

Many thanks!

Answer

Yes, I have used the following to briefly highlight a newly-added row. It produces a nice effect to let the user know there is new data, and sounds just like what you are looking for:

jQuery("#" + rowId, "#myGrid").effect("highlight", {}, 2000);
Categories
discuss

What is the java equivalent to javascript’s String.fromCharCode?

What it the java equivalent of javascript’s:

String.fromCharCode(n1, n2, ..., nX)

http://www.w3schools.com/jsref/jsref_fromCharCode.asp

Answer

That would be something like as follows:

public static String fromCharCode(int... codePoints) {
    StringBuilder builder = new StringBuilder(codePoints.length);
    for (int codePoint : codePoints) {
        builder.append(Character.toChars(codePoint));
    }
    return builder.toString();
}

Note that casting to char isn’t guaranteed to work because the codepoint value might exceed the upper limit of char (65535). The char was established in the dark Java ages when Unicode 3.1 wasn’t out yet which goes beyond 65535 characters.

Update: the String has actually a constructor taking an int[] (introduced since Java 1.5, didn’t knew it from top of head), which handles this issue correctly. The above could be simplified as follows:

public static String fromCharCode(int... codePoints) {
    return new String(codePoints, 0, codePoints.length);
}
Categories
discuss

What is the use of adding a null key or value to a HashMap in Java?

HashMap allows one null key and any number of null values. What is the use of it?

Answer

I’m not positive what you’re asking, but if you’re looking for an example of when one would want to use a null key, I use them often in maps to represent the default case (i.e. the value that should be used if a given key isn’t present):

Map<A, B> foo;
A search;
B val = foo.containsKey(search) ? foo.get(search) : foo.get(null);

HashMap handles null keys specially (since it can’t call .hashCode() on a null object), but null values aren’t anything special, they’re stored in the map like anything else

Categories
discuss

How to create a new Date() in Javascript from a non-standard date format

I have a date in this format: dd.mm.yyyy

When I instantiate a JavaScript date with it, it gives me a NaN

In c# I can specify a date format, to say: here you have my string, it’s in this format, please make a Datetime of it.

Is this possible in JavaScript too? If not, is there an easy way?

I would prefer not to use a substring for day, substring for month etc. because my method must also be capable of german, italian, english etc. dates.

Answer

You will need to create a function to extract the date parts and use them with the Date constructor.

Note that this constructor treats months as zero based numbers (0=Jan, 1=Feb, ..., 11=Dec).

For example:

function parseDate(input) {
  var parts = input.match(/(d+)/g);
  // note parts[1]-1
  return new Date(parts[2], parts[1]-1, parts[0]);
}

parseDate('31.05.2010');
// Mon May 31 2010 00:00:00

Edit: For handling a variable format you could do something like this:

function parseDate(input, format) {
  format = format || 'yyyy-mm-dd'; // default format
  var parts = input.match(/(d+)/g), 
      i = 0, fmt = {};
  // extract date-part indexes from the format
  format.replace(/(yyyy|dd|mm)/g, function(part) { fmt[part] = i++; });

  return new Date(parts[fmt['yyyy']], parts[fmt['mm']]-1, parts[fmt['dd']]);
}

parseDate('05.31.2010', 'mm.dd.yyyy');
parseDate('31.05.2010', 'dd.mm.yyyy');
parseDate('2010-05-31');

The above function accepts a format parameter, that should include the yyyy mm and dd placeholders, the separators are not really important, since only digits are captured by the RegExp.

You might also give a look to DateJS, a small library that makes date parsing painless…

Categories
discuss

Java: how to name boolean properties

I just had a little surprise in a Webapp, where I’m using EL in .jsp pages.

I added a boolean property and scratched my head because I had named a boolean “isDynamic”, so I could write this:

<c:if test="${page.isDynamic}">
   ...
</c:if>

Which I find easier to read than:

<c:if test="${page.dynamic}">
   ...
</c:if>

However the .jsp failed to compile, with the error:

javax.el.PropertyNotFoundException: Property 'isDynamic' not found on type com...

I turns out my IDE (and it took me some time to notice it), when generating the getter, had generated a method called:

isDynamic()

instead of:

getIsDynamic()

Once I manually replaced isDynamic() by getIsDynamic() everything was working fine.

So I’ve got really two questions here:

  1. is it bad to start a boolean property’s name with “is”?

  2. wether it is bad or not, didn’t IntelliJ made a mistake here by auto-generating a method named isDynamic instead of getIsDynamic?

Answer

  1. Sensitive subject, but in my opinion it is bad. The variable name should not denote a question, but a statement. E.g. pageIsDynamic, dynamical or dynamicallyGenerated. There is however no clear coding convention for this. As long as you’re consistent throughout the coding, either way won’t harm that much.

  2. No, it didn’t. The Javabean specification states that it is allowed to prefix boolean getter method names with is as well. It is usually preferred above get. As every other decent IDE, IntellIJ just adheres this specification. Eclipse and Netbeans would do the same. Here’s an extract of chapter 8.3.2:

8.3.2 Boolean properties

In addition, for boolean properties, we allow a getter method to match the pattern:

public boolean is<PropertyName>();

This “is<PropertyName>” method may be provided instead of a “get<PropertyName>” method, or it may be provided in addition to a “get<PropertyName>” method.

In either case, if the “is<PropertyName>” method is present for a boolean property then we will use the “is<PropertyName>” method to read the property value. An example boolean property might be:

public boolean isMarsupial();
public void setMarsupial(boolean m);
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..