简体   繁体   English

如何将javascript值传递给控制器​​动作?

[英]How to pass javascript value to controller action?

I've tried different ways to get my selected value from my Category.php file to controller file ( IndexController.php ). 我尝试了多种方法来将我选择的值从Category.php文件转换为控制器文件( IndexController.php )。 For this I have 3 files Category.php as renderer (is a dropdown list I added to a Grid.php in magento) 为此,我有3个文件Category.php作为渲染器(这是我在magento中添加到Grid.php的下拉列表)

<?php

class Ns_Thorleif_Block_Adminhtml_Commerciaux_Edit_Form_Renderer_Category extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{   
     public function render(Varien_Object $row)
{

$category = Mage::getModel('thorleif/category')->getCollection();

$category_rows = $category->getData();
$html = '<select id="select_category" name="select_category" onchange="ChooseContact(this)">';
$sep = '';
?>
<input type="text" name="id_cat" id="id_cat" value="" />
<?php

foreach ($category_rows as $rows) {

    // put the correct key here

    if (!$rows['level']) {
        $html .= $sep . "<optgroup label='{$rows['name']}'>";
        $sep = '</optgroup>';
    } else {
        $nbsp = str_repeat('&nbsp;', ($rows['level'] - 1)* 5);
        $html .= "<option value='{$rows['id_linio_category']}' title='{$rows['name']}' selected>
        $nbsp {$rows['name']}</option>";
    }
}
$html .= '</select>';
return $html;
}
}
?>

Grid.php Grid.php

 public function _prepareColumns()
        {
    $this->addColumn('lin',
                array(
                    'header' => 'Lin Category',
                    'align' => 'left',
                    'index' => 'lin',
                    'filter'    => false,
                    'sortable'  => false,
                    'renderer' => 'Ns_Thorleif_Block_Adminhtml_Commerciaux_Edit_Form_Renderer_Category'
                )
            );
            $this->addColumn('action',
                array(
                    'header'    =>  Mage::helper('customer')->__('Action'),
                    'width'     => '100',
                    'type'      => 'action',
                    'getter'    => 'getId',
                    'actions'   => array(
                        array(
                            'caption'   => Mage::helper('customer')->__('Sync'),
                            'url'       => array('base'=> '*/*/sync'),
                            'field'     => 'id',
                            'target'=>'_blank'
                        )
                    ),
                    'filter'    => false,
                    'sortable'  => false,
                    'index'     => 'stores',
                    'is_system' => true,
            ));
            return parent::_prepareColumns();
}

IndexController.php where I want to get/use the Selected "Javascript_value" below from Category.php 我想在其中获取/使用Category.php中选定的“ Javascript_value”的IndexController.php

public function syncAction()
    {

    //var_dump($_REQUEST['id_cat']);
    //var_dump($_POST['id_cat']);
    //var_dump($_GET['id_cat']);
        $id = $this->getRequest()->getParam('id');
        $products = Mage::getModel('catalog/category')
                    ->load($id)
                    ->getProductCollection()->getData();

        $send = "<Request>\n";

        Foreach($products as $values){
            $code = Mage::getModel('catalog/product')->load($values['entity_id'])->getData();
            $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($values['entity_id']);


            $send .= "<Product>".$this->getProductXml($code, $commercial, ***JAVASCRIPT_VALUE***, $stock)."</Product>\n";

        }
        $send .="</Request>"; 
        $api = new Ns_Thorleif_Adminhtml_IndexController();
        $response = $api->postProductAction($send);
        echo "<xmp>$send</xmp>";
        echo "<xmp>$response</xmp>";
    }

The 3 files have the result on the same front page Category.php=select dropdownlist, Grid.php= full grid in the front including column with dropdown list, IndexController.php = Function I use to process some data. 这3个文件的结果在相同的首页上Category.php = select dropdownlist,Grid.php =完整网格,包括下拉列表列,IndexController.php =我用来处理某些数据的函数。

Any suggestion please? 有什么建议吗? the Idea I've tried the get/post/request, tried to save data in database and try to get it. 我尝试过get / post / request,尝试将数据保存在数据库中并尝试获取它。

You are just redirecting the control not posting any form. 您只是重定向控件而不发布任何表单。 thats why you are not able to get the data. 这就是为什么您无法获取数据的原因。 You can do two of the things. 您可以做两件事。

  1. In "ChooseContact" js function, rediect to syncAction with the selected value in get method and remove the action button. 在“ ChooseContact” js函数中,重新将其与get方法中的选定值重新同步为syncAction并删除操作按钮。

  2. In the action button, instead of redirecting the control you can call a js function and get the dropdown selected value and then redirect. 在操作按钮中,您可以调用js函数并获取下拉列表中的选定值,然后重定向,而不是重定向控件。 Please refer : Magento adminhtml grid with javascript action column 请参阅: Magento带有javascript操作列的adminhtml网格

Hope this will help! 希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM