- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我提前为代码格式道歉。我有这样的数据源:
var data = {
key1: 'value',
key2: 123,
key3: {
key30: 'value',
key31: {
key310: 123,
key311: 'hello'
}
}
}
使用 React,我尝试将其转换为 <ul>
像这样:
<ul id="data">
<li>key1: value</li>
<li>key2: 123</li>
<li>key3: [Object]
<ul>
<li>key30: value</li>
<li>key31: [Object]
<ul><li>key310: 123</li>
<li>key311: hello</li>
</ul>
</li>
</ul>
</li>
</ul>
到目前为止,我已经尝试过:
React.createClass({
getInitialState: function() {
return {lists: [{
key1: 'value',
key2: 123,
key3: {
key30: 'value',
key31: {
key310: 123,
key311: 'hello'
}
}
}]}
},
generateFirst: function(parent, data, tempContainer) {
var li
var liHead
var wrap
var key
for (key in data) {
if (this.getType(data[key]) === 'object') {
// li head
liHead = React.DOM.li({
className: 'token-head'
}, key + ':' + data[key])
// line
li = React.DOM.li({
className: 'iop'
}, React.DOM.ul({
className: 'iok'
}, this.generateFirst(data[key])))
// wrap
wrap = React.DOM.ul({
className: 'wrap-ul'
}, li)
tempContainer.push(liHead)
tempContainer.push(wrap)
} else {
li = React.DOM.li({
className: 'iol'
}, key + ':' + data[key])
tempContainer.push(li)
}
}
},
renderTokenContent: function(data) {
var tempContainer = []
this.generateFirst(data, tempContainer)
return (<ul>{tempContainer}</ul>)
},
render: function() {
var self = this
return (<ul className="scroll-helper">
{ this.state.lists.map(function(data) {
return (<li >
{ self.renderTokenContent(data) }
</li>)
}) }
</ul>)
}
})
它解析了整棵树,但没有在正确的容器中呈现元素。
最佳答案
这是来自 https://github.com/calitek/ReactPatterns 的嵌套树的示例React.14.Common/ TreeView 。请注意,在这种情况下,嵌套属性是子项。
import React from 'react';
import lodashGet from 'lodash/object/get';
let TreeRootSty = {lineHeight: '120%'}
let liSty = {listStyleType: 'none'};
let ulSty = {height: 'inherit', WebkitPaddingStart: '16px'};
let ulStyle = {height: 'inherit', WebkitPaddingStart: '16px'};
let iconSty = {marginRight: '10px', width: '16px'};
let nottogglable = {
color: '#FFF',
cursor: 'pointer',
margin: '0 0 0 .8em'
};
let togglable = {
color: '#815C7C',
cursor: 'pointer',
margin: '0'
};
let options = {};
let getTreeNode = function(child, index) {
return <li key={index} style={liSty}><JTreeViewNode node={child} iconClick={this.props.iconClick} titleClick={this.props.titleClick} /></li>;
};
class JTreeViewNode extends React.Component {
constructor(props) { super(); }
iconHandler = () => {
if (this.props.node.children && this.props.node.children.length > 0) {
this.props.iconClick(this.props.node);
} else {
this.clickHandler();
}
};
clickHandler = () => { this.props.titleClick(this.props.node); };
render() {
let titleSty = {color: '#afac87', marginTop: '2px'};
let childNodes;
let pSty = nottogglable;
if (this.props.node.children && this.props.node.children.length > 0) {
childNodes = this.props.node.children.map(getTreeNode, this);
titleSty.color = this.props.node.selected ? '#7BB53B' : '#AF90A5';
} else {
titleSty.color = this.props.node.selected ? '#b58900' : '#afac87';
}
let isClosed = true;
if (this.props.node.closed != null) isClosed = this.props.node.closed;
let branch = (
<ul id='ulStyle' style={ulStyle}>
{childNodes}
</ul>
)
if (isClosed) branch = null;
let props = this.props;
let iconType = lodashGet(props, options.typeName);
if (iconType == options.icon.sun) iconSty.background = "url('./img/sun.ico') 0/16px no-repeat !important";
else if (iconType == options.icon.leaf) iconSty.background = "url('./img/leaf.ico') 0/16px no-repeat !important";
else if (iconType == options.icon.snow) iconSty.background = "url('./img/snow.ico') 0/16px no-repeat !important";
else iconSty.background = "url('./img/sun.ico') 0/16px no-repeat !important";
return (
<div id='TreeNode'>
<div id='pSty' style={pSty} className='FlexBox'>
<div id='iconSty' onClick={this.iconHandler} style={iconSty}> </div>
<div id='titleSty' onClick={this.clickHandler} style={titleSty} >
{this.props.node.title}
</div>
</div>
{branch}
</div>
);
}
}
export default class JTreeView extends React.Component {
render() {
options = this.props.options;
let childNodes = this.props.data.map(getTreeNode, this);
return (
<div id='TreeRootSty' style={TreeRootSty}>
<ul id='ulSty' style={ulSty}>
{childNodes}
</ul>
</div>
);
}
}
关于javascript - react : recursively render nested elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34867236/
下面两个CSS选择器有什么区别? 来自解释here ,它们听起来一样吗? div p{} 选择div元素内的所有p元素 div > p{} 选择父级为 div 元素的所有 p 元素。 最佳答案 区别在
我需要怎么做: 目前,事实证明: 我更喜欢它看起来像: 最佳答案 有了这些空行,看起来您的 select 语句是(正确地)选择您
用替换元素是否有效至 .在这种情况下,我想要, 这样我就可以在...中附加验证数据。这也可以从 中实现吗?或 等等? 最佳答案 标签没有 而且不需要一个。同样适用于 和 .您可以将验证数据作为
我刚刚发现了 Angular 1.2.1 的一个奇怪问题,在 this fiddle 中进行了演示。 (在 IE、FF 和 Chrome 中测试):如果我创建一个非常简单的模板化指令,它无法像 那样
我正在尝试使用 Jsoup 迭代 java 中的两个元素,但是,我收到运行时异常错误。看来我无法将nodes.Element 的类型转换为element.Element。 非常感谢您的帮助,谢谢。 代
假设我的文档中有一组元素,它们是单个对象的子元素。这些元素中的每一个都使用不同的参数注册一个新的事件监听器。如果我丢弃父对象,是否需要手动取消注册所有 eventListener?或者浏览器是否跟踪所
我不应该在我的应用程序中使用 jQuery,但我有一个场景,我需要元素的偏移量,而不是使用 $(element).offset() 我已经使用了 angular.element(element).of
我是TS的新手,我想知道为什么我在以下代码中遇到类型错误(简化):。错误在`{iconMap[名称]}中:。“元素隐式具有‘any’类型,因为‘字符串’类型的表达式不能用于索引类型‘{Categori
我是TS的新手,我想知道为什么我在以下代码中遇到类型错误(简化):。错误在`{iconMap[名称]}中:。“元素隐式具有”any“类型,因为”string“类型的表达式不能用于索引类型”{ Cate
什么意思: Separator.Iterator.Element == Self.Iterator.Element.Iterator.Element 在this (Swift 标准库)swift 实例
是否可以在 img 元素上使用前后伪选择器?认为它是但没有任何运气,将 css 切换到 div 并且它工作正常。 .page-overhang 类是 img 元素。 // page overhang
我在 UI 中的按钮 Click 事件上有以下代码,它返回一个 MS-Excel 文件。它在第一次点击事件中完美运行,但之后返回以下错误。 任何建议,我怎样才能摆脱它? ERROR: Uncaught
如何在 CSS 中编写这个想法: 选择 ElementA 内但不在 ElementB 内且 ElementB 在 ElementA 内的每个元素。 这是一个例子:
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
如果我在 C# 中的 XElement myXML 变量中有以下 XML, myvalue 要获得“myvalue”,我需要执行以下操作: myXML.Element(
我找不到用户名和密码字段。我检查元素,并尝试通过 id、xpath 或 css 选择器查找它,但它给出错误 NoSuchElementException: Message: no such eleme
我的任务是在用户点击它时从输入框中删除占位符并使标签可见。如果用户未在其中再次填写任何内容,请放回占位符并使标签不可见。 我可以隐藏它但不能重新分配它。我试过 element.setAttribute
我正在编写一个 c# 类来编写一个 XML 文件,该文件需要与我们使用的现有 XML 的结构完全匹配,这样一些遗留系统就不会混淆。 当一个元素的InnerText值为null时,我需要xml元素的元素
自定义元素的一个常见做法,至少在 Polymer(最流行的 Web 组件框架)中,是定义一个新的自定义元素。恕我直言,这对 来说不是一个好习惯呈现 元素,因为该元素无法逐步呈现,必须等到它被加载(注册
我正在尝试跟踪元素的可见性及其显示的控制台错误:“元素“a[data-vars-ei]”必须是 AMP 元素”。 但是在点击跟踪的情况下,类似的事情工作正常。 我无法理解为什么会发生这种情况以及我应该
我是一名优秀的程序员,十分优秀!