从ExtJS中的表中获取第一行的值时出错

时间:2017-02-22 09:42:59

标签: extjs combobox

我是ExtJS的新手。所以很难理解很多事情。

我有一个示例组合框,我从表中检索数据

这是组合框:

xtype: 'fieldset',
    title: 'Dress Type',
    items: [{
        xtype: 'combobox',
        name: 'dresses',
        forceNewStore: true,
        queryMode: 'local',
        displayField: "description",
        valueField: "description",
        mapperId: 'getavailabletype',
        emptyText: 'Select Type
        forceSelection: true,
        maskRe: /[A-Za-z0-9]/,
        margin: '15px',
        allowBlank: false,
        triggers: {
            clear: {
                cls: 'x-form-clear-trigger',
                handler: function() {
                    this.reset();
                }
            }
        },
    }]

以下是我尝试保留表格第一个值的方法:     me.down("combobox[name=dresses]").setValue(me.down("combobox[name=dresses]").store.getAt('0').get('description'));

我得到了:

  

未捕获的TypeError:无法读取属性' get' null @getAt(' 0')

请帮忙

2 个答案:

答案 0 :(得分:1)

我不知道你试图通过什么样的听众和组件实现这一目标,但从我的角度来看,实现这一目标的最佳位置是组合框的“render”事件监听器。

像这样:

listeners: {
    'render': function(combo) {
        combo.setValue(combo.getStore().first());
    }
}

这里有一个工作小提琴:https://fiddle.sencha.com/#view/editor&fiddle/1qqk

如果你有一个对组合商店的引用,让我们说它在 comboStore 变量中,你可以像这样设置第一个值:

{
    xtype: 'combobox',
    value: comboStore.first()
}

另一个小提琴:https://fiddle.sencha.com/#view/editor&fiddle/1qql

现在,如果您在初始化组合时不确定是否会加载商店,则可以为组合框设置以下监听器:

listeners: {
    render: function(combo) {
        var store = combo.getStore(),
            setFirstStoreValFn = function() {
                combo.setValue(store.first());
            };

        if (store.getCount()) {
            setFirstStoreValFn();
        }
        else {
            store.on('load', setFirstStoreValFn, null, {single: true})
        }
    }
}

所以基本上,它创建了一个可重用的函数,它将组合值设置为商店中的第一个记录,然后它检查商店是否有记录(这意味着它已被加载)它将调用该函数,如果没有 - 它将在商店中设置一个加载侦听器,然后调用设置值函数。

再次,小提琴:https://fiddle.sencha.com/#view/editor&fiddle/1qqm

答案 1 :(得分:0)

你需要导航到cmp - >商店 - >第一条记录--->数据

代表public class CustomAdapter extends BaseAdapter { private Context ctx; private List<CustomList> list; public CustomAdapter(Context ctx, List<CustomList> list){ this.ctx = ctx; this.list = list; } @Override public int getCount() { return list.size(); } @Override public CustomList getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate(R.layout.layout_item, parent, false); } Button button = (Button) convertView.findViewById(R.id.button); button.setText(list.get(position)); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { \\change color here } }); return convertView; } }