<dd id="bt93c"><center id="bt93c"></center></dd>
    1. <th id="bt93c"></th>
          1. <rp id="bt93c"><acronym id="bt93c"><u id="bt93c"></u></acronym></rp>

            EXTJS7 treestore.fillNode 追加多個節點時報錯

            小編:管理員 215閱讀 2022.09.07

            版本

            7.x

            現象

            使用treestore.fillNode(node,newNodes)接口向樹節點追加孩子節點,如果newNodes數組元素是js對象而非Model對象,數組有多個元素時會報錯

            TypeError: Cannot read property ‘index’ of undefined

            如果newNodes數組只有一個元素時則不會報錯

            解決

            先使用NodeInterface.createNode接口將js對象轉為Model對象

            for(i = 0; i < newNodes.length; i++){
            	newNodes[i] = node.createNode(newNodes[i]);
            }
            treestore.fillNode(node, newNodes);
            復制源碼解析
            • ext-core/src/data/TreeStore.js
            fillNode: function(node, newNodes) {
                var me = this,
                    newNodeCount = newNodes ? newNodes.length : 0;
            
                // If we're filling, increment the counter so nodes can react without doing
                // expensive operations
                if (++me.bulkUpdate === 1) {
                    me.suspendEvent('datachanged');
                }
            
                if (newNodeCount) {
                    me.setupNodes(newNodes);
                    node.appendChild(newNodes, undefined, true);
                }
                // only set loaded if there are no newNodes;
                // appendChild already handles updating the loaded status, 
                // and will do it *after* the child nodes have been added
                else {
                    if (me.bulkUpdate === 1) {
                        node.set('loaded', true);
                    }
                    else {
                        node.data.loaded = true;
                    }
                }
            
                if (!--me.bulkUpdate) {
                    me.resumeEvent('datachanged');
                }
            
                // No need to call registerNode here, because each child will register itself as it joins
                return newNodes;
            },
            
            setupNodes: function(newNodes) {
                var me = this,
                    sorters = me.getSorters(),
                    needsIndexSort = false,
                    newNodeCount = newNodes.length,
                    /* eslint-disable-next-line max-len */
                    performLocalSort = me.sortOnLoad && newNodeCount > 1 && !me.getRemoteSort() && me.getFolderSort() || sorters.length,
                    performLocalFilter = me.needsLocalFilter(),
                    node1, node2, i;
            
                // Apply any local filter to the nodes as we fill
                if (performLocalFilter) {
                    me.doFilter(newNodes[0]);
                }
            
                // See if there are any differing index values in the new nodes. If not, then we do not
                // have to sortByIndex
                // 如果傳入的記錄元素數量大于1會執行此邏輯進行排序
                for (i = 1; i < newNodeCount; i++) {
                    node1 = newNodes[i];
                    node2 = newNodes[i - 1];
            
                    // Apply any filter to the nodes as we fill
                    if (performLocalFilter) {
                        me.doFilter(node1);
                    }
            		// 此處根據傳入的記錄判斷排序,但是傳入的js對象并不一定有data字段,則拋出異常,如果使用Model數組則無此問題
                    needsIndexSort = node1.data.index !== node2.data.index;
                }
            
                // If there is a set of local sorters defined.
                if (performLocalSort) {
                    // If sorting by index is needed, sort by index first
                    me.needsIndexSort = true;
                    Ext.Array.sort(newNodes, me.getSortFn());
                    me.needsIndexSort = false;
                }
                else if (needsIndexSort) {
                    Ext.Array.sort(newNodes, me.sortByIndex);
                }
            },
            復制
            • ext-core/src/NodeInterface.js`
            appendChild: function(node, suppressEvents, commit) {
            	if (Ext.isArray(node)) {
            	    ln = node.length;
            	    result = new Array(ln);
            	    // Suspend view updating and data syncing during update
            	    me.callTreeStore('beginFill');
            	
            	    for (i = 0; i < ln; i++) {
            	        result[i] = me.appendChild(node[i], suppressEvents, commit);
            	    }
            	
            	    // Resume view updating and data syncing after appending all new children.
            	    // This will fire the add event to any views (if its the top level append)
            	    me.callTreeStore('endFill', [result]);
            	}
            	else {
            	    // Make sure it is a record 此處將js對象轉換為Model
            	    node = me.createNode(node);
            復制
            關聯標簽:
            久久99久久99小草精品免费看

            <dd id="bt93c"><center id="bt93c"></center></dd>
            1. <th id="bt93c"></th>
                  1. <rp id="bt93c"><acronym id="bt93c"><u id="bt93c"></u></acronym></rp>