【问题标题】:I needed to use the pop() to take out a option in a comboBox (array) that i have created...but was stuck我需要使用 pop() 在我创建的组合框(数组)中取出一个选项......但被卡住了
【发布时间】:2012-11-07 20:27:39
【问题描述】:

我需要使用 pop() 来取出数组中的最后一个元素(位于组合框中),但这不起作用...请帮助!

附言当我运行代码时,我没有收到任何错误,其他一切正常,按钮也是如此:

我的代码:

var barColours:Array = new Array();
barColours  = ["red", "green", "blue", "orange", "purple"];

comboBox.dataProvider = new DataProvider(barColours);
comboBox.addEventListener(Event.CHANGE, onChange);
comboBox.x = 670;
comboBox.y = 55;

minus_btn.addEventListener(MouseEvent.CLICK, takeAwayCol);

function takeAwayCol(ev:MouseEvent):void
{
      barColours.pop();
} 

【问题讨论】:

    标签: actionscript-3 flash flash-cs5


    【解决方案1】:

    修改数组不会“通知”您需要重新分配 dataProvider 的更改列表。

    var barColours:Array = new Array();
    barColours  = ["red", "green", "blue", "orange", "purple"];
    
    comboBox.dataProvider = new DataProvider(barColours);
    comboBox.addEventListener(Event.CHANGE, onChange);
    comboBox.x = 670;
    comboBox.y = 55;
    
    minus_btn.addEventListener(MouseEvent.CLICK, takeAwayCol);
    
    function takeAwayCol(ev:MouseEvent):void
    {
          barColours.pop();
          comboBox.dataProvider = new DataProvider(barColours);
    } 
    

    已编辑每条评论

    var barColours:Array = ["red", "green", "blue", "orange", "purple"];
    var removedBarColours:Array = new Array();
    
    comboBox.dataProvider = new DataProvider(barColours);
    comboBox.addEventListener(Event.CHANGE, onChange);
    comboBox.x = 670;
    comboBox.y = 55;
    
    minus_btn.addEventListener(MouseEvent.CLICK, takeAwayCol);
    
    function takeAwayCol(ev:MouseEvent):void
    {
    //if you need to pull from the beginning of the list instead of the end use shift/unshift method(s) of Array
          removedBarColours.push(barColours.pop());
          comboBox.dataProvider = new DataProvider(barColours);
    } 
    
    function addCol(ev:MouseEvent):void
    {
          barColours.push(removedBarColours.pop());
          comboBox.dataProvider = new DataProvider(barColours);
    } 
    

    【讨论】:

    • 对不起,还有没有办法点击另一个按钮将那些已经被删除的元素添加回数组的末尾?
    • 没问题,是的,您必须保留另一个包含已删除值的数组,以便将它们从一个列表转移到另一个列表,我会尽快更新上面的代码。
    猜你喜欢
    • 2016-04-28
    • 2021-07-24
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多