How can I edit the _search view (which is implemented by Yii2 Crud Generator), so that I can make two form fields on the same line?
(Note that I have resized the form fields to 500px so that they can fit one line).
Also, I would like the 'Search' and 'Reset' buttons to be on the same line too.
You can use options and assign eg: a proper twitter-bootstrap grid width
this way :
<?php echo $form->field($model, 'your_filed', ['options' => ['class' => 'col-md-4', ]]) ?>
Related
Problem
This problem is regarding Yii2 project which should show relevant Recipient list when click on the User Group name.
At the moment I have created all the databases, models, CRUD generations with gridviews & working perfectly. But the problem is when I click on the grid item it navigates to another view (the default way) as following pic which I want to load in a part of the same view.
User-groups (view -> index.php)
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//navigate to the relevent recipient list by click on the group name
'rowOptions' => function ($model, $key, $index, $grid) {
return [
'style' => "cursor: pointer",
'id' => $model['group_id'],
'onclick' => 'location.href="'.Yii::$app->urlManager->createUrl('recipient-list/recipients').'&scenario=RECIPIENTS¶ms="+(this.id);',
];
},
'columns' => [
'group_id',
'group_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
I have given the rowOptions in the GridView so that the record can be clicked & navigate to the recipient list through the URL. Then that will be captured from the controller & filter the result according to group_id & render the view.
What I want
I want to dynamically load the recipient list when click on the left side group list.
I have tried
iFrames but it is not working properly. it works as a separate tab in the browser.
added many Yii navigation extensions like sideNavs by kartik for group list. But the problem was it haven't the ability to getting data from database. when I create a group it didn't show in the list. (It is good if the side nav was static)
So .. What I want to know is, is there are any way to do this with the controller or is there any Jquery code to support this...
any suggestions or references are warmly welcome.
you can use jQuery .load for this.
lets say your recipient list container div has a id of #recipient-list so all you have to do is add the following on your onclick event:
$( "#recipient-list" ).load("YOUR_URL"); return false;
// replace YOUR_URL with the url you are generating already
// and don't forget the return false part. it't there so that link doesn't redirect
When you click in the row, on click event
you will make an ajax request to retrieve the
data for the second grid (Recipient List)
on success or complete of ajax request
you will call $.pajax.reload to refresh
the data in second grid like this
$.pjax.reload({container:'#recipient-list'});
Using this code:
<?= $form->field($model, 'username') ?>
would return a specific HTML containing a set of divs, text etc.
Let's say I don't want to print out the label "Username" Above the input field, and maybe I want to add some HTML code for this input (maybe a div next to the input field..) what is the best way to do this?
Do I need to edit the class/file that is actually responsible for the output of this function?
The field above the 'username' is label you can change it by chaining lable() function
<?php $form->field($model, 'username')->textInput()->label('Add your lable here') ?>
Please click this link for more detail
The situation
I'm working on a large form which have been split into several tabs, both to group fields of a kind and make filling easier. The tabs are activated using JavaScript (jQuery) and the form is submitted whole (all tabs at once, even if there are blank fields).
I am trying to submit a hidden field along with the form to keep user's current tab. This way, when the form is submitted, data is saved and the user goes back to the very same tab it was before. This field is not attached to any Model, as it is just a helper.
The problem + what have I tried
The field is correctly populated by jquery as the user switch tabs, and it is submitted along with the form. This is what I did:
<?php
echo $this->Form->create('Briefing', array('enctype' => 'multipart/form-data'));
// This is my aux field, which is populated via jQuery as user switch tabs
echo $this->Form->hidden('tab-active', array('name' => 'tab-active', 'id' => 'tab-active'));
echo $this->Form->input('title');
echo $this->Form->submit('Save', array('class' => 'btn btn-success'));
echo $this->Form->end();
?>
Let's suppose the user submitted the form when it was at "Tab #4". When I debug($this->data); at my BriefingsController, this is what I get:
/app/Controller/BriefingsController.php (line 133)
array(
'tab-active' => 'tab-4',
'Briefing' => array(
'title' => 'My test title'
)
)
So my Controller receives the form data for both tab-active and Briefing.title fields, but when the form is loaded after submission, only the Model field is populated with the submitted data, and tab-active comes with an empty value. CakePHP does not auto populate the value for my non-model-attached field.
Any thoughts on that? Is this a default CakePHP behavior or is there something I could do to make it work(auto populate)? I have no problem with the jQuery (already tested it in several ways), the real problem is into getting this field populated back after form submission. Any help is much appreciated.
CakePHP populates forms with the data of the form's model. So if you create a form for 'Briefing' creation, CakePHP will look at the $this->data['Briefing'] array, and populate inputs matching the fields that are present in this array. So as your hidden 'tab-active' field is present inside the 'Briefing' Form creation, CakePHP expects your 'Briefing' model to have a 'tab-active' attribute.
So my guess is that your $this->data array should be :
array(
'Briefing' => array(
'tab-active' => 'tab-4',
'title' => 'My test title'
)
)
<?php
$this->widget('CAutoComplete', array(
'name'=>'search',
'id'=>'input-box',
'attribute'=>'search',
'url'=> $this->createAbsoluteUrl('products/suggestions'),
'value'=>($_GET['search'] == '')?'Search for Mobiles, Cameras & Laptops':$_GET['search'],
'minChars'=>2,
'scroll'=>false,
'resultsClass'=>'searchAutoComplete ac_results',
'htmlOptions'=> array(
'class'=>'searchClickClear',
),
'methodChain'=>'.result(function(){$("form#search-form").submit();})'
));
?>
This is my widget for taking input, and I am passing the class searchAutoComplete. What the class should be? What I want is, when I click on the field the value will be empty.
When you click on input field, event onclick is going on.
So, you can handle it and change input value to empty. Howto?
Change your 'htmlOptions' like this:
'htmlOptions'=> array(
'onclick'=>'$("#input-box").val("")';
),
#input-box - css-selector of your input.
val() - jQuery method for setting some value to elements. See more info about this method
resultsClass - the CSS class for the dropdown list. Defaults to "ac_results". More info
For the task solution you don't need change resultsClass from the default value. Remove this string.
Im building a web app using the yii framework. I have a dropdownlist and im calling an action and updating a div tag using ajax array 'update'=>'#price' field. the code works fine and it updates the price div.
But i want to update two fields like that, i tried passing an array to the update field. but it didnt work.
Any Idea how I can update two div tags and show two values using one action call?
Heres My Code..
echo CHtml::beginForm();
echo CHtml::dropDownList('amount_'.$position,'', array(1=>1,2=>2,3=>3),
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('shoppingCart/updateAmount'),
'update'=>'#price_'.$position, //selector to update
)));
echo CHtml::endForm();
and in my action im just echoing
echo 'LKR '.Shop::priceFormat(#$product->getPrice($cart[$position]['Variations'], $value));
It'll be great if someone could help.
It's simply jQuery selector. I believe you can use comma for few ids. Or you can use class selector. (I think class selector would be better here)
'ajax' => array(
/* ... */
'update' => '#price_1, #price_2, #price_3',
/* or */
'update' => '.price'
)