Checkbox filtering in Magento admin grid - checkbox

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/

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 :)

Yii2 Grdiview Checkbox column submit via Form

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.

hook_action_info does not create its VBO list item in view

I am trying to code my custom action using views and VBO.
The my view shows the user a list of commerce line items.
Here the code:
function nlmcode_action_info() {
return array(
'vbo_download_pdf' => array(
'type' => 'entity',
'label' => t('Download PDF'),
'configurable' => FALSE,
'triggers' => array('any')
),
);
}
function vbo_download_pdf($entity, $context) {
dpm("Do the magic here.");
}
I then can see the bulk operation available in the select field but once I have selected my custom 'Download PDF' option and added to the view, the item is missing in the VBO drop down list.
Solved.
Permissions to 'Download PDF' have to be set.

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.

CakePHP form helper - HABTM multiple checkbox styling

I have two tables: "restaurants" and "cuisines" which are related to each other by a HABTM table
The table cuisines has certain fixed entries - 54 number
A restaurant can have any number of cuisines. On baking the application this came with a multiple select. Since i wanted check boxes i used array( 'type' => 'select', 'multiple' => 'checkbox') to convert it into checkboxes.
Now i want to style the way this checkboxes are displayed into columns of 4 as seen on the screenshot below.
img2.pict. com/82/bc/a4/1453459/0/200908111511.png
echo $form->input('Cuisine', array('type' => 'select', 'multiple' => 'checkbox'));
The above code produces many div's around each element as follows
http://img2.pict.com/1a/a3/0a/1453457/0/200908121509.png
I have tried the following:
echo $form->input('Cuisine', array( 'type' => 'select', 'multiple' => 'checkbox', 'div' => false, 'label' => false));
but this code only removes the outside divs and label. I am not able to control the internal
<div class="checkbox">
<label for="CuisineCuisine2">Andhra</label>
that appear around the single checkboxes.
How can I use the FormHelper to remove or give classes to the internal divs, so I can do some custom styling?
Or is there any other way to populate this HABTM table to get the effect i want?
You could get around this by doing $form->select() instead, and apply a style or class attribute to get it to look how you want.
It seems to make sense to not use the $form->input() function if you are going to remove the div and label anyway.
You can stylize the DIV elements with CSS.
<style>
div.input div.checkbox {
float: left;
width: 50%;
}
</style>
You can remove or give classes to the internal divs like this
$this->Form->input("hello_test",array('type'=>'checkbox','div'=>'class_name'));
By default cake uses : type class e.g - type is checkbox then class="checkbox"

Resources