how to run yii widget when button clicked - php

I have this widget:
<?php
class Search extends CWidget
{
public $dataProvider = null;
public function init()
{
$criteria = new CDbCriteria();
if ( isset($_GET['file']) ) {
$criteria->compare('fileName', $_GET['file'], true, 'OR');
$criteria->compare('tags', $_GET['file'], true, 'OR');
}
$this->dataProvider = new CActiveDataProvider("Files", array(
'criteria' => $criteria,
//'countCriteria' => $criteria,
'pagination'=>array(
// results per page
'pageSize'=>1,
),
));
}
public function run(){
$this->render('site/result', array(
'dataProvider' => $this->dataProvider,
'pages' => $this->dataProvider->pagination,
));
}
}
?>
how I can run this widget only when the search button is clicked, and how to view its result in the view reuslt that is located in the folder site

Maybe I misunderstood you, but I think you have a little confusion about widget. Avoid making a
lengthy answer, I made a model in below
Question 1: How I can run this widget only when the search button is clicked?
The idea is that you put the widget content into hidden div, and then add a jQuery script to show it after the search button is clicked
<div id='search-result' style="display:none"><?php $this->widget(...) ?></div>
<script>
$('#search-button-id').click({
// do something
$('#search-result').show();
})</script>
Question 2: how to view its result in the view reuslt that is located in the folder site?
Imao, I recommend you don't do this. The widget is independant part, you can use it for many specific purposes & many locations of your project, so just put a simple view for widget to render in its own, instead of trying to render a view outside widget's scope. It should look like below
/your-app/protected/extensions/widgets/search/Search.php
/your-app/protected/extensions/widgets/search/views/result.php // view for rendering

Related

Drupal 8 - Adding/Creating Menu Item with HTML in it

I'm creating a website using Drupal 8. I want to create a menu item link that I could add HTML/Javascript code in it (I'm trying to display a widget that expands on click in the menu rather than displaying it in its own block next to the menu). The only way I could see to add a menu item is to link to a page.
You could work with a derivative. This lets you customize pretty much everything about it and control what is to be made. An example below:
Note: I am assuming you have a general knowledge of custom modules. If not follow this link
Create the following file in your custom module:
# my_module.links.menu.yml
my_module.custom_links:
deriver: \Drupal\my_module\Plugin\Derivative\CustomLinkDerivative
And now for the derivative class (Located under my_module/src/Plugin/Derivative)
<?php
namespace Drupal\my_module\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CustomLinkDerivative extends DeriverBase implements ContainerDeriverInterface {
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static();
}
/**
* {#inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$links['custom_menulink'] = [
'title' => t('Custom menulink'),
'menu_name' => 'main',
'route_name' => 'entity.node.canonical',
'parent' => footer,
'route_parameters' => [
'node' => 1,
]
] + $base_plugin_definition;
return $links;
}
}
Note: Derivatives get triggered during rebuild of cache!
This just creates a link in the footer that directs to node 1. You can add all sort of stuff and logic to your liking. Hope this helps you :)

yii gridview does not display all the data from its respective table

i have this query using CActiveDataProvider. My table contains 15 records that satisfies the condition, but for some reason, only ten records are being displayed. What might be missing here.
$prov2 = new CActiveDataProvider('BaseSiReceivedItem', array(
'criteria' => array(
'condition' => 'iar_no = 0'
)));
echo count($prov2->data);
'pagination'=>array(
'pageSize'=>15,
)
Does that now show you 15?
This link about itemCounts may also be helpful. Yii Pagination Variables from DataProvider
The listview widget's pageSize value in the view file might be overriding the pageSize value in the controller (if it's not set, the default value may override the controller).
Or, you could create a custom list view like this:
extensions/CustomListView.php
<?php
Yii::import('zii.widgets.CListView');
class CustomListView extends CListView {
public function init() {
if ($this->dataProvider !== null)
$this->dataProvider->pagination->pageSize = 30;
parent::init();
}
}
and use it in your view: views/{model}/index.php
<?php $this->widget('CustomListView', array(
'dataProvider' => $dataProvider,
'itemView' => '_view'
)); ?>
I had also encountered this problem , listing only 10 items while disabling pagination from my list view. At that time I used the following code and I got all the items listed :)
$data = new CActiveDataProvider('ProviderPortfolioSet', array('criteria' => $criteria, 'pagination' => false));

Add product selection widget to custom attribute in Magento

Does anyone know how can I add a custom product attribute with a widget renderer?
You can see this in Promo rules if you select SKU you'll got an Ajax popup with product selection.
so how would I go about it?
in :
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY...
In other words, how can I use a widget to select custom attribute values?
EDIT:
The scenario is as follows:
I would like to create a product attribute that will, upon a button click, open a product selection widget.
After the selection, the selected SKU's will go in in a comma delimited format.
This behavior can be seen in the catalog and shopping cart price rules.
If you filter the rule by SKU (SKU attribute must be enabled to "apply to rules"), you'll get a field and a button that will open the product selection widget.
Here is some thoughts that should get you going on the right track:
First, in a setup script, create your entity:
$installer->addAttribute('catalog_product', 'frontend_display', array(
'label' => 'Display Test',
'type' => 'varchar',
'frontend_model' => 'Test_Module/Entity_Attribute_Frontend_CsvExport',
'input' => 'select',
'required' => 0,
'user_defined' => false,
'group' => 'General'
));
Make sure to set the frontend_model to the model that you are going to use. The frontend model affects the display of the attribute (both in the frontend and the adminhtml sections).
Next, create yourself the class, and override one or both of the following functions:
public function getInputType()
{
return parent::getInputType();
}
public function getInputRendererClass()
{
return "Test_Module_Block_Adminhtml_Entity_Renderer_CsvExport";
}
The first (getInputType()) is used to change the input type to a baked in input type (see Varien_Data_Form_Element_* for the options). However, to set your own renderer class, use the latter function - getInputRendererClass(). That is what I am going to demonstrate below:
public function getElementHtml()
{
return Mage::app()->getLayout()->createBlock('Test_Module/Adminhtml_ExportCsv', 'export')->toHtml();
}
Here, to clean things up, I am instantiating another block, as the element itself doesn't have the extra functions to display buttons and the like.
Then finally, create this file:
class Test_Module_Block_Adminhtml_ExportCsv extends Mage_Adminhtml_Block_Widget
{
protected function _prepareLayout()
{
$button = $this->getLayout()->createBlock('adminhtml/widget_button')
->setData(array(
'label' => $this->__('Generate CSV'),
'onclick' => '',
'class' => 'ajax',
));
$this->setChild('generate', $button);
}
protected function _toHtml()
{
return $this->getChildHtml();
}
}
This doesn't cover the AJAX part, but will get you very close to getting the rest to work.

Yii CGridView - Custom Columns

been looking for a solution to add a feature for "Custom Columns"... Meaning, I present a list of columns that I can show the user and he selects the ones he wants to see and after the selection the table is updated and add/removes the needed columns.
Didn't find anything on Google (perhaps it has a different name than what I was looking for...)
Anyone has an Idea on how it can be accomplished?
Thanks in advance!
This is not a complete sample, but can give you some clues on how to implement it. You've to define some kind of form to collect the data about how your grid has to be rendered. I recommend you to create a CFormModel class if there are more than 3 input fields. Create a view file with the form and a div or renderPartial of a file containing a grid:
$form = $this->beginWidget('CActiveFormExt');
echo $form->errorSummary($model);
echo $form->labelEx($model,'column1');
echo $form->dropDownList($model
echo $form->error($model,'column1');
echo CHtml::ajaxSubmitButton('UpdateGrid',array('controller/grid'),
array('update'=>'#grid'),
$this->endWidget();
// you can render the 'default options' before any ajax update
$this->renderPartial('_grid',array($customColumns=>array('id','name'),'dataProvider'=>$dataProvider));
In the _grid.php view file:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'grid',
'dataProvider'=>$dataProvider,
'columns' => $customColumns;
));
In the controller:
function actionGrid(){
// recover the form data, and build the custom columns array
$customColumns = array();
$customColumns[] = '.....';
$dataProvider = ...;
$this->renderPartial('_formTrabajo', array('customColumns' => $idSiniestro, 'dataProvider' => $dataProvider'), false);
}
When you click the ajaxSubmitButton, the form is sent to the url specified through ajax, and the reply from the controller must contain the renderPartial of the view containing the grid, so the jQuery call can replace the html correctly. You must pass an array from your controller to the partial view of the grid, with the custom list of columns you want to display.

What is a minimal, complete example of pagination in Kohana 3.2?

I found much information about pagination in Kohana 3.2 but most of it is scattered across forum comments and blog posts with no single complete source to refer to.
(note: I intended to self answer this question)
This is what worked for me:
Download the pagination module from https://github.com/kloopko/kohana-pagination (pagination was removed from Kohana 3.2, so this is an adapted module).
Install the module in modules/pagination.
Add the module in bootstrap.php:
Kohana::modules(array(
// ... other modules ...
'pagination' => MODPATH.'pagination'
));
Copy the configuration file from modules/pagination/config/pagination.php to application/config/pagination.php.
Add the following actions to your controller:
public function action_index() {
// Go to first page by default
$this->request->redirect('yourcontroller/page/?page=1');
}
public function action_page() {
$orm = orm::factory('your_orm');
$pagination = Pagination::factory(array(
'total_items' => $orm->count_all(),
'items_per_page' => 20,
)
);
// Pass controller and action names explicitly to $pagination object
$pagination->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action()));
// Get data
$data = $orm->offset($pagination->offset)->limit($pagination->items_per_page)->find_all()->as_array();
// Pass data and validation object to view
echo View::factory('yourview/page', array('data' => $data, 'pagination' => $pagination));
}
Create yourview/page as follows:
<?php
foreach($data as $item) {
// ...put code to list items here
}
// Show links
echo $pagination;
Modify application/config/pagination.php according to your needs. I had to change the 'view' parameter to 'pagination/floating' which displays ellipses (...) when the list of pages is too large, unlike the default 'pagination/basic' which lists all pages regardless of length.
Pagination wasn't originally working/supported in Kohana 3.2. Luckily, somebody has updated the module and you can get the code at https://github.com/kloopko/kohana-pagination

Categories