使用来自数据网格的数据填充组合框,从sqlite数据库接收数据

时间:2013-04-29 03:14:16

标签: sqlite datagrid air combobox flex3

我仍然是Adobe Air / Flex的新手,而且还是SQL的新手。

我已经下载了这个(http://coenraets.org/blog/2008/11/using-the-sqlite-database-access-api-in-air ... -part-1 /)代码并且一直在查看它,我正在尝试实现不同的想法。

我正在使用这种形式的画布。

MaterialForm.mxml由面板CadastroMaterial.mxml调用,该面板还显示一个数据网格,其中包含从此表单保存的信息。

sql语句由MaterialEvent.as使用lib flexlib。

创建

我在从数据网格到组合框中获取数据时遇到问题,我这样做是为了获取数据:

<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" width="388" height="475"     creationComplete="init()" label="{_material.material_id > 0 ? _material.tipo : 'Novo Material'}">

<mx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.managers.PopUpManager;
        import Eventos.MaterialEvent;

        import Eventos.TMaterialEvent;
    import Formularios.TMaterialForm;
    import mx.collections.ArrayCollection;

    [Bindable] private var tMateriais:ArrayCollection;
    // Para criar a conexão com o banco de dados
    private function openDatabase():void
    {
        var file:File = File.applicationDirectory.resolvePath("bd/SISC.db");
        sqlConnection = new SQLConnection();
        sqlConnection.open(file);
        // Para verificar se o banco de dados já existe se sim utiliza-o senão cria um novo, não é útil se não cria a tabela
        //var isNewDB:Boolean = !file.exists;
        //if (isNewDB) createDatabase();

        findAll();
    }

    // Para selecionar a tabela do banco de dados desejada
    private function findAll():void
    {
        var stmt:SQLStatement = new SQLStatement();
        stmt.sqlConnection = sqlConnection;
        stmt.text = "SELECT tmaterial FROM TMATERIAL";
        stmt.execute();
        tMateriais = new ArrayCollection(stmt.getResult().data);
    }





        [Bindable] public var _material:Object;

        public var sqlConnection:SQLConnection;

        private var validators:Array;

        private function init():void
        {
            validators = [tipoValidator, responsavelValidator, compartimentoValidator];
            openDatabase();// colocado para abrir o banco de outro lugar
        }

        public function set material(material:Object):void
        {
            _material = material;
        }

        public function get material():Object
        {
            return _material;
        }

        private function save():void
        {
            if (Validator.validateAll(validators).length>0)
            {
                return;
            }

            _material.tipo = tipo.text;
            _material.num = num.text;
            _material.responsavel = responsavel.text;
            _material.compartimento = compartimento.text;
            _material.observacoes = observacoes.text;
            if (_material.material_id > 0)
            {
                update();
            }
            else
            {
                insert();
            }
        }

        private function insert():void
        {
            try 
            {
                var stmt:SQLStatement = new SQLStatement();
                stmt.sqlConnection = sqlConnection;
                stmt.text = 
                    "INSERT INTO material (tipo, num, responsavel, compartimento, observacoes) " +
                        "VALUES (:tipo, :num, :responsavel, :compartimento, :observacoes)"; 
                stmt.parameters[":tipo"] = _material.tipo; 
                stmt.parameters[":num"] = _material.num; 
                stmt.parameters[":responsavel"] = _material.responsavel; 
                stmt.parameters[":compartimento"] = _material.compartimento; 
                stmt.parameters[":observacoes"] = _material.observacoes;
                stmt.execute();
                _material.material_id = stmt.getResult().lastInsertRowID;
                label = _material.tipo;
                dispatchEvent(new MaterialEvent(MaterialEvent.CREATE, _material, true));
            }
            catch (error:SQLError)
            {
                Alert.show(error.details, "Erro");
            }
        }

        private function update():void
        {
            try
            {
                var stmt:SQLStatement = new SQLStatement();
                stmt.sqlConnection = sqlConnection;
                stmt.text = 
                    "UPDATE material set " + 
                        "tipo=:tipo, " + 
                        "num=:num, " +
                        "responsavel=:responsavel, " +
                        "compartimento=:compartimento, " +
                        "observacoes=:observacoes " +
                        "WHERE material_id=:materialId";
                stmt.parameters[":tipo"] = _material.tipo; 
                stmt.parameters[":num"] = _material.num; 
                stmt.parameters[":responsavel"] = _material.responsavel; 
                stmt.parameters[":compartimento"] = _material.compartimento; 
                stmt.parameters[":observacoes"] = _material.observacoes; 
                stmt.parameters[":materialId"] = _material.material_id; 
                stmt.execute();
                label = _material.tipo;
                dispatchEvent(new MaterialEvent(MaterialEvent.UPDATE, _material, true));
            }
            catch (error:SQLError)
            {
                Alert.show(error.details, "Error");
            }
        }

        private function deleteItem():void
        {
            try 
            {
                var stmt:SQLStatement = new SQLStatement();
                stmt.sqlConnection = sqlConnection;
                stmt.text = "DELETE FROM material WHERE material_id = :materialId";
                stmt.parameters[":materialId"] = _material.material_id; 
                stmt.execute();
                dispatchEvent(new MaterialEvent(MaterialEvent.DELETE, _material, true));
            }
            catch (error:SQLError)
            {
                Alert.show(error.details, "Erro");
            }
        }

    ]]>
