please tell me where i am doing mistake in following javascript code of conversion to float - asp.net

i wrote following code to do some of a specifice column of gridview.. but its not working please tell me were i am missing...
function ManipulateGrid()
{
var gvDrv = document.getElementById("<%= GridView1.ClientID %>");
var gt=0.0;
for (i=1; i<gvDrv.rows.length; i++)
{
var cell = gvDrv.rows[i].cells;
var valold = cell[7].innerHTML;
var val = 0.0;
if(isNaN(parseFloat(valold)))
{
val=0.0;
else
val =valold;
}
gt = parseFloat (gt) + val;
}
alert(gt);
}

It's in and around your bracketed if block. You need to change the if block, and not throw away the conversion results.
if(isNaN(parseFloat(valold)))
{
val= 0.0;
}
else
{
val = parseFloat(valold);
}
Or even better:
var parsed = parseFloat(valold);
if(isNaN(parsed ))
{
val= 0.0;
}
else
{
val = parsed;
}

You haven't closed the curly brace for the if condition properly. Should be something like
var valueToCheck = parseFloat(valold);
if(isNaN(valueToCheck))
{
val= 0.0;
}
else
{
val = valueToCheck;
}

Related

How to get format of current selection?

Is there a way to get the format of the current selection? Here is what I have so far:
var currentFormat:TextLayoutFormat;
var selectionStart:int;
var selectionEnd:int;
var operationState:SelectionState;
var editManager:IEditManager;
if (richEditableText.textFlow && richEditableText.textFlow.interactionManager is IEditManager) {
editManager = IEditManager(richEditableText.textFlow.interactionManager);
selectionStart = Math.min(richEditableText.selectionActivePosition, richEditableText.selectionAnchorPosition);
selectionEnd = Math.max(richEditableText.selectionActivePosition, richEditableText.selectionAnchorPosition);
if (operationState == null) {
operationState = new SelectionState(richEditableText.textFlow, selectionStart, selectionEnd);
}
// this does not work
currentFormat = editManager.getCommonCharacterFormat(operationState);
}
I couldn't find anything so here is what seems to work:
/**
* Get format of element range
**/
public static function getElementRangeFormat(elementRange:ElementRange):TextLayoutFormat {
var leaf:FlowLeafElement = elementRange.firstLeaf;
var attr:TextLayoutFormat = new TextLayoutFormat(leaf.computedFormat);
for (;;)
{
if (leaf == elementRange.lastLeaf)
break;
leaf = leaf.getNextLeaf();
attr.concatInheritOnly(leaf.computedFormat);
}
return Property.extractInCategory(TextLayoutFormat, TextLayoutFormat.description, attr, Category.CHARACTER, false) as TextLayoutFormat;
}
Inspired by ElementRange.getCommonCharacterFormat().

google maps api v3 directions_changed event inside a loop

I have a route that is a combination of multiple sub routes. (By sub routes i mean multiple DirectionsRenderer objects)
I also have a loop where i go throught all of the DirectionsRenderer objects and attach "directions_changed" event to all of them.
Unfortunatelly only that last DirectionsRenderer object fires the event when i try to test what happens when i drag the polyline.
Here is a part of my code:
for(var k=0; k<directionsRenderers.length; k++)
{
var directionsRenderer = directionsRenderers[k];
var a = registerDirectionsChangedEvent(k,directionsRenderer);
}
function registerDirectionsChangedEvent(index, directionsRenderer)
{
this.index = index;
this.directionsRenderer = directionsRenderer;
var that = this;
var a = google.maps.event.addListener(this.directionsRenderer, "directions_changed", function()
{
var newStopPoint;
var directionsResult = that.directionsRenderer.getDirections();
var route = directionsResult.routes[0];
var legs = route.legs;
var legsIndex = 0;
for(var i=0; i<legs.length; i++)
{
var cLeg = legs[i];
var viaWaypoints = cLeg.via_waypoints;
if(viaWaypoints.length > 0)
{
newStopPoint = viaWaypoints[0];
if(that.index === 1) legsIndex += 9;
else if(that.index === 2) legsIndex += 18;
break;
}
legsIndex++;
}
addNewStopPoint(newStopPoint,legsIndex);
});
return a;
}
As i have mentioned above not only the last DirectionsRenderer object get fired but the ones before the last event do not implement the default behaviour.
Maybe you have noticed that i use:
this.index = index;
this.directionsRenderer = directionsRenderer;
var that = this;
event without it it still does not work.
I also tried to use a function created inside the loop like this:
(function someFunction(a,b){//do something here})(a,b);
but still no luck!
any advice how to make it works?
Use function closure, this is probably a duplicate (not tested):
for(var k=0; k<directionsRenderers.length; k++)
{
var directionsRenderer = directionsRenderers[k];
var a = registerDirectionsChangedEvent(k,directionsRenderer);
}
function registerDirectionsChangedEvent(index, directionsRenderer)
{
var a = google.maps.event.addListener(directionsRenderer, "directions_changed", function()
{
var newStopPoint;
var directionsResult = directionsRenderer.getDirections();
var route = directionsResult.routes[0];
var legs = route.legs;
var legsIndex = 0;
for(var i=0; i<legs.length; i++)
{
var cLeg = legs[i];
var viaWaypoints = cLeg.via_waypoints;
if(viaWaypoints.length > 0)
{
newStopPoint = viaWaypoints[0];
if(index === 1) legsIndex += 9;
else if(index === 2) legsIndex += 18;
break;
}
legsIndex++;
}
addNewStopPoint(newStopPoint,legsIndex);
});
return a;
}

google.maps.LatLngBounds () issue

I am trying to find if a latitude and longitude exists in a rectangle or not. I used following code but its having some issue i donno what.
function test(boxes) {
for (var i = 0; i < boxes.length; i++) {
var bounds = new google.maps.LatLngBounds (box[i]);
if (bounds.contains(new google.maps.LatLng(22.7287, 75.8654))) {
alert("i am here");
// marker is within bounds
}
else {
alert("out of box");
}
}
}
if i pass constructor value as 0.0,0.0, it works while if i use boxes[i] its not executing while boxes[i] having the boundaries e.g (17.788,72.828),(21.2620,73.4602)
Based on your comments (you really should update the question), this should work:
function test(boxes) {
for (var i = 0; i < boxes.length; i++) {
if (boxes[i].contains(new google.maps.LatLng(22.7287, 75.8654))) {
alert("i am here");
// marker is within bounds
}
else {
alert("out of box");
}
}
}

What's wrong with this piece of code?

I'm trying to make a image editor in Flex 4.5.
However, there's one little thing that doesn't work as appropriate. Here's my code:
private function returnCropIndicatorBmpDataForTopLeft():BitmapData{
var topLeftX:int = 0;
var topLeftY:int = 0;
var topRightX:int = _bmpData.width;
var topRightY:int = 0;
var bottomRightX:int = _bmpData.width;
var bottomRightY:int = _bmpData.height;
var bottomLeftX:int = 0;
var bottomLeftY:int = _bmpData.height;
var cropIconX:int = _mouseX;
var cropIconY:int = _mouseY;
var temporaryBitmapData:BitmapData = _bmpData;
var originalColor:uint;
var dimmedColor:uint = 0x202020;
for (var i:int = 0; i < _bmpData.width; i++)
{
for (var j:int = 0; j < _bmpData.height; j++)
{
originalColor = _bmpData.getPixel(i,j);
if(i>cropIconX && j>cropIconY){
temporaryBitmapData.setPixel(i,j,originalColor);
}else{
temporaryBitmapData.setPixel(i,j,dimmedColor);
}
}
}
/*
*by the end of this loop, we have, in the temporaryBitmapData variable, a version of the _bmpData,
*the area to be cropped out dimmed a little bit.
*/
return temporaryBitmapData;
}
if you look carefully inside the first if statement in the innermost for loop,
temporaryBitmapData.setPixel(i,j,originalColor);
that bit of code should do the following:
If this (i,j) pixel is outside of the "to-be-cropped-out area", then repaint it with the original pixel color.
It just can't seem to get that to work!!!!
I've substituted that line with some hardcoded value (say, 0xFFFFFF for white) and it did work, so the problem is not there....
Hope you guys can help me out I've spent more than 4 hours on this already trying many different approaches!!
P.S.> I debugged this code in FB 4.5 and placed a breakpoint in that very if statement. For some reason, the _bmpData variable, which is where I want to get the original pixel color from , shows a small red square next to it.... Perhaps that is indicative of something.. perhaps it's somehow thread-'locked'?? I don't know, hope someone can figure out!
EDIT the problem I'm having is the following: the dimming of the image works fine, but if I then move the mouse cursor back to an area that got dimmed out, then the original image doesn't get repainted, as expected.
There must be something else going on there... I just tested your code and it worked fine for me.
package {
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.net.*;
import flash.display.Loader;
import flash.events.Event;
public class ImageCropper extends MovieClip {
protected var _bmpData:BitmapData;
protected var _mouseX:int;
protected var _mouseY:int;
var imageLoader:Loader;
public function ImageCropper() {
_mouseX = 80;
_mouseY = 50;
imageLoader = new Loader();
var image:URLRequest = new URLRequest("http://www.travelooce.com/pics/bear_picnic_table.jpg");
imageLoader.load(image);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, _imageLoaded);
imageLoader.x = 0;
imageLoader.y = 0;
}
public function _imageLoaded($evt:Event):void {
_bmpData = new BitmapData(imageLoader.width, imageLoader.height, false);
_bmpData.draw(imageLoader);
var bmp2:BitmapData = returnCropIndicatorBmpDataForTopLeft();
var bmp:Bitmap = new Bitmap(bmp2);
addChild(bmp);
}
private function returnCropIndicatorBmpDataForTopLeft():BitmapData{
var topLeftX:int = 0;
var topLeftY:int = 0;
var topRightX:int = _bmpData.width;
var topRightY:int = 0;
var bottomRightX:int = _bmpData.width;
var bottomRightY:int = _bmpData.height;
var bottomLeftX:int = 0;
var bottomLeftY:int = _bmpData.height;
var cropIconX:int = _mouseX;
var cropIconY:int = _mouseY;
// This is the important line to change.
var temporaryBitmapData:BitmapData = _bmpData.clone();
var originalColor:uint;
var dimmedColor:uint = 0x202020;
for (var i:int = 0; i < _bmpData.width; i++)
{
for (var j:int = 0; j < _bmpData.height; j++)
{
originalColor = _bmpData.getPixel(i,j);
if(i>cropIconX && j>cropIconY){
temporaryBitmapData.setPixel(i,j,originalColor);
}else{
temporaryBitmapData.setPixel(i,j,dimmedColor);
}
}
}
/*
*by the end of this loop, we have, in the temporaryBitmapData variable, a version of the _bmpData,
*the area to be cropped out dimmed a little bit.
*/
return temporaryBitmapData;
}
}
}
Output:
http://cl.ly/3d0h023C1p392O270I2L

How to get a substring from a string variable which is having HTML tags

I have a string variable assigned with some Content in HTML format.
Some thing like this var strLabel:String = "This is <b>Test</b>;
But in run time i have get the first 10 characters of the string and display it in another control.
For this, what did was
txtLabelName.Text = String(strLabel).substr(0,10) + ' ...';
But in the output i am getting as This is <b. Basically it considering HTML related tags also as text. But my code has to omit the HTML tags while calculating sub string.
Please anybody help me to do this?
Use this regexp to strip html tags (more or less):
txtLabelName.text = strLabel.replace(/\<.+?\>/g, '').substr(0, 10) + ' ...';
It's a very simple regexp to strip all tags so it won't work on more complex tags (probably).
I got this code from some blog. This is working Perfectly
public static function stripHtmlTags(html:String, tags:String = ""):String
{
var tagsToBeKept:Array = new Array();
if (tags.length > 0)
tagsToBeKept = tags.split(new RegExp("\\s*,\\s*"));
var tagsToKeep:Array = new Array();
for (var i:int = 0; i < tagsToBeKept.length; i++)
{
if (tagsToBeKept[i] != null && tagsToBeKept[i] != "")
tagsToKeep.push(tagsToBeKept[i]);
}
var toBeRemoved:Array = new Array();
var tagRegExp:RegExp = new RegExp("<([^>\\s]+)(\\s[^>]+)*>", "g");
var foundedStrings:Array = html.match(tagRegExp);
for (i = 0; i < foundedStrings.length; i++)
{
var tagFlag:Boolean = false;
if (tagsToKeep != null)
{
for (var j:int = 0; j < tagsToKeep.length; j++)
{
var tmpRegExp:RegExp = new RegExp("<\/?" + tagsToKeep[j] + "( [^<>]*)*>", "i");
var tmpStr:String = foundedStrings[i] as String;
if (tmpStr.search(tmpRegExp) != -1)
tagFlag = true;
}
}
if (!tagFlag)
toBeRemoved.push(foundedStrings[i]);
}
for (i = 0; i < toBeRemoved.length; i++)
{
var tmpRE:RegExp = new RegExp("([\+\*\$\/])","g");
var tmpRemRE:RegExp = new RegExp((toBeRemoved[i] as String).replace(tmpRE, "\\$1"),"g");
html = html.replace(tmpRemRE, "");
}
return html;
}

Resources