簡體   English   中英

如何在prestashop 1.5中為模塊添加JS?

[英]How to add JS for modules in prestashop 1.5?

我正在prestashop 1.5中開發模塊。 我正在使用displayAdminProductsExtra掛鈎在管理標簽中顯示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文件。 不知道在哪里犯錯。任何幫助將不勝感激。

在創建Prestashop模塊時,必須添加函數hookHeader,並在其中添加在頁面中添加js的行。

需要這樣的東西:

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

另一方面,查看blockcategories.php文件中模塊blockcategories的代碼,我們看到以下內容:

public function displayForm()
{
...
}

此功能是使用與其他模塊相同的方式來創建用於模塊配置的頁面。 也許這是一個更簡單的選擇,但開發速度更快。

問候

鈎子DisplayHeader (或Header )用於在前台頁面中注冊資產!

這很適合您的JavaScript路徑:

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

但是在注冊JavaScript之前,您沒有通過這種方法注冊jQuery資產:

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

另外,您不應使用鈎子DisplayBackOfficeHeader 代替它,您應該使用鈎子ActionAdminControllerSetMedia

這是詳細信息, 以及如何在后台(在管理頁面中)注冊JavaScript

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM