简体   繁体   中英

as3 delete row in datagrid to filter data

Im trying to filter a datagrid by deleting row if value of column is not equal to my input... But the if condition does not seem to work... Below is my code:

for(var k:int =0; k<DataGrid.length;k++){
    wew = DataGrid.getItemAt(k).Name.toString();
    if(txt_username.text != wew){
        trace("not equal");
        DataGrid.removeItemAt(k); // remove row if it does not equal to   name input by user
    }
    else{
        trace("same");
    }
    }
} 

Sample data :

John    val1    val2   val3
Awts    val1    val2   val3
Awts    val1    val2   val3

User input: "John"

Desired output:

John    val1   val2   val3

您甚至不必使用此删除数据,如果您需要过滤数据而不删除值,您可以使用数组集合的过滤器功能,并且您可以这样做,请尝试使用谷歌搜索。

You have some little problems in your code :

  • DataGrid is a class name which you can use to create an instance that you can use in your code, like this for example :

     var data_grid:DataGrid = new DataGrid(); addChild(data_grid);

    then you can use that instance :

     for(var k:int = 0; k < data_grid.length; k++){ // ... }
  • Your wow var should be a declared string :

     var wow:String = data_grid.getItemAt(k).Name.toString();

so your code can be like this :

for(var k:int = 0; k < data_grid.length; k++){
    var wew:String = data_grid.getItemAt(k).Name.toString();
    if(txt_username.text != wew){
        data_grid.removeItemAt(k);
        k --;
    }
} 

Hope that can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM