Yii2 Grdiview Checkbox column submit via Form - checkbox

in my yii2 project i've got a gridview with a simple checkbox column.
<?=
GridView::widget([
'id' => 'gridwithcheckboxes',
'dataProvider' => $dataProvider,
'columns' => [
['attribute' => 'a', 'value' => 'tabfora.a'],
['attribute' => 'b', 'value' => 'tabforb.b'],
'user',
'mobile',
'description',
['class' => 'yii\grid\CheckboxColumn'],
],
]);
?>
I know i can get the checkboxes values by this js helper:
var keys = $('#gridwithcheckboxes').yiiGridView('getSelectedRows');
Is there a method to pass them with a form submit to a controller action instead using javascript?
Thanks for all the help.

I resolve this problem like this.
Create link in which href you will add ids of the rows you checked.
<a href="" class="btn btn-info" target="_blank" id="exampleButton" data-pjax=false>Button</a>
Then register a javascript action in the bottom of the page, where on click of the checkbox you will update href of link.
<?php
$this->registerJs('
$(document).on("ready pjax:success", function() {
$(".kv-row-checkbox").change(function(){
var keys = $("#gridwithcheckboxes").yiiGridView("getSelectedRows");
var keysJson = JSON.stringify(keys);
$("a[id=\"exampleButton\"]").attr("href", "name-of-action?keys="+keysJson);
});
$(".select-on-check-all").change(function(){
var keys = $("#gridwithcheckboxes").yiiGridView("getSelectedRows");
var keysJson = JSON.stringify(keys);
$("a[id=\"exampleButton\"]").attr("href", "name-of-action?keys="+keysJson);
});
});
',View::POS_READY);
?>
.kv-row-checkbox and .select-on-check-all are classes of checkboxes, you must check if yours are different.
In the controller
public function actionNameOfAction($keys)
{
// decoding
$keys = json_decode($keys);
// Operation with ids
......
}

I use the gridview widget including the checkbox column within regular html form tags to pass the selected id's via $_POST. You need to have a hidden input with value =Yii::$app->request->getCsrfToken(), or it won't work. In the controller, $_POST['selection] is an array of the selected id's.

The only way to do it via form is actually to wrap a gridview with a form. The form should start before the gridview and end after the gridview. You can have one or two submit buttons.
It doesn't mean you can't use ajax for this if you want.

Related

Hook into elementor widget?

I am trying to find a hook that will let me add my own code into an existing elementor widget. For example they have the "post widget" which lets you display a list of posts based on the conditions/categories you set.
I would like to add my own code into this "block" but am unable to find any specific hooks for hooking into an existing widget (specifically the posts widget)
Any help would be much appreciated. Is there a hook for this? If not what is my next best option?
Thanks!
This depends on what you want to achieve but in general there are hooks.
I am not sure about the posts-widget but I can show you some examples in general.
If you want to add controls to a widget use this (you can find additional information to the names and stuff in their documentation https://developers.elementor.com/add-controls-to-widgets/)
add_action( 'elementor/element/heading/section_title/before_section_end', function( $element, $args ) {
$element->add_control( 'title_color',
[
'label' => 'Color' ,
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'red',
'options' => [
'red' => 'Red',
'blue' => 'Blue',
],
'section' => 'section_title',
'tab' => 'content',
]
);
}, 10, 2);
the widget in the example is the heading. You can find out the registered names by inspecting the editor or the blocks inside the plugin directory
If you want to change the rendered content of a widget you can use this
add_action( 'elementor/widget/render_content', function( $content, $widget ){ // $content (string) = the rendered content of the widget; widget = the data object
if ($widget->get_name() === 'heading') { // your targeted widget
$settings = $widget->get_settings(); // every that is stored in this method. Titles, captions, margins and so on. Usually this is all you need
// eg if you simply want to wrap your widgets content you can do something like this
$content .= '<div class="i-am-a-wrapper">'.$content.'</div>';
}
return $content;
}, 10, 2 );
I hope this helps :)

Cakephp3 Saving Multiple dropdown selections to database

I have a form with a dropdown that is multiple => true
echo $this - > Form - > input('test_id', [
'options' => $tests,
'required' => true,
'empty' => 'Select Tests',
'multiple' => true
]);
But when i submit the form only one value is saved in database.
I have searched for solution and found this:
Multiple select in input Cakephp
It suggests using SaveMany in my Controller, but i can't figure it out how to use it to get my desired output.
I'm very new to this framework so any help would be appreciated.
sorry i m also new but as other answers and question i don't see 'selected' => $selected in your form
CakePHP 2.0 Select form Mulitple selected
and How to set selection in mulitiselect list box in cakephp

Different Title for Different Radio buttons in Cakephp form helper

How can i give different title for different radio button using CakePHP Form Helper
$radio_options = array('unknown'=>'Unknown','negative'=>'Negative','positive'=>'Positive');
$titles = array('0'=>'Unknown','1'=>'Negative','2'=>'Positive');
I am trying to create radio buttons like this
echo $this->Form->input('radio_buttons', array(
'options' => $radio_options,
'legend' =>false,
'label' => true,
'div'=>false,
'class'=>'radio inline',
'type' => 'radio',
'separator'=>'<br>',
'title'=>$titles,));
But its not working..Form Helper creating same title for all of the radio buttons.
Look at what the FormHelper generates, then generate it manually or with a php foreach loop or something.
There are cases like this, when it's just easier (or the only way) to not use a helper.

Checkbox filtering in Magento admin grid

I have a Slider model having some associated Images (models). When editing a Slider, there's a tab for its images. Now this can be modified, to select and deselect containing images. This, it seems, is done through an admin grid, having a checkbox column (the "values" key is for testing):
$this->addColumn('in_slider', array(
'header_css_class' => 'a-center',
'type' => 'checkbox',
'name' => 'in_slider',
'values' => array(1,2),
'align' => 'center',
'index' => 'slider_image_id'
));
Let's say the Slider in the above image has one image attached, and that works fine. But, when user selects "Any" and clicks "Search", all images must show, but those that are not associated should be deselected. I think this is standard behavior for such a feature.
What I don't get is the mechanism behind those two buttons: Reset Filter and Search. From what I've managed to dig up, a grid class has a getGridUrl() method which is used to refresh the grid data (via AJAX). That URL mapping to a controller.
// in grid class
public function getGridUrl()
{
return $this->getUrl('*/*/editGrid', array('_current' => true));
}
// Image controller
public function editGridAction()
{
$blockMarkup = $this->getLayout()->createBlock('module/someblock')->toHtml();
$this->getResponse()->setBody($blockMarkup);
}
But what about the "Search" button? How does that work?
How do other modules know when to show ALL entities, or searching for "Yes" or "No"?
When saving the slider, how can one grab the selected values? Working in Slider controller's saveAction I presume, but how do you get the checkbox values?
What you're looking for is a mass action. Add this function to your grid file (whatever extends Mage_Adminhtml_Block_Weidget_Grid):
protected function _prepareMassaction()
{
$this->setMassactionIdField('some_ID');
$this->getMassactionBlock()->setFormFieldName('element_name'); //html name of checkbox
$this->getMassactionBlock()->addItem('some_ID', array(
'label'=> __('Some Label'),
'url' => $this->getUrl('*/*/doSomething'), //an action defined in the controller
'selected' => 'selected',
'confirm' => __('Are you sure?')
));
return $this;
}
Here's a good blog post that helps to explain the concept:
http://inchoo.net/ecommerce/magento/how-to-add-massactions-to-magentos-grid/

Give ID to submit button

I have a form and i have ended it using,
echo $this->Form->end('Login');
However, i can't seem to assign it an id or class to style it and be able to reference it with jquery. Also,I want to style it with a picture and the picture has the text already in so i need the buttons value to be empty but i can't manage that either...
Any Help?
Many Thanks, Chris
***Note - this is the html it generates;
<div class="submit"><input type="submit" value="Login"/></div>
Ideally i would like to remove the div aswell but its not a necessity.
here's the solution:
$options = array
(
'label' => 'Update',
'value' => 'Update!',
'id' => 'blabla',
'div' => array(
'class' => 'glass-pill',
)
);
echo $this->Form->end($options);
Cheers! =)

Resources