Answer the question
In order to leave comments, you need to log in
How to add data-attribute?
Hello everyone, I started studying yii not so long ago, the following task appeared ...
I have tables:
1) products - description of products
2) attributes - name of attributes, just name
3) attributes_products - linking table which has a column with product id, attribute and custom description text for each product attribute description is different
structure similar to opencart with attributes, the only difference is that you can add different attributes to opencart, and I need to fill in all attributes is mandatory, (these attributes can only be added and removed site admin)
so I need to make blocks dynamically formed on the add page for each attribute from the table,
should be of type label attribute name (table - attributes) and input in which there will be text (table - attributes_products), (dynamic input),
in the model I select everything like this
public function getAttributes()
{
foreach(Attribute::find()->all() as $one){
if (DrugsAttribute::find()->andWhere(['attribute_id' => $one->attribute_id, 'drugs_id' => $this->drugs_id])->exists()) {
// запись существует
$dAone = DrugsAttribute::find()->andWhere(['attribute_id' => $one->attribute_id, 'drugs_id' => $this->drugs_id])->one();
$arrayatt[] = array('attribute_id'=>$dAone->attribute_id, 'drugs_id'=>$dAone->drugs_id, 'text'=>$dAone->text, 'name'=>$one->name);
} else {
$arrayatt[] = array('attribute_id'=>$one->attribute_id, 'drugs_id'=>$this->drugs_id, 'text'=>'', 'name'=>$one->name);
}
}
return $arraysa;
}
foreach($model->attribute_array as $one){
echo $form->field($model, 'attribute_array')->textInput(['value' => $one["text"])->label($one["name"]);
}
$form->field($model, 'attribute_array')->textInput(['value' => $one["text"], 'options' =>['data-drugs_id' => $one["drugs_id"]]])->label($one["name"]);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question