如何在prestashop 1.5中为模块添加JS?

时间:2013-08-24 06:52:55

标签: jquery prestashop

在prestashop 1.5中开发模块。我使用displayAdminProductsExtra挂钩在admin选项卡中显示tpl文件。当我在tpl中包含我的jquery代码时,它工作正常。但是当我尝试将其作为新文件并包含其无效时。到目前为止,我尝试了以下方法..

使用displayBackOfficeHeader注册一个钩子并像这样调用..

public function hookdisplayBackOfficeHeader($params) 
{
    $this->context->controller->addJS(($this->_path).'abc.js');
}

我尝试在displayAdminProductsExtra中添加它也像这样..

$this->context->controller->addJS(_MODULE_DIR_.$this->module->name.'/views/js/abc.js'); //first tried..
$this->context->controller->addJS(($this->_path).'abc.js','all'); //second tried this..

我尝试了这样的getcontent ..

public function getContent()
{   
    $this->_html= '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js">
        <script src="../modules/abc/abc.js" type="text/javascript" ></script>';
    return $this->_html;
}

但这些方法没有添加我的js文件。不知道在哪里犯了错误。非常感谢。

2 个答案:

答案 0 :(得分:4)

创建Prestashop模块时,必须添加函数hookHeader,并在其中添加在页面中添加js的行。

需要这样的东西:

public function hookHeader ($ params)
{
     $ this-> controller-> addJS (($ this-> _path). 'abc.js');
}

另一方面,查看blockcategories.php文件中模块blockcategories的代码,我们看到以下内容:

public function displayForm()
{
...
}

此功能用于创建模块配置页面,与使用其他模块的方式相同。也许这是一个更简单的选择,但开发速度更快。

此致

答案 1 :(得分:0)

钩子DisplayHeader(或Header)用于在前台页面中注册资产!

这很适合您的JavaScript路径:

$this->context->controller->addJS($this->_path . 'abc.js');

但是在注册JavaScript之前,您没有通过这种方法注册jQuery资产:

$this->context->controller->addJquery();

此外,您不应使用钩子DisplayBackOfficeHeader。代替它,您应该使用钩子ActionAdminControllerSetMedia

这是详细信息how to register JavaScript in a back-office (in admin pages)