</mx:Script>

<mx:Validator id="tipoValidator" required="true" source="{tipo}" property="text"/>
<mx:Validator id="responsavelValidator" required="true" source="{responsavel}" property="text"/>
<mx:Validator id="compartimentoValidator" required="true" source="{compartimento}" property="text"/>

<mx:Form width="381" height="466">
    <mx:FormItem label="Tipo:" required="true">
        <mx:ComboBox dataProvider="{tMateriais}" id="tipo" text="{_material.tipo}" width="200"/>
    </mx:FormItem>
    <mx:FormItem label="Número:">
        <mx:TextInput id="num" text="{_material.num}" width="200"/>
    </mx:FormItem>
    <mx:FormItem label="Responsavel:" required="true">
        <mx:TextInput id="responsavel" text="{_material.responsavel}" width="200"/>
    </mx:FormItem>
    <mx:FormItem label="Compartimento:" required="true">
        <mx:TextInput id="compartimento" text="{_material.compartimento}" width="200"/>
    </mx:FormItem>
    <mx:FormItem label="Observações:">
        <mx:TextInput id="observacoes" text="{_material.observacoes}" width="200"/>
    </mx:FormItem>

</mx:Form>  

<mx:Button label="Salvar" click="save()" left="16" bottom="20"/> 
<mx:Button label="Deletar" click="deleteItem()" left="87" bottom="20"/> 

但结果出现在组合框[object Object]而不是表格TMATERIAL中插入的材料类型的名称,如果我在另一种形式的表格中插入30个值,则30在组合框中显示为[object Object]我跟踪了这​​个,错误就是这种形式。谁能帮助我?对不起英语(谷歌翻译)。日Thnx。

1 个答案:

答案 0 :(得分:0)

问题是ComboBox需要提示如何显示dataProvider中每个项目的名称。默认情况下,许多Flex组件会在dataProvider中查找具有名为label的属性的元素。如果此类属性存在且为String,则ComboBox将显示该label属性的值。

如果dataProvider中的元素没有label属性,那么Flex组件将在toString()对象上调用dataProvider,在这种情况下会导致输出“[object Object]”。

如果dataProvider中的元素没有label属性,那么您可以通过使用ComboBox的labelFieldlabelFunction属性告诉ComboBox如何显示名称

使用labelField属性指定dataProvider中可用作标签的属性名称(在您的情况下,我认为这是tipo或{{1} })

:tipo

或者使用<mx:ComboBox dataProvider="{tMateriais}" id="tipo" labelField="tipo" /> 属性指定一个函数,该函数将用于为labelFunction中的每个项生成标签。

dataProvider

<mx:ComboBox dataProvider="{tMateriais}" id="tipo" labelFunction="myLabelFunction" /> 具有以下方法签名:

labelFunction