<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>Noel Rocha &#187; Tutoriais</title>
	<atom:link href="http://www.noelrocha.com/blog/category/tutoriais/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.noelrocha.com/blog</link>
	<description>Capacitando o sistema...</description>
	<pubDate>Sun, 07 Jun 2009 22:09:50 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tabela de Produto com preço total gerado a cada seleção</title>
		<link>http://www.noelrocha.com/blog/2009/06/07/tabela-de-produto-com-preco-total-gerado-a-cada-selecao/</link>
		<comments>http://www.noelrocha.com/blog/2009/06/07/tabela-de-produto-com-preco-total-gerado-a-cada-selecao/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 20:38:59 +0000</pubDate>
		<dc:creator>Noel Rocha</dc:creator>
		
		<category><![CDATA[Dicas]]></category>

		<category><![CDATA[Programação]]></category>

		<category><![CDATA[Tutoriais]]></category>

		<category><![CDATA[DOM]]></category>

		<category><![CDATA[Faculdade]]></category>

		<category><![CDATA[HTML]]></category>

		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.noelrocha.com/blog/?p=331</guid>
		<description><![CDATA[Esse foi um exercício bastante interessante que tive semana passada na faculdade durante a aula de Linguagem de Script do professor Carlos Sicsu. Nesse aplicativo que faremos será utilizado as tecnologias HTML, DOM e JAVASCRIPT.
O enunciado do exercício era o seguinte:
Construir uma página que contenha uma tabela chamada &#8220;produto-preco&#8221;. A cada selecao de um produto, [...]]]></description>
			<content:encoded><![CDATA[<p>Esse foi um exercício bastante interessante que tive semana passada na faculdade durante a aula de <strong>Linguagem de Script</strong> do professor<a title="Currículo Lattes" href="http://buscatextual.cnpq.br/buscatextual/visualizacv.jsp?id=K4701776P6" target="_blank"> Carlos Sicsu</a>. Nesse aplicativo que faremos será utilizado as tecnologias <strong>HTML</strong>, <strong>DOM </strong>e <strong>JAVASCRIPT</strong>.</p>
<p>O enunciado do exercício era o seguinte:<br />
<em>Construir uma página que contenha uma tabela chamada &#8220;produto-preco&#8221;. A cada selecao de um produto, o preco total de compra deve ser visualizado ao final da tabela. O preço poderá conter casas decimais.<br />
</em></p>
<p>Criando função <em>calc_value_of_selected_products()</em> em JavaScript que calculará o preço total dos produtos:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">function</span> calc_price_of_selected_products<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// Obtendo formulário que contém os checkboxes com o valor dos produtos</span>
	<span style="color: #003366; font-weight: bold;">var</span> products    <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;product&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #006600; font-style: italic;">// Preço será do tipo float por causa dos produtos que podem ter</span>
	<span style="color: #006600; font-style: italic;">// centavos em seu valor</span>
	<span style="color: #003366; font-weight: bold;">var</span> total_price <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Verificando quais checkboxes foram selecionados e somando seu valor;</span>
  	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> products.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>products<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">checked</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			total_price <span style="color: #339933;">=</span> total_price <span style="color: #339933;">+</span> parseFloat<span style="color: #009900;">&#40;</span>products<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Atualizando div que contém o preço total dos produtos</span>
	document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;total&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Total: R$ &quot;</span> <span style="color: #339933;">+</span> total_price.<span style="color: #660066;">toFixed</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Para que o valor em centavos fique com 2 casas decimais é necessário usar a função <strong>toFixed()</strong>. Exemplo de seu uso:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">parseFloat<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;10.2&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toFixed</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// 10.20</span>
parseFloat<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;100&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toFixed</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// 100.00</span></pre></div></div>

<p>O preço de cada produto ficará dentro do value em cada checkbox. O checkbox deverá chamar a função calc_price_of_selected_product() que criamos toda vez que for clicado nele.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;checkbox&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;135.00&quot;</span> onclick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;calc_price_of_selected_products()&quot;</span><span style="color: #339933;">&gt;</span></pre></div></div>

<p> A Construindo a estrutura da tabela em <strong>HTML</strong> com alguns produtos e seus respectivos preços:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;product&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">border</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Produtos<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>R$<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;checkbox&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;135.99&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;calc_price_of_selected_products()&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Oculos<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>135.99<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;checkbox&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;53.20&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;calc_price_of_selected_products()&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Sunga<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>53.20<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;checkbox&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;97.30&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;calc_price_of_selected_products()&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Biquini<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>97.30<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>A soma de todos os valores selecionados deverá ser exibido pela div total. Essa div deverá ser inserida após o formulário.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;total&quot;</span>&gt;</span>Total: R$ 0.00<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>O funcionamento desse script pode ser visto no endereço abaixo:<br />
<a href=" http://www.noelrocha.com/blog/examples/tabela_produtos_preco.html" target="_new"> http://www.noelrocha.com/blog/examples/tabela_produtos_preco.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.noelrocha.com/blog/2009/06/07/tabela-de-produto-com-preco-total-gerado-a-cada-selecao/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

