Я абсолютный новичок и yii2 и в php, но есть стремление изучать... очень нужна ваша помощь...
Мне не понятно как я могу привязать селект к гридвью, то есть чтобы при выборе покупателя, таблица бы обновилась со всеми заказами этого покупателя через pjax.
Select
Код: Выделить всё
<?php
try {
echo Select2::widget([
'id'=>'customer',
'name' => 'customer',
'data' => \yii\helpers\ArrayHelper::map(\app\models\Customers::find()->all(), 'id', 'name'),
'maintainOrder' => true,
'options' => [
'placeholder' => 'Customer ...',
'multiple' => true,
],
'pluginOptions' => [
'tags' => true,
'maximumInputLength' => 10
],
]);
} catch (Exception $e) {
throw new RuntimeException ($e->getMessage());
}
?>
Код: Выделить всё
$js = <<<JS
$("#customer").on('change', function()
{
const customer = $('#customer').val();
if (customer !== 0){
$.ajax({
url: 'index.php?r=orders',
dataType: 'html',
method: 'GET',
data: {id: customer},
success: function (data, textStatus, jqXHR) {
$.pjax.reload({container:'#pjaxContainer'});
console.log('success');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('An error occured!');
}
});
}
}
);
JS;
$this->registerJs($js, yii\web\View::POS_READY);
Код: Выделить всё
<?php \yii\widgets\Pjax::begin([
'id'=>'pjaxContainer',
]); ?>
<?php
try {
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//'rowOptions' =>$searchModel->id,
'headerRowOptions'=>['class'=>'kartik-sheet-style'],
'columns'=>[
['class' => 'yii\grid\SerialColumn'],
['class' => 'yii\grid\CheckboxColumn'],
'id',
'quantity',
'products_id',
'customers_id',
[
'attribute' => 'products_id',
'label' => 'Name of product',
'value' => 'products.name',
],
[
'attribute' => 'products_id',
'label' => 'Description',
'value' => 'products.description',
],
[
'attribute' => 'customers_id',
'label' => 'Name',
'value' => 'customers.name',
],
[
'attribute' => 'customers_id',
'label' => 'Lastname',
'value' => 'customers.lastname',
],
],
]);
} catch (Exception $e) {
throw new RuntimeException($e->getMessage());
}
?>
<?php \yii\widgets\Pjax::end() ?>
Код: Выделить всё
public function actionIndex()
{
$searchModel = new OrdersSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}