Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Updating datagrid after user text input?

Guest
Jan 27, 2013 Jan 27, 2013

Hi guys

I have an editable datagrid.

When you enter text in it, I notice that the dataprovider doesn't update until after you've clicked out of the row twice.

eg

Type 'new stuff' in the column 'Whoopa', hit return (or click another row), then, in function cell_edit_Ended(event:DataGridEvent)...

var _newText = TextInput(event.currentTarget.itemEditorInstance).text;

trace(_newText); // traces 'new stuff'

trace("record: "+data_grid.dataProvider.getItemAt(_row)["Artist"]); // traces nothing

Click another row, and trace("record: "+data_grid.dataProvider.getItemAt(_row)["Artist"]); // traces 'new stuff'

How do you get it to update?  It's causing wacky errors with a duplicate removal function I follow it up with.

Cheers for your help.

TOPICS
ActionScript
1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 28, 2013 Jan 28, 2013

Just open a new AS3 doc (FP11+) and make a string with the data your JSON.stringify() is making and test a JSON.parse() on it directly to an Array:

var str:String = '[{"Delete":"X","Genre":"none","Duet":""}]';

var obj:Array = JSON.parse(str);

trace(obj);

You get the error:

Scene 1, Layer 'Layer 1', Frame 1, Line 21118: Implicit coercion of a value with static type Object to a possibly unrelated type Array.

Because JSON.parse() returns an "Object" and you're assigning it to an "Array".

Even though you

...
Translate
LEGEND ,
Jan 27, 2013 Jan 27, 2013

Check the type of event you are using.  Very often object class events are triggered before the change is made, so you get the data before the event.  Usually you need to resort to using a more universal Event.CHANGE event to capture the state of something after the change is made.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 27, 2013 Jan 27, 2013

Have you tried running data_grid.invalidate(); (next frame forced revalidation) or data_grid.validateNow(); to see if you get more response (valid) data?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 27, 2013 Jan 27, 2013

use data_grid.validateNow() or use an enterframe event listener to delay tracing your dataProvider.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 27, 2013 Jan 27, 2013

Thanks guys

The listener is:

DataGridEvent.ITEM_EDIT_END

The event is:

event:DataGridEvent

data_grid.invalidate() didn't do anything (although I used it in another part of my code to fix another issue, so thanks!!!)

data_grid.validateNow() caused an #2094: Event dispatch recursion overflow. and crashed the app

Initially I just used the function myFunc(event:DataGridEvent) to decide if the user inputted text should be used to rename a file on their hard drive, by seeing if the rows 'File Path' column is populated and the cell was indeed actually changed (works fine).

Then I wanted to use it to call a 'remove duplicates' function after user input, as I don't want any rows to have the same data in three particular columns. The duplicates func works fine on it's own if I've imported csv data into the datagrid - it strips dups out, but something falls over after manual input and gives the error:

#1009: Cannot access a property or method of a null object reference

then...

Error #2025: The supplied DisplayObject must be a child of the caller

then my datagrid disappears.

I'll take a look in the morn as it's 4.20am, but does anything there ring any bells as to what I'm doing wrong?

Cheers again guys.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 27, 2013 Jan 27, 2013

both of the two solutions below work:

var dp:DataProvider=data_grid.dataProvider;

data_grid.addEventListener(DataGridEvent.ITEM_EDIT_END,cell_edit_Ended);

function cell_edit_Ended(e:DataGridEvent):void{

    dp.validateNow();

    for(var i:int=0;i<dp.length;i++){

        for(var s:String in dp.getItemAt(i)){

            trace(s,dp.getItemAt(i))

        }

    }

    this.addEventListener(Event.ENTER_FRAME,ff);

}

function ff(e:Event):void{

    this.removeEventListener(Event.ENTER_FRAME,ff);

    for(var i:int=0;i<dp.length;i++){

        for(var s:String in dp.getItemAt(i)){

            trace(s,dp.getItemAt(i))

        }

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013

Cheers kGlad

Could you please clear up a little confusion I have about data providers?

I've been making edits to the datagrid like...

(initial set up):

var myData:Array = new Array();

var _songListDP = new DataProvider();

myData = JSON.parse(event.target.data);// add string data from file to array as objects
_songListDP = new DataProvider(_myData);//add array to dataprovider

data_grid.dataProvider = _songListDP;

_myData = [];//remove to free up memory

then editing example...

data_grid.dataProvider.removeItemAt(rowIndices[i2]);

Should I instead be making the edits to _songListDP?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 28, 2013 Jan 28, 2013

Just throwing something out here that got me way back. Your code here:

var myData:Array = new Array();

...

myData = JSON.parse(event.target.data);

You typed myData as an Array but JSON.parse() returns an Object. That should cause trouble and unless I'm not aware of a non-documented update, it may try to best handle it by considering the entire object returned as a single index (e.g. myData[0]). Check the return type of JSON.parse() here:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html#parse()

If you want to parse that object into an array, typically provide a reviver function that parses the object into a better structure.

DataProvider constructors do however use a list, XML or array of objects to construct.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/data/DataProvider.html#DataPro...()

As long as your data is formed properly you should just be able to:

data_grid.dataProvider = new DataProvider(event.target.data);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013

I'm going over this now.

At the other end, I'm saving the contents of the datagrid to a txt file, using:

var saveString:String = JSON.stringify(data_grid.dataProvider.toArray());

The data it's saving is like this (complete with square brackets):

[{"Delete":"X","Genre":"none","Duet":""}]

Back to your post, when I load the txt file data back in, this is what I use:

_myData = JSON.parse(event.target.data);// add string data from file to array as objects

_songListDP = new DataProvider(_myData);//add array to dataprovider

data_grid.dataProvider = _songListDP;

From memory, I made _myData an array, as an object was screwing things up, but maybe I'm storing the data in the txt file incorrectly in the first place (causing the object screw up)?

Does my process look incorrect somewhere?  Should the square brackets be in the txt file data?

Thanks Sinious

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 28, 2013 Jan 28, 2013

Just open a new AS3 doc (FP11+) and make a string with the data your JSON.stringify() is making and test a JSON.parse() on it directly to an Array:

var str:String = '[{"Delete":"X","Genre":"none","Duet":""}]';

var obj:Array = JSON.parse(str);

trace(obj);

You get the error:

Scene 1, Layer 'Layer 1', Frame 1, Line 21118: Implicit coercion of a value with static type Object to a possibly unrelated type Array.

Because JSON.parse() returns an "Object" and you're assigning it to an "Array".

Even though you typed _myData as an Array, and your JSON is technically an array notation of JSON, the JSON.parse() function does not support returning it as an Array, thus the error above.

Luckily the JSON.parse() will return the object in a notation you can use like an array. Consider the changed source here:

var str:String = '[{"Delete":"X","Genre":"none","Duet":""}]';

var obj:Object = JSON.parse(str);

trace(obj[0].Delete + ", " + obj[0].Genre + ", " + obj[0].Duet);

Trace:

X, none,

Bottom line, no need to make the array (it's not even valid), just assign the dataProvider at the same time you parse it.

data_grid.dataProvider = new DataProvider(JSON.parse(event.target.data));

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013

Thank you Sinious - I'll do that when I wake up. It might fix up some other issues I was having too.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 28, 2013 Jan 28, 2013

You're welcome, nite nite .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 28, 2013 Jan 28, 2013

Thanks Sinious - that worked straight off.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 29, 2013 Jan 29, 2013
LATEST

You're welcome and good luck!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines