Magento - 在产品列表中添加页码以标题(Magento - Adding page number to title on products listing)
我有一个扩展,它使用产品列表块通过属性显示产品网格过滤器(扩展名为“ 属性信息页面” )。
在该扩展块的“_prepareLayout”函数中,扩展使用以下代码设置页面的标题和描述:
$head = $this->getLayout()->getBlock('head'); . . . $head->setTitle($title); head->setDescription($des);
我想以这种格式将文本添加到标题和描述中:
$page_info = "Page A of B for "; $title = $page_info . title; $items_info = "Listings X-Y (out of Z) "; $des = items_info . $des;
我试过这段代码,以获得当前页面,最后一页,数字和项目等等:
$html_pager = Mage::getBlockSingleton('page/html_pager'); $html_pager->setCollection($product_collection); $limit = Mage::getSingleton('core/app')->getRequest()->getParam('limit'); if(empty($limit)) { $limit = 8; } $html_pager->setLimit($limit); $LastPageNumber = $html_pager->getLastPageNum(); $current_page = $html_pager->getCurrentPage(); $page_info = ""; if($current_page > 1) { $page_info = "Page " . $current_page . " of $LastPageNumber for "; } $FirstNum = $html_pager->getFirstNum(); $LastNum = $html_pager->getLastNum(); $TotalNum = $html_pager->getTotalNum(); $items_info = "Listings " . $FirstNum . "-" . $LastNum . " (out of ". $TotalNum . ") ";
该代码为我提供了正确的信息,但它在产品网格中导致问题 - 它始终在产品网格中显示10个产品(无论我在“每页显示”中选择什么)。
任何想法如何在不破坏网格功能的情况下获取信息?
I have an extension that uses the products list block to show the products grid filter by an attribute (the extension is Attribute Info Pages).
In the "_prepareLayout" function of this extension block the extension sets the page's title and description using this code :
$head = $this->getLayout()->getBlock('head'); . . . $head->setTitle($title); head->setDescription($des);
I want to add text to the title and description in this format :
$page_info = "Page A of B for "; $title = $page_info . title; $items_info = "Listings X-Y (out of Z) "; $des = items_info . $des;
I've tried this code it order to get the current page, last page, number and items and so on :
$html_pager = Mage::getBlockSingleton('page/html_pager'); $html_pager->setCollection($product_collection); $limit = Mage::getSingleton('core/app')->getRequest()->getParam('limit'); if(empty($limit)) { $limit = 8; } $html_pager->setLimit($limit); $LastPageNumber = $html_pager->getLastPageNum(); $current_page = $html_pager->getCurrentPage(); $page_info = ""; if($current_page > 1) { $page_info = "Page " . $current_page . " of $LastPageNumber for "; } $FirstNum = $html_pager->getFirstNum(); $LastNum = $html_pager->getLastNum(); $TotalNum = $html_pager->getTotalNum(); $items_info = "Listings " . $FirstNum . "-" . $LastNum . " (out of ". $TotalNum . ") ";
The code gives me the correct information but it causes a problem in the products grid - it always shows 10 products in the products grid (no matter what I choose in "Show per page").
Any ideas how to get the information without breaking the grid functionality?
原文:https://stackoverflow.com/questions/9604493
最满意答案
在你的插件里,
this
是一个jQuery对象。 您应该使用.prop
来获取DOM属性:this.prop('offsetWidth'); this.prop('scrollWidth');
插件的一个好习惯是循环初始jQuery对象,同时返回它以允许链接。 在循环内部,
this
将是DOM元素。(function($){ $.fn.clipping = function() { return this.each(function(){ console.log("Offset Width: " + this.offsetWidth); console.log("Scroll Width: " + this.scrollWidth); if(this.offsetWidth > this.scrollWidth) { } else { } }); }; })( jQuery );
并且您的插件将与包含多个DOM元素的对象一起使用。
Inside you plugin,
this
is a jQuery object. You should use.prop
to get DOM properties:this.prop('offsetWidth'); this.prop('scrollWidth');
A good practice for plugins is to loop the initial jQuery object while returning it to allow chaining. Inside the loop,
this
will be the DOM element.(function($){ $.fn.clipping = function() { return this.each(function(){ console.log("Offset Width: " + this.offsetWidth); console.log("Scroll Width: " + this.scrollWidth); if(this.offsetWidth > this.scrollWidth) { } else { } }); }; })( jQuery );
And your plugin will work with object containing many DOM elements.
相关问答
更多-
插件方法未定义(Plugin method undefined)[2023-05-19]
扩展时不包括原始settings对象。 另外,我建议您创建this.settings字段以保持默认settings分开: $(function() { var settings = { onAdd: function() {}, onRemove: function() {}, sourceSelecter: null, targetSelector: null }; $.fn.tokenizer = function(options) { this.s ... -
我通过“资源”选项卡检查了我的应用程序提供的javascript文件,发现我的application.js仍然有JQuery,因此,大概是,当文件加载时,它在没有Coookie的情况下重新定义了JQuery。 发生这种情况是因为我从使用gems加载JQuery到使用外部CDN,但没有重新编译(或删除)我的资产。 通过从public/assets删除已编译的资产来解决 I checked the javascript files served by my application via the "resour ...
-
我想你可以这样做: $.getJSON(url,function(json){ alert(json.responseData.resultText); }); I think you can do: $.getJSON(url,function(json){ alert(json.responseData.resultText); });
-
在你的插件里, this是一个jQuery对象。 您应该使用.prop来获取DOM属性: this.prop('offsetWidth'); this.prop('scrollWidth'); 插件的一个好习惯是循环初始jQuery对象,同时返回它以允许链接。 在循环内部, this将是DOM元素。 (function($){ $.fn.clipping = function() { return this.each(function(){ console. ...
-
有时如果你试图直接访问事件对象然后它在chrome中运行但不在firefox中运行。 所以解决方案是采取与div相关的自定义位置 打开jquery.tippy.js去函数getPositions(第323行) 将第325行替换为349 if (!event) { event = window.event; } var x = ''; var y = ''; if( event == undefined ) ...
-
使用没有元素的JQuery插件时,“TypeError:a未定义”(“TypeError: a is undefined” when using JQuery plugin with no element)[2023-10-18]
由于代码是您自己的插件,您可以通过添加扩展插件来处理多个选择器/元素 return $(this).each(function() { 到你的插件的顶部: $(function () { $.fn.mapit = function (position, styles) { return $(this).each(function() { var self = this; var map = new google.maps.Map(self, { c ... -
javascript prototype.call()方法需要另外一个参数来引用this 。 if ($.isFunction(options.onDivNameClick)) { options.onDivNameClick.call(this, options.values); } 因此 - 没有实际值通过该函数。 The javascript prototype.call() method needs one more parameter referring to this. if ($.i ...
-
当你定义一个插件时, this已经引用了jquery对象(而不是dom元素),所以我认为你的插件var应该包含每个元素,如下所示: (function ($) { $.fn.talk = function(method) { var settings = {}; var $el = this; //Not need of $() here var methods = { init: function(options){ ...
-
jQuery History Plugin错误:$ .history未定义(jQuery History Plugin Error: $.history is undefined)[2023-09-11]
$.history.init()格式来自较新版本的插件,可在此处找到: http : $.history.init() 你只需要更新你的jquery.history.js :) The $.history.init() format is from the newer version of the plugin, found here: http://github.com/tkyk/jquery-history-plugin You just need to update your jquery.histo ... -
您已将$(this)上下文放在$(window)滚动函数中。 这就是为什么你得到DOM的元素[undefined]因此你无法得到它的top属性 您可能需要在此之前初始化DOM的元素,如下所示: $.fn.offBottom = function() { var oElement = $(this); // $(this) will refer to the DOM that called the [offBottom] property method $(window).scroll(fu ...