PDA

View Full Version : jsonView item removal



BernardChhun
9 Jan 2007, 8:45 AM
The jsonView has this jsonData property that shows all of its data as an array. so we use the splice() function to remove an item. Then we refresh the whole jsonView to take the removal into account.



for (var x = 0; viewLen = jsonView.jsonData.length; x < viewLen, x++){
if (jsonView.jsonData[x]["a_property"] == "anything"){
jsonView.jsonData.splice(x,1);
jsonView.refresh();
break;
}
}


here be warned that all your renderer functions will be applied again on the jsonData even though those functions ran during the first rendering. that can give us some surprises...so let's say you use the money function to render numbers:



function money(mnt) {
mnt -= 0;
mnt = (Math.round(mnt*100))/100;
mnt = (mnt == Math.floor(mnt)) ? mnt + '.00' : ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);
return "$ " + mnt;
}


if mnt is equal to
10 it will return
$ 10.

once you refresh the jsonView after the removal it will return
$ NaN because mnt is now equal to
$ 10

You need to change the money function to:

function isNumber(a) {
return typeof a == 'number' && isFinite(a);
}
function money(mnt) {
if (isNumber(mnt)){
mnt -= 0;
mnt = (Math.round(mnt*100))/100;
mnt = (mnt == Math.floor(mnt)) ? mnt + '.00' : ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);
return "$ " + mnt;
}else{
return mnt;
}
}
I added a little number checker to make sure the transformation is not applied on anything else than a number.

that was my hassle of the day!

Bernard