Autor Zpráva
PetrQ
Profil
Hezký večer,
prosím o radu. používám tento jQuery pro přidání řádku tabulky - formuláře, spolu s php - viz dole.
Všechno funguje, ale potřeboval bych, aby se každý další řádek tabulky vytvořil bez zadaných údajů, tedy v tomto případě bez
hodnoty zadané do name='language[$i][poc]'.
Momentálně se mi vytvoří nový řádek, obsahující zadané hodnoty z řádku předcházejícího.
Díky všem za pomoc! Petr


// JavaScript Document
$(document).ready(function(){
//Setting some variables needed, don't edit these
	var displaySubmit=false, newRowCount=0;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Define some variables - edit to suit your needs
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	var maxRows=15;
	var rowSpeed = 300;
	var $table = $("#myTable");
	var $tableBody = $("tbody",$table);
	var $addRowBtn = $("#controls #addRow");
	var $removeAllBtn= $("#controls #removeAllRows");
	var $hiddenControls = $("#controls .hiddenControls");
	var blankRowID = "blankRow";
	var newRowClass = "newRow";
	var oddRowClass = "rowOdd";
	var evenRowClass = "rowEven"
	var hiddenClass = "visible"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//click the add button
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	$addRowBtn.click(function(){
		if(newRowCount < maxRows){
			newRowCount++;
			//get the class on the first row...
			if($tableBody.find("tr:first-child").hasClass(evenRowClass)){
				//the curent first row is even, so we add an odd class
				newClasses = hiddenClass+" "+newRowClass+" "+oddRowClass;
			}else{
				//the current first row is odd, so we add an even row
				newClasses = hiddenClass+" "+newRowClass+" "+evenRowClass;
			}
			//clone the blank row, put the clone at the top, set the correct classes, remove the ID, animate the divs inside
			//normally I'd use .addClass(), but I want the classes I set to replace the current set classes, so I use .attr("class") to overwrite the classes
			newRow = $("#"+blankRowID,$tableBody).clone().prependTo($tableBody).attr("class",newClasses).removeAttr("id").show().find("td div").slideDown(rowSpeed,function(){
				//run this once animations finish
				showHideSubmit();
			});

			//Add click event to the remove button on the newly added row
			newRow.find(".removeRow").click(function(){
				thisRow = $(this).parents("tr");
				rowRemoved=false;
				newRowCount--;
				//animate the row
				thisRow.find("td div").slideUp(rowSpeed,function(){
					//this is run once the animation completes
					if(!rowRemoved){ //this only lets it fire once per row
						thisRow.remove();
						showHideSubmit();
						//make sure alternating rows are correct once a row is removed
						$tableBody.find("tr:odd").removeClass(evenRowClass).addClass(oddRowClass); //odd rows have an odd class
						$tableBody.find("tr:even").removeClass(oddRowClass).addClass(evenRowClass);//even rows have an even class
						if(newRowCount < maxRows){
							$addRowBtn.removeAttr("disabled");//re-enable the add button
						}
					}
					rowRemoved=true;
				}); 
				return false; //kill the browser default action
			});
		}
		//disable button so you know you've reached the max
		if(newRowCount >= maxRows){
			$addRowBtn.attr("disabled","disabled");//set the "disabled" property on the button
		}
		return false; //kill the browser default action
	});
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


	
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Click the remove all button
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	$removeAllBtn.click(function(){
		//Close all the newly created rows
		$tableBody.find("tr."+newRowClass+"").each(function(){
			newRowCount--;
			showHideSubmit();
			//this happens once for every div that slides - no harm done though
			$(this).find("td div").slideUp(rowSpeed,function(){
				$(this).parents("."+newRowClass).remove(); //remove this row
				$addRowBtn.removeAttr("disabled"); //re-enable the add button
			});
		});
		return false;
	});
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Function to show or hide the submit/cancel buttons
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	function showHideSubmit(){
		if(newRowCount > 0 && !displaySubmit){
				//at least one new row is visible, show the hidden controls
				$hiddenControls.fadeIn(rowSpeed);
				//partially fade out all rows that are not new and disable links within
				//$tableBody.find("tr:not(#"+blankRowID+", ."+newRowClass+")").fadeTo(rowSpeed,0.25, function(){
				//	$(this).find("a").click(function(){return false;});//makes all clicked links go nowhere
				//});
				displaySubmit= true;
		}else if(newRowCount <= 0){
			//no new rows are shown, hide the controls
			$hiddenControls.fadeOut(rowSpeed);
			//fade old rows back in and re-enable links
			$tableBody.find("tr:not(#"+blankRowID+", ."+newRowClass+")").fadeTo(rowSpeed,1,function(){
				$(this).find("a").unbind("click");//removes the click event we site above
			});
			newRowCount=0;//Make sure the count is reset to 0...just in case
			displaySubmit= false;
		}
	}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
});


Stránka php vypadá takto:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" media="scReen" href="css/style.css"/>

<script type="text/javascript" src="menu.js"></script>

<!-- Zaváděcí skript pro funkce jQuery -->
<script language="javascript" type="text/javascript" src="js/jquery.js"></script>
<!-- LIGHTBOX pro jQuery -->
<script type="text/javascript" src="../js/jquery.lightbox-0.5.js"></script>
<script type="text/javascript" src="../js/jquery_lightbox.js"></script>

<!-- Javascript pro schování / zobrazení části stránky -->
<script language="javascript" type="text/javascript" src="../js/ukaz_skryj.js"></script>
<!-- Tento Javascript se stará o tlačítko "Nechci přidat obrázek". Přesměruje na udanou URL -->
<SCRIPT LANGUAGE="JavaScript">
<!--
function JdiNa() { window.location = "index.php?clanek=zeptejtese"; }
// -->
</script>

<!-- Javascript pro přidání řádku tabulky -->
<script language="javascript" type="text/javascript" src="js/pridej_radek.js"></script>




<title>Guitar Cases</title>
</head>
<body>
<?

echo "<font color='#CC3300' size='5'>Objednávka</font><br /><br />";

echo"<form method='POST'>";


echo"<table id='myTable' cellspacing='0'>


<font color=\"#CC3300\" size='2'>Položky objednávky</font>
<br /><br />
<thead>
<tr style='text-align:center;'>
<th style='width:20px;'>#</th><th style='width:200px;'>Typ</th><th style='width:180px;'>Provedení</th>
<th style='width:15px;'>Počet</th>
</tr>

<tr height='5'></tr>
</thead>

<tbody>";


/*This is your 'row template' that is cloned and put at the top when the add button is clicked.
Be sure that EVERY table cell's contents is wrapped in a div tag!
*/
echo"<tr id='blankRow'>
<td><div><button class='negative removeRow' title='Remove' style='background:#E5DCB9; border: none;'><font color='#CC3300' size='2'>X</font></button></div></td>
<td><div><select name='language[$i][typ]' style='width:200px;'>
<option value='Akustické klasická kytara'>Akustické - klasická kytara</option>
<option value='Akustické dreadnought'>Akustické - dreadnought</option>
<option value='Akustické jumbo'>Akustické - jumbo</option>
<option value='Elektrické Strat a Tele'>Elektrické - Strat / Tele</option>
<option value='Elektrické LP a PRS'>Elektrické - LP / PRS</option>
<option value='Jazzové 16 palců'>Jazzové 16 \" ( 41 cm )</option>
<option value='Jazzové 17 palců'>Jazzové 17 \" ( 43 cm)</option>
<option value='Atypické'>Atypické</option>
</select>
</td>
<td><select name='language[$i][mod]' style='width:180px;'>
<option value='Standard'>Standard</option>
Toto téma je uzamčeno. Odpověď nelze zaslat.