我有一个填充有文本文件的数据网格,其中包含逐行单词(即每行一个)。数据网格有 5 列。文本文件中的每 5 个单词被放入一个新行。然后可以添加或删除数据网格。我将如何使用数据网格中的所有值重写文本文件?
这就是填充数据网格的方式。如何遍历整个数据网格单元格并重写文本文件中的数据?
var loadFavourites: URLLoader = new URLLoader();;
var arFavourites = new Array();
loadFavourites.addEventListener(Event.COMPLETE, onLoadedFavourites);
loadFavourites.load(new URLRequest("lists/Favourites.txt"));
function onLoadedFavourites(event:Event):void
{
arFavourites = event.target.data.split("\r\n");
for (var i:int = 0; i <= arFavourites.length; i++)
{
if (i != 0 && i % 5 == 0)
{
dg.addItem({Place: arFavourites[i-5],Subject:arFavourites[i-4],Object:arFavourites[i-3],Feeling:arFavourites[i-2],Action:arFavourites[i-1]});
}
}
}
添加记录:
var i:int=0;
var tPlace = mc_places.mc_places_text.txtPlaces.text;
var tSubject = mc_subject.mc_subject_text.txtSubject.text;
var tObject = mc_object.mc_object_text.txtObject.text;
var tFeeling = mc_feeling.mc_feeling_text.txtFeeling.text;
var tAction = mc_action.mc_action_text.txtAction.text;
arFavourites[FavCount+1]=tPlace;
arFavourites[FavCount+2]=tSubject;
arFavourites[FavCount+3]=tObject;
arFavourites[FavCount+4]=tFeeling;
arFavourites[FavCount+5]=tAction;
dg.addItem({Place: arFavourites[FavCount+1],Subject:arFavourites[FavCount+2],Object:arFavourites[FavCount+3],Feeling:arFavourites[FavCount+4],Action:arFavourites[FavCount+5]});
dg.scrollToIndex(dg.length-1);
FavCount=FavCount+5;
for (i=0; i<arFavourites.length; i++) {
trace(arFavourites[i]);
}
删除记录:
var k:int;
var i:int;
trace("Selected Index: " + dg.selectedIndex);
if (dg.length == 0) {
return;
}
for (k=0; k<dg.length; k++) {
if (dg.isItemSelected(dg.getItemAt(k))) {
for (i=0; i<arFavourites.length; i++) {
if (arFavourites[i] == dg.getItemAt(k)[1]) {
arFavourites.splice(i,1);
}
if (arFavourites[i] == dg.getItemAt(k)[2]) {
arFavourites.splice(i,1);
}
if (arFavourites[i] == dg.getItemAt(k)[3]) {
arFavourites.splice(i,1);
}
if (arFavourites[i] == dg.getItemAt(k)[4]) {
arFavourites.splice(i,1);
}
if (arFavourites[i] == dg.getItemAt(k)[5]) {
arFavourites.splice(i,1);
}
}
dg.removeItemAt(k);
dg.scrollToIndex(k);
dg.clearSelection();
break;
}
}