Добавяне на нов ред в таблица и запазване на зависимости

Post Reply
Knoppix_
Турист
Турист
Posts: 322
Joined: Mon Aug 24, 2009 11:55 am

Добавяне на нов ред в таблица и запазване на зависимости

Post by Knoppix_ » Mon Mar 07, 2016 6:56 pm

Здравейте имам следният проблем, таблица в която имам два селект бокса който са зависими и се зареждат от база данни, и два инпута идеята ми е че искам да клонирам реда т.е да добавя нов ред динамично но да запазя зависимоста в случая тази зависимост обаче я имам само на първото поле не на другите

ето js-a

Code: Select all

<script>
function addRow&#40;order_table&#41; &#123;
	var table = document.getElementById&#40;order_table&#41;;
	var rowCount = table.rows.length;
	if&#40;rowCount < 12&#41;&#123;							// limit the user from creating fields more than your limits
		var row = table.insertRow&#40;rowCount&#41;;
		var colCount = table.rows&#91;0&#93;.cells.length;
		for&#40;var i=0; i<colCount; i++&#41; &#123;
			var newcell = row.insertCell&#40;i&#41;;
			newcell.innerHTML = table.rows&#91;0&#93;.cells&#91;i&#93;.innerHTML;
		&#125;
	&#125;else&#123;
		 alert&#40;"Можете да добавяте не повече от 12 реда."&#41;;
			   
	&#125;
&#125;
</script>
Това е самата таблица

Code: Select all

<form>
<table id="order_table">
<tr>
<td><select name="glass" class="glass">
<option selected="selected">--Изберете стъклопакет--</option>
<?php
	$stmt1 = $DB_con->prepare&#40;"SELECT * FROM products"&#41;;
	$stmt1->execute&#40;&#41;;
	while&#40;$rows=$stmt1->fetch&#40;PDO&#58;&#58;FETCH_ASSOC&#41;&#41;
	&#123;
		?>
        <option value="<?php echo $rows&#91;'productID'&#93;; ?>"><?php echo $rows&#91;'productName'&#93;; ?></option>
        <?php
	&#125; 
?>
</select></td>
<td><select name="size" class="size">
<option selected="selected">--Изберете вид--</option>
</select></td>
<td><input type="text" name="height" id="height" /></td><td><input type="text" name="width" id="width" /></td>
<td>
<input type="button" onClick="addRow&#40;'order_table'&#41;" /></td>
</tr>
</table>
</form>

User avatar
lam3r4370
Шаман
Шаман
Posts: 3424
Joined: Wed Aug 20, 2008 4:12 pm
Answers: 131

Post by lam3r4370 » Mon Mar 07, 2016 6:58 pm

Т.е искаш да клонираш първия ред?
jQuery

Code: Select all

$&#40;'tr&#58;first'&#41;.clone&#40;&#41;.appendTo&#40;'table'&#41;

Knoppix_
Турист
Турист
Posts: 322
Joined: Mon Aug 24, 2009 11:55 am

Post by Knoppix_ » Mon Mar 07, 2016 7:04 pm

Ако го клонирам няма да ми се запази това ако избера дадена опция от първият селект да излизат във вторият само стойностите който отговарят за нея

User avatar
lam3r4370
Шаман
Шаман
Posts: 3424
Joined: Wed Aug 20, 2008 4:12 pm
Answers: 131

Post by lam3r4370 » Mon Mar 07, 2016 7:19 pm

Е, дай и остатъка от кода, не става щото използваш change примерно, а трябва да използваш $('table').on('change', 'select')

Knoppix_
Турист
Турист
Posts: 322
Joined: Mon Aug 24, 2009 11:55 am

Post by Knoppix_ » Mon Mar 07, 2016 7:27 pm

Code: Select all


<?php
include_once 'inc/dbcon.inc.php';
?>
<?php
session_start&#40;&#41;;
require_once 'inc/class.user.php';
$user_home = new USER&#40;&#41;;

if&#40;!$user_home->is_logged_in&#40;&#41;&#41;
&#123;
	$user_home->redirect&#40;'?page=request'&#41;;
&#125;
$stmt = $user_home->runQuery&#40;"SELECT * FROM tbl_users WHERE userID=&#58;uid"&#41;;
$stmt->execute&#40;array&#40;"&#58;uid"=>$_SESSION&#91;'userSession'&#93;&#41;&#41;;
$row = $stmt->fetch&#40;PDO&#58;&#58;FETCH_ASSOC&#41;;


