Показать сообщение отдельно
  #10 (permalink)  
Старый 31.05.2018, 14:30
Интересующийся
Отправить личное сообщение для bipib Посмотреть профиль Найти все сообщения от bipib
 
Регистрация: 04.12.2017
Сообщений: 13

Может кому будет интересно. Вот решение моей задачи
это html
<tbody>
													<?php $project_image_row = 0; ?>
													<?php foreach ($project_images as $project_image) { ?>
													<tr id="project_image-row<?php echo $project_image_row; ?>">
													  <input type="hidden" class="project_image_id" value="<?php echo $project_image_row; ?>" />
													  <td class="text-left"><a href="" id="thumb-image<?php echo $project_image_row; ?>" data-toggle="image" class="img-thumbnail"><img src="<?php echo $project_image['thumb']; ?>" alt="" title="" data-placeholder="<?php echo $placeholder; ?>" /></a><input type="hidden" name="project_image[<?php echo $project_image_row; ?>][image]" value="<?php echo $project_image['image']; ?>" id="input-project_image<?php echo $project_image_row; ?>" /></td>
													  <td class="project-products">
													    <div class="form-group" id="product-to-project<?php echo $project_image_row; ?>" style="display:inherit">
															<label class="col-lg-2 col-md-2 col-sm-2 col-xs-12 control-label"><?php echo $entry_product; ?></label>
															<div class="col-lg-10 col-md-10 col-sm-10 col-xs-12 project-products-rows">
																<input type="text" name="productsInput" value="" class="form-control" />
																<div id="project-image-list<?php echo $project_image_row; ?>" class="well well-sm" style="height: 150px; overflow: auto; margin-bottom:0;">
																	<?php if(isset($project_image['products'])) { ?>
																		<?php foreach($project_image['products'] as $product) { ?>
																			<div id="product-to-project-list<?php echo $project_image_row; ?>_<?php echo $product['product_id']; ?>"><i class="fa fa-minus-circle"></i> <?php echo $product['name']; ?>
																				<input type="hidden" name="project_image[<?php echo $project_image_row; ?>][product_to_project][]" value="<?php echo $product['product_id']; ?>" />
																			</div>
																		<?php } ?>
																	<?php } ?>
																</div>
															</div>
														</div>
													  </td>
													  <td class="text-right"><input type="text" name="project_image[<?php echo $project_image_row; ?>][sort_order]" value="<?php echo $project_image['sort_order']; ?>" placeholder="<?php echo $entry_project_image_sort_order; ?>" class="form-control" /></td>
													  <td class="text-left"><button type="button" onclick="$('#project_image-row<?php echo $project_image_row; ?>').remove();" data-toggle="tooltip" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>
													</tr>
													<?php $project_image_row++; ?>
													<?php } ?>
												  </tbody>


а это js


var project_image_row = <?php echo $project_image_row; ?>;

	function addProjectImage() {
		html = '<tr id="project_image-row' + project_image_row + '">';
		html += '<input type="hidden" class="project_image_id" value="<?php echo $project_image_row; ?>" />';
		html += '  <td class="text-left"><a href="" id="thumb-image' + project_image_row + '" data-toggle="image" class="img-thumbnail"><img src="<?php echo $placeholder; ?>" alt="" title="" data-placeholder="<?php echo $placeholder; ?>" /></a><input type="hidden" name="project_image[' + project_image_row + '][image]" value="" id="input-project_image' + project_image_row + '" /></td>';
		html += '	<td><div class="form-group" id="product-to-project' + project_image_row + '" style="display:inherit">';
		html += '	<label class="col-lg-2 col-md-2 col-sm-2 col-xs-12 control-label"><?php echo $entry_product; ?></label>';
		html += '		<div class="col-lg-10 col-md-10 col-sm-10 col-xs-12">';
		html += '			<input type="text" name="productsInput" value="" class="form-control" />';
		html += '			<div id="project-image-list' + project_image_row + '" class="well well-sm" style="height: 150px; overflow: auto; margin-bottom:0;">';
		html += '			</div>';
		html += '		</div>';
		html += '	</div></td>';
		html += '  <td class="text-right"><input type="text" name="project_image[' + project_image_row + '][sort_order]" value="" placeholder="<?php echo $entry_sort_order; ?>" class="form-control" /></td>';
		html += '  <td class="text-left"><button type="button" onclick="$(\'#project_image-row' + project_image_row  + '\').remove();" data-toggle="tooltip" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';
		html += '</tr>';

		$('#project_images tbody').append(html);

		project_image_row++;
		initializeAutocomplete();
	}
	
	var initializeAutocomplete = function () {
		
	var currentProductsInputObject;
	$('input[name=\'productsInput\']').keydown(function() {
		currentProductsInputObject = $(this);
	});
	$('input[name=\'productsInput\']').click(function() {
		currentProductsInputObject = $(this);
	});

		$("input[name=\'productsInput\']").autocomplete({
			source: function(request, response) {
				$.ajax({
					url: 'index.php?route=catalog/product/autocomplete&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request),
					dataType: 'json',
					success: function(json) {
						response($.map(json, function(item) {
							return {
								label: item['name'],
								value: item['product_id']
							}
						}));
					}
				});
			},
			select: function(item) {
				var project_image_id = currentProductsInputObject.parents('tr').find('.project_image_id').val();
				
				$('input[name=\'project_image['+project_image_id+'][product_to_project]\']').val('');

				$('#product-to-project-list'+project_image_id+'_' + item['value']).remove();

				$('#project-image-list'+project_image_id+'').append('<div id="product-to-project-list'+project_image_id+'_' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="project_image['+project_image_id+'][product_to_project][]" value="' + item['value'] + '" /></div>');
			}
		});
		
		var project_image_id = currentProductsInputObject.parents('tr').find('.project_image_id').val();
		$('#project-image-list'+project_image_id+'').delegate('.fa-minus-circle', 'click', function() {
			$(this).parent().remove();
		});
		
		
	}
	initializeAutocomplete();


при динамическом добавлении строк в каждой из них срабатывает autocomplete
Ответить с цитированием