From 6b8a8d6a05903334b2153943c2fc7a1da24348ab Mon Sep 17 00:00:00 2001 From: fomichev Date: Mon, 18 Nov 2024 18:00:08 +0300 Subject: [PATCH] =?utf8?q?=D0=98=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?utf8?q?=D0=B8=D0=B5=20=D1=82=D0=BE=D0=B2=D0=B0=D1=80=D0=BE=D0=B2=20?= =?utf8?q?=D1=82=D0=B5=20=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D0=B5=20?= =?utf8?q?=D1=83=D0=B6=D0=B5=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?utf8?q?=D0=BD=D1=8B=20=D0=B8=D0=B7=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA?= =?utf8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../crud/Product1cReplacementController.php | 34 ++++++++--- .../crud/product1c-replacement/_form.php | 15 ++++- .../crud/product1c-replacement/multy-form.php | 19 +++++- .../web/js/crud/product1cReplacement/_form.js | 60 ++++++++++++------- .../crud/product1cReplacement/multy-form.js | 23 +++++-- 5 files changed, 113 insertions(+), 38 deletions(-) diff --git a/erp24/controllers/crud/Product1cReplacementController.php b/erp24/controllers/crud/Product1cReplacementController.php index 4f26b9f1..042988bb 100644 --- a/erp24/controllers/crud/Product1cReplacementController.php +++ b/erp24/controllers/crud/Product1cReplacementController.php @@ -177,18 +177,36 @@ class Product1cReplacementController extends Controller ]); } - public function actionSearch($q = null) + public function actionSearch($q = null, $exclude = null, $guid = null) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; - $products = Products1c::find() - ->select(['id', 'name', 'articule']) // Добавляем 'articule' + + $query = Products1c::find() + ->select(['id', 'name', 'articule']) ->where(['tip' => 'products']) - ->andWhere(['ilike', 'name', $q]) - ->limit(20) - ->asArray() - ->all(); + ->andFilterWhere(['ilike', 'name', $q]) + ->limit(20); + + + if ($exclude) { + $excludeIds = explode(',', $exclude); + $query->andWhere(['not in', 'id', $excludeIds]); + } + + + if ($guid) { + $existingReplacements = Product1cReplacement::find() + ->select('guid_replacement') + ->where(['guid' => $guid]) + ->column(); + if ($existingReplacements) { + $query->andWhere(['not in', 'id', $existingReplacements]); + } + } + + $products = $query->asArray()->all(); $items = []; foreach ($products as $product) { @@ -200,6 +218,8 @@ class Product1cReplacementController extends Controller return ['items' => $items]; } + + /** * Updates an existing Product1CReplacement model. * If update is successful, the browser will be redirected to the 'view' page. diff --git a/erp24/views/crud/product1c-replacement/_form.php b/erp24/views/crud/product1c-replacement/_form.php index f52415b4..ff411109 100644 --- a/erp24/views/crud/product1c-replacement/_form.php +++ b/erp24/views/crud/product1c-replacement/_form.php @@ -57,7 +57,20 @@ $this->registerJsFile('/js/crud/product1cReplacement/_form.js', ['position' => \ 'ajax' => [ 'url' => \yii\helpers\Url::to(['/crud/product1c-replacement/search']), 'dataType' => 'json', - 'data' => new JsExpression('function(params) { return {q:params.term}; }'), + 'data' => new JsExpression('function(params) { + var exclude = []; + exclude.push($("#product1creplacement-guid").val()); + $(".guid-replacement-select").each(function() { + if ($(this).val()) { + exclude.push($(this).val()); + } + }); + return { + q: params.term, + exclude: exclude.join(","), + guid: $("#guid-select").val() + }; + }'), 'processResults' => new JsExpression('function(data) { return { results: $.map(data.items, function(item) { diff --git a/erp24/views/crud/product1c-replacement/multy-form.php b/erp24/views/crud/product1c-replacement/multy-form.php index f3487daa..73165498 100644 --- a/erp24/views/crud/product1c-replacement/multy-form.php +++ b/erp24/views/crud/product1c-replacement/multy-form.php @@ -19,7 +19,7 @@ $this->registerJsFile('/js/crud/product1cReplacement/multy-form.js', ['position' 'product-replacement-form']); ?> - + 'guid-select']) ?> field($model, 'guid')->widget(Select2::class, [ 'value' => $model->id, 'options' => ['placeholder' => 'Выберите товар...', 'id' => 'product-replacement-form-guid'], @@ -48,7 +48,7 @@ $this->registerJsFile('/js/crud/product1cReplacement/multy-form.js', ['position'
$replacement): ?> replacementProduct ? $replacement->replacementProduct->name : 'Неизвестно'; ?>
@@ -62,7 +62,20 @@ $this->registerJsFile('/js/crud/product1cReplacement/multy-form.js', ['position' 'ajax' => [ 'url' => \yii\helpers\Url::to(['/crud/product1c-replacement/search']), 'dataType' => 'json', - 'data' => new JsExpression('function(params) { return {q:params.term}; }'), + 'data' => new JsExpression('function(params) { + var exclude = []; + exclude.push($("#guid-select").val()); + $(".guid-replacement-select").each(function() { + if ($(this).val()) { + exclude.push($(this).val()); + } + }); + return { + q: params.term, + exclude: exclude.join(","), + + }; + }'), 'processResults' => new JsExpression('function(data) { return { results: $.map(data.items, function(item) { diff --git a/erp24/web/js/crud/product1cReplacement/_form.js b/erp24/web/js/crud/product1cReplacement/_form.js index 88918ef3..de94ed4f 100644 --- a/erp24/web/js/crud/product1cReplacement/_form.js +++ b/erp24/web/js/crud/product1cReplacement/_form.js @@ -1,49 +1,67 @@ $(document).ready(function () { let counter = 1; - // Добавление нового поля - $('#add-guid-replacement').on('click', function () { - counter++; - let newField = ` - -
- - - -
- - `; - $('#guid-replacement-container').append(newField); - // Инициализация Select2 с использованием AJAX для нового поля - $(`#product1creplacement-guid_replacement-${counter}`).select2({ + function initializeSelect2(selector) { + $(selector).select2({ placeholder: 'Выберите замену...', allowClear: true, minimumInputLength: 1, ajax: { - url: '/crud/product1c-replacement/search', // Замените URL на ваш реальный путь поиска + url: '/crud/product1c-replacement/search', dataType: 'json', data: function (params) { - return { q: params.term }; // Передача поискового термина + var exclude = []; + exclude.push($("#product1creplacement-guid").val()); + $(".guid-replacement-select").each(function () { + if ($(this).val()) { + exclude.push($(this).val()); + } + }); + console.log(exclude); + return { + q: params.term, + exclude: exclude.join(",") + }; }, processResults: function (data) { + console.log(data); return { results: $.map(data.items, function (item) { return { id: item.id, - text: item.name + text: item.text }; }) }; } } }); + } + + + $('#add-guid-replacement').on('click', function () { + counter++; + let newField = ` +
+ + + +
+ `; + $('#guid-replacement-container').append(newField); + + + initializeSelect2(`#product1creplacement-guid_replacement-${counter}`); }); - // Удаление поля + $(document).on('click', '.remove-guid-replacement', function () { let target = $(this).data('target'); - $(target).remove(); // Удаляем группу полей + $(target).remove(); }); + + + initializeSelect2('.guid-replacement-select'); }); \ No newline at end of file diff --git a/erp24/web/js/crud/product1cReplacement/multy-form.js b/erp24/web/js/crud/product1cReplacement/multy-form.js index a71e8580..37a2849f 100644 --- a/erp24/web/js/crud/product1cReplacement/multy-form.js +++ b/erp24/web/js/crud/product1cReplacement/multy-form.js @@ -13,7 +13,7 @@ function initReplacementHandlers() { `; $('#guid-replacement-container').append(newField); - // Инициализация Select2 для нового поля + $(`#product1creplacement-guid_replacement-${counter}`).select2({ placeholder: 'Выберите замену...', allowClear: true, @@ -21,13 +21,24 @@ function initReplacementHandlers() { ajax: { url: '/crud/product1c-replacement/search', dataType: 'json', - data: function (params) { - return { q: params.term }; + data: function(params) { + var exclude = []; + exclude.push($("#guid-select").val()); + $(".guid-replacement-select").each(function() { + if ($(this).val()) { + exclude.push($(this).val()); + } + }); + return { + q: params.term, + exclude: exclude.join(","), + + }; }, processResults: function (data) { return { results: $.map(data.items, function (item) { - return { id: item.id, text: item.name }; + return { id: item.id, text: item.text }; }) }; } @@ -53,7 +64,7 @@ function initReplacementHandlers() { success: function(data) { console.log(data); if (data.success) { - // Перезагрузка через Pjax после успешного удаления + $.pjax.reload({container: '#pjax-container', async: false}); } else { alert(data.message || 'Ошибка при удалении записи.'); @@ -66,7 +77,7 @@ function initReplacementHandlers() { }); } -// Инициализация обработчиков при загрузке страницы + $(document).ready(function () { initReplacementHandlers(); $(document).on('pjax:end', function () { -- 2.39.5