$userID = $row&#91;'userID'&#93;;

?>

<?php echo $userID; ?>

<form name="order" action="post">


<table>
<tr>
<td align="center">Стъклопакет</td>
<td align="center">Размер</td>
<td align="center">Височина</td>
<td align="center">Широчина</td>
</tr>
</table>
<table id="order_table">
<tr>
<td><select name="glass" class="glass">
<option selected="selected">--Изберете стъклопакет--</option>
<?php
	$stmt1 = $DB_con->prepare&#40;"SELECT * FROM products"&#41;;
	$stmt1->execute&#40;&#41;;
	while&#40;$rows=$stmt1->fetch&#40;PDO&#58;&#58;FETCH_ASSOC&#41;&#41;
	&#123;
		?>
        <option value="<?php echo $rows&#91;'productID'&#93;; ?>"><?php echo $rows&#91;'productName'&#93;; ?></option>
        <?php
	&#125; 
?>
</select></td>
<td><select name="size" class="size">
<option selected="selected">--Изберете вид--</option>
</select></td>
<td><input type="text" name="height" id="height" /></td><td><input type="text" name="width" id="width" /></td>
<td>
<input type="button" onClick="addRow&#40;'order_table'&#41;" /></td>
</tr>
</table>
</form>

Това е заявката

А това е депент на селект бокса

Code: Select all

<?php
require_once&#40;"dbcon.inc.php"&#41;;
if&#40;$_POST&#91;'id'&#93;&#41;
&#123;
	$id=$_POST&#91;'id'&#93;;
		
	$stmt = $DB_con->prepare&#40;"SELECT * FROM products_size WHERE sizeProduct=&#58;id"&#41;;
	$stmt->execute&#40;array&#40;'&#58;id' => $id&#41;&#41;;
	?><option selected="selected">Избери размер</option><?php
	while&#40;$row=$stmt->fetch&#40;PDO&#58;&#58;FETCH_ASSOC&#41;&#41;
	&#123;
		?>
        <option value="<?php echo $row&#91;'sizeName'&#93;; ?>"><?php echo $row&#91;'sizeName'&#93;; ?></option>
        <?php
	&#125;
&#125;
?>
Ето го и js

function addRow(order_table) {
var table = document.getElementById(order_table);
var rowCount = table.rows.length;
if(rowCount < 12){ // limit the user from creating fields more than your limits
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells.innerHTML;
}
}else{
alert("Можете да въведете не повече от 12 полета.");

}
}

User avatar
lam3r4370
Шаман
Шаман
Posts: 3424
Joined: Wed Aug 20, 2008 4:12 pm
Answers: 131

Post by lam3r4370 » Mon Mar 07, 2016 7:33 pm

Не, не, дай JS кода

Knoppix_
Турист
Турист
Posts: 322
Joined: Mon Aug 24, 2009 11:55 am

Post by Knoppix_ » Mon Mar 07, 2016 7:40 pm

Ето джавата на на зависимите селект боксове

Code: Select all

$&#40;document&#41;.ready&#40;function&#40;&#41;
&#123;
	$&#40;".glass"&#41;.change&#40;function&#40;&#41;
	&#123;
		var id=$&#40;this&#41;.val&#40;&#41;;
		var dataString = 'id='+ id;
	
		$.ajax
		&#40;&#123;
			type&#58; "POST",
			url&#58; "inc/get_state.php",
			data&#58; dataString,
			cache&#58; false,
			success&#58; function&#40;html&#41;
			&#123;
				$&#40;".size"&#41;.html&#40;html&#41;;
			&#125; 
		&#125;&#41;;
	&#125;&#41;;

User avatar
lam3r4370
Шаман
Шаман
Posts: 3424
Joined: Wed Aug 20, 2008 4:12 pm
Answers: 131

Post by lam3r4370 » Mon Mar 07, 2016 7:52 pm

Това и ти написах, промени просто
$(".glass").change(function() {//...})
на

Code: Select all

$&#40;'table'&#41;.on&#40;'change', '.glass', function&#40;&#41; &#123;//...&#125;&#41;
Така слушаш за change на .glass за цялата таблица и като добавяш нови редове в нея динамично няма да е проблем :)

Knoppix_
Турист
Турист
Posts: 322
Joined: Mon Aug 24, 2009 11:55 am

Post by Knoppix_ » Tue Mar 08, 2016 3:32 am

Не сега е още по зле изобщо няма зависимост

Post Reply