I would like to render JQuery UI sortable list (I am using Yii2 JUI widget), to be populated with data from database table. I am passing data from database to the view and have it in a variable. The item HTML structure looks like this:
<div class="row row-item dist simple-border">
<div class="col-md-4">
<img alt="Bootstrap Image Preview" src="<IMAGE_PATH>" />
</div>
<div class="col-md-8">
<h4>
<ITEM DESCRIPTION>
</h4>
</div>
</div>
My question is: What is the best way to populate items inside the Widget, as it is impossible to execute php code within.
<?= yii\jui\Sortable::widget([
'items' => [
------<expected foreach php loop for each model>------
['content' =>
<This is where one HTML item goes>
],
------<end foreach>------
],
'options' => ['class' => 'connectedSortable',],
'clientOptions' => ['cursor' => 'move', 'connectWith' => '.connectedSortable']
]) ?>
My first idea was to create a function that returns item content as a string, but it doesn't seem like a good, efficient practice. Any ideas would be greatly appreciated. Regards.
EDIT: So far, I have created a function, which renders mentioned above items by concatenation. Still, I don't think this is the proper way to do it. I am still new to Yii and would be thankful for any tips. Regards.
I have found such solution for my problem, feel free to review and improve it.
Controller
public function actionIndex()
{
$searchModel = new Exchange();
$dataProvider = $searchModel->getOwnerItems(Yii::$app->request->queryParams);
$ownerItems = $this->getOwnerItems($dataProvider);
return $this->render('index', [
'ownerItems' => $ownerItems,
]);
}
protected function getOwnerItems($dataProvider){
$items=array();
foreach($dataProvider->models as $model){
$item=array('content' => '<div class="row row-item dist simple-border"> <div class="col-md-4"> <img alt="Bootstrap Image Preview" src="uploads/'.$model->image.'" /> </div> <div class="col-md-8"> <h4>'. $model->name .'</h4> </div> </div>');
array_push($items,$item);
}
return $items;
}
View
<?= yii\jui\Sortable::widget([
'items' => $ownerItems,
'options' => ['class' => 'connectedSortable',],
'clientOptions' => ['cursor' => 'move', 'connectWith' => '.connectedSortable']
]) ?>
I am aware of possible imperfection of this solution, but I have no other ideas at the moment. Regards
Related
Each User has multi profiles. once they logged in, they are asked to select a profile,
here is the code for Form to select profile.
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-default">
<div class="panel-heading">
<h3 align ="center" class="panel-title">Select Profiles</h3>
</div>
<div class="panel-body">
<?php
foreach($resJacs->{'details'} as $key) {
echo form_open('selectaccess', array(
'class' => 'form-group',
'role' => 'form'
));
echo form_submit(array(
'value' => $key->profile_name,
'name' => $key->profile_type,
'class' => 'btn btn-lg btn-default btn-block'
));
echo form_close();
}
?>
</div>
</div>
</div>
</div>
when user selects the profile profile id is passed to session for later use. here is the code for "selectaccess",
public function SelectAccess() {
$sess_data = array(
'id' => $this->session->userdata['is_logged_in']['id'],
'prfid' => $this->input->post('')
);
print_r($sess_data);
}
how can i prfid as mentioned in selectaccess method.
I just want to give an example, maybe this can help u :
I am use pure html .
<form action="SelectAccess/<?php echo $id; ?>">
<input type="text" name="name">
<button type="submit">Submit</button>
</form>
Controller
function SelectAccess($val='') {
$input = $this->input->post('name');
$_SESSION['whatever'] = $val;
}
Normally you need to know the name of the field in order to get the value at the controller. But you are dynamically creating the field names so that gets tricky.
Fortunately you are only posting one input so $_POST should have only one item. The way your view is written the value of $_POST[0] will be provided by $key->profile_name. Hopefully, that value is what you are looking for.
public function SelectAccess() {
{
$sess_data = array(
'id' => $this->session->userdata['is_logged_in']['id'],
'prfid' => isset($_POST[0])) ? $_POST[0] : NULL;
);
}
when I click on the action of a controller and i disable pagination, the web page won't load...
the problem is when i click on the action controller which is some sort of:
public function actionMyview(){
return $this->render("//myview/index");
}
in the view code I'm making some sort of MVVM (i'm calling the ActiveDataProvider into it):
<?php
set_time_limit(0);
use yii\helpers\Html;
use yii\grid\GridView;
use frontend\models\myModel;
use yii\web\View;
use yii\data\ActiveDataProvider;
$dataProvider2 = new ActiveDataProvider([
'query' => myModel::find(),
'pagination' => false,
]);
$this->title = 'Listing 3 thousand records';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="ventas-model-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create manually a new record', ['create'], ['class' => 'btn btn-success col-xs-4 col-md-4 col-sm-4 col-lg-4']) ?>
<div class="col-xs-3 col-md-3 col-sm-3 col-lg-3" >
<input type="text" class="form-control" id="search-criteria" placeholder="please write a text in order to search" width=""/>
</div>
<span class="col-xs-5 col-md-5 col-sm-5 col-lg-5"> </span>
<?php
$this->registerJs("
var rows = jQuery('.table tr');
$('#search-criteria').keyup(function() {
var val = jQuery.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
rows.show().filter(function() {
var text = jQuery(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
",
View::POS_END,
'ventanamodal001');
?>
</p>
<?php echo GridView::widget([
'dataProvider' => $dataProvider2,
'columns' => [
[
'attribute' => 'date',
'format' => ['date', 'php:d/m/Y']
],
["label"=>'First Column',"attribute"=>"first","value"=>function($data0){
return myModel::aBolivares($data0->first);
}],
["label"=>'second Column',"attribute"=>'second',"value"=>function($data0){
return myModel::aBolivares($data0->second);
}],
["label"=>"totals","value"=>function($data0){
$totals=$data0->first+$data0->secod;
return myModel::aBolivares($totals);
},"format"=>"html","enableSorting"=>true,"attribute"=>"first"],
['label'=>'description',
'value'=>function ($data){
return empty($data->Observ)?" ":"<p class='glyphicon glyphicon-eye-open'></p>";
},
"format"=>"html","attribute"=>"Observ"],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
the code sample will iterate into a yii2 ActiveDataProvider object given to the yii\grid\GridView;... when I upload into a remote location it will fail: (I'm using HostGator shared hosting).
IT'S IMPORTANT TO SAY: IT WILL FAIL WHEN GIVEN MORE THAN 3 THOUSAND RECORDS == 3000 records directly from DB. If you use let's say 1500 records it will slow down but will work... only when injecting a high volume of records will fail...
I'm Having a bit of a issue with Depdrop from Kartik-v and Dynamic Form from Wbraganca. The problem is when I load the update form, the data isn't loaded into the second dynamic field. Until i add a new row then the data is shown.
.
It doesn't give any error. All I want is for the data to be shown on the second row and so on.
Code in the form
<?php $catList=ArrayHelper::map(Hs::find()->all(), 'hscode', 'hscode' );?>
<?= $form->field($modelsItems, "[{$i}]hscode")->dropDownList($catList, ['id'=>'cat-id','prompt'=>'Select...']);?>
</div>
<div class="col-sm-6" style="width: 200px">
<?= $form->field($modelsItems, "[{$i}]hsproduct")->widget(Depdrop::classname(), [
'options'=>['id'=>'product-id'],
'pluginOptions'=>[
'placeholder' => FALSE,
'initialize' => true,
'depends'=>['cat-id'], // the id for cat attribute
'url'=> Url::to(['hs/subcat']),
]
]);
?>
</div>
<div class="col-sm-6" style="width: 170px">
<?php $companylist=ArrayHelper::map(Company::find()->all(), 'company', 'company' );?>
<?= $form->field($modelsItems, "[{$i}]company")->dropDownList($companylist, ['id'=>'company-id','prompt'=>'Select...']);?>
</div>
<div class="col-sm-6" style="width: 170px">
<?= $form->field($modelsItems, "[{$i}]variety")->widget(DepDrop::classname(), [
'options'=>['id'=>'cp-varitey'],
'pluginOptions'=>[
'placeholder' => FALSE,
'initialize' => true,
'depends'=>['company-id'], // the id for cat attribute
'url'=> Url::to(['company/subcat2'])
]
])->label('Variety');
?>
</div>
For those who having this same problem i solve it by changing the id. The first ID was cat-id but when you create a new dynamic field the id then change to cat-0--id for some reason. The way i solve it was by changing the id to this
'id'=>"cat-".$i."--id" and should do this for the rest of the id. My product id now is 'id'=>"product-".$i."--id".
I hope this helps some one. Thanks
I using form type in symfony 2.8.x.
I need form attribute text in view.
form type:
$builder->add( 'category_id', ChoiceType::class,
array( 'label'=>'Category',
'constraints'=>array( new Regex(array("pattern"=>"'([^0-9]*)$'si",
"message"=>"Required field!"))
),
'choices'=>$categoriesRepo->getAllActive(),
'choices_as_values'=>true,
'choice_label' => 'getName',
'choice_value' => 'getId',
'attr'=>array( 'class'=>'form-control',
'help'=>'Help message.',
)));
In view:
<?php echo $view->render("XXXBundle:XXX:form_element.html.php", array('form'=>$templateForm['category_id']))?>
form_element.html.php
<div class="form-group">
<?php echo $view['form']->label($form, null, array('required'=>false, 'label_attr'=>array('class'=>'col-md-3 control-label'))) ?>
<div class="col-md-4">
<?php echo $view['form']->widget($form, array('attr'=>array('help'=>false))) ?>
<span class="help-block error"><?php echo $view['form']->errors($form) ?></span>
<span class="help-block"> HELP_MESSAGE </span>
</div>
</div>
I need "help" attribute to HELP_MESSAGE in view.
There is a solution to this problem?
Thank you!
You are the first Symfony user I've ever seen who renders PHP files instead of twig. Congratulations.
The real business part of your code you haven't posted, so it's hard to give a definitive answer, but somewhere at the bottom of your controller's action function there will be a call to $this->render('XXXBundle:XXX:template.html.php');
The 'Symfony way' is to pass the help message in that:
$help_message = 'My Help Message';
$this->render('XXXBundle:XXX:template.html.php', array('help_message' => $help_message));
In your template file:
<?php echo($view['help_message']);?>
I'm developing app with Yii framework. I need from dropdown list with checkboxes as values of this list. I've been searching about this but didn't find anything. Can someone help me with this task?
Here is the link to a Yii extension which does exactly what you want!
Yii Framework Extension: echmultiselect
On top of that, there are other Yii extensions for multiselect features that are also quite interesting! you might also want to take a look at these as well!
Yii Framework Extension: Select2
Yii Framework Extension: emuliselect
You can do it like this (in this example your model must have a public field $availableLanguages):
<?= $yourActiveForm->field($model, 'availableLanguages', ['template' => "
<div class='dropdown'>
<button
class='btn btn-default dropdown-toggle'
data-toggle='dropdown'
type='button'>
<span>Select languages</span>
<span class='caret'></span>
</button>
{input}
</div>"])->checkboxList(
[
'FR'=>'France',
'DE'=>'Germany'
],
[
'tag' => 'ul',
'class' => 'dropdown-menu',
'item' => function ($index, $label, $name, $checked, $value) {
return '<li>' . Html::checkbox($name, $checked, [
'value' => $value,
'label' => Html::encode($label),
]) . '</li>';
}
]); ?>
You can create a simple textbox and when the user clicks you display a list of checkboxes
<div id="listOfDays">
<span><?php echo Yii::t('frontend','CHOOSE_DAY'); ?> </span>
</div>
<div id="itemslistOfDays" style="display:none;position:absolute;z-index:10;background-color:white; width:300px">
<?php
echo $form->checkBoxList($model, 'freq_details', array(
'1'=>Yii::t('frontend','MONDAY'),
'2'=>Yii::t('frontend','TUESDAY'),
'3'=>Yii::t('frontend','WEDNESDAY'),
'4'=>Yii::t('frontend','THURSDAY'),
'5'=>Yii::t('frontend','FRIDAY'),
'6'=>Yii::t('frontend','SATURDAY'),
'7'=>Yii::t('frontend','SUNDAY'),
)); ?>
</div>
You will have to write some javascript or jquery in order to hide,show and bind click event