在Extjs树中限制子项长度

时间:2011-03-16 22:29:04

标签: javascript extjs

我想将子节点的长度限制为20个字符,例如xxxxxxxxxxxxxxxxxxxx ......

树的数据将来自Json格式的数据库。这是我的代码。

Ext.onReady(函数(){

var Tree = Ext.tree;

var tree = new Tree.TreePanel({
    useArrows: true,
    autoScroll: true,
    animate: true,
    enableDD: true,
    containerScroll: true,
    border: false,
    // auto create TreeLoader
    dataUrl: 'test.json',

    root: {
        nodeType: 'async',
        text: 'Ext JS',
        draggable: false,
        id: 'src'
    }
});

// render the tree
tree.render('tree-div');
tree.getRootNode().expand(); });

1 个答案:

答案 0 :(得分:3)

为此,您需要定义自定义树加载器并覆盖createNode函数:

var tree = new Tree.TreePanel({
    useArrows: true,
    autoScroll: true,
    animate: true,
    enableDD: true,
    containerScroll: true,
    border: false,

    loader: new Ext.tree.TreeLoader({
        dataUrl: this.dataUrl,
        createNode: function(attr) {
            attr.text = attr.text.substr(0, 20);
            Ext.tree.TreeLoader.superclass.createNode.call(this, attr);
        }
    }),

    root: {
        nodeType: 'async',
        text: 'Ext JS',
        draggable: false,
        id: 'src'
    }
});