All pastes #931592 Raw Copy code Copy link Edit

new

public text v1 · immutable
#931592 ·published 2008-03-07 08:37 UTC
rendered paste body
<html>
<head>
	<script>
		var ClonerClass = function()
		{
			this.Data = new Array();
			this.CurrentObject = new Object();
			this.Attributes = [ 'id' ];
			this.cloneNode = function( Data, Node, TargetNode )
			{
				this.Data = Data;
				var clonedNode = Node.cloneNode( true );
				TargetNode.appendChild( clonedNode );
				this.replaceVars( clonedNode );
				return clonedNode;
			}

			this.replaceVars = function( Node )
			{
				if( !Node.innerHTML )
				{
					var html = Node.nodeValue;
        				for( key in this.Data )
					{
						html = html.replace( '%' + key + '%', this.Data[key] );
					}
					Node.nodeValue = html;
				}
				if( Node.attributes && Node.attributes.length )
				{
        				for( var i=0; i<this.Attributes.length; i++ )
					{
						if( Node.attributes[this.Attributes[i]] )
						var html = Node.attributes[this.Attributes[i]].nodeValue; 
        					for( key in this.Data )
						{
							html = html.replace( '%' + key + '%', this.Data[key] );
						}
						Node.attributes[this.Attributes[i]].nodeValue = html;
					}
				}
				  
				if( Node.childNodes && Node.childNodes.length )
				{
					for( var i=0; i<Node.childNodes.length; i++ )
					{
						this.replaceVars( Node.childNodes[i] );	
					}
				}
			}
		}

		var Cloner = new ClonerClass();




		function addItem( data )
		{
			var clonedNode = Cloner.cloneNode( data, document.getElementById( 'exampleItem' ), document.getElementById( 'exampleItem' ).parentNode );
			clonedNode.id = 'item_' + data['index'];
			clonedNode.style.display = '';
		}


		function fillItems()
		{
			var data = { 'index': '454000', 'name': 'Пазл', 'count': 2 };
			addItem( data );
			var data = { 'index': '800000', 'name': 'Булка', 'count': 5 };
			addItem( data );
		}
	</script>
</head>
<body onLoad="fillItems();">
	<table border="1" id="table">
		<tr>
			<th>Индекс</th>
			<th>Наименование</th>
			<th>Количество</th>
		</tr>
		<tr id="exampleItem" style="display: none;">
			<td id="item_%index%">Индекс ему %index%</td>
			<td>Имя ему %name%</td>
			<td>Количество ему %count%</td>
		</tr>
	</table>
</body>
</html>