Skip to content

Instantly share code, notes, and snippets.

@VasiliuKr
Last active October 12, 2022 09:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save VasiliuKr/560452d4f7cb0faeeacd8333f88083b2 to your computer and use it in GitHub Desktop.
Save VasiliuKr/560452d4f7cb0faeeacd8333f88083b2 to your computer and use it in GitHub Desktop.
bitrix smart filter numbers slider
// В файле script.js шаблона фильтра редактируем:
SmartFilter.prototype.recountMinPrice = function()
{
/* Блок кода взят с https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round */
var myNamespace = {};
myNamespace.round = function(number, precision) {
var factor = Math.pow(10, precision);
var tempNumber = number * factor;
var roundedTempNumber = Math.round(tempNumber);
return roundedTempNumber / factor;
};
/* Конец блока */
var newMinPrice = (this.priceDiff*this.leftPercent)/100;
newMinPrice = (this.minPrice + newMinPrice).toFixed(this.precision);
if (newMinPrice != this.minPrice && newMinPrice > 1000) // Ввели доп.условие для округления значений > 1000
this.minInput.value = myNamespace.round(newMinPrice, -2); // Округления до сотен (-1 до десятков, -3 до тысяч)
else if (newMinPrice != this.minPrice)
this.minInput.value = newMinPrice;
else
this.minInput.value = "";
smartFilter.keyup(this.minInput);
};
SmartFilter.prototype.recountMaxPrice = function()
{
/* Повторяющийся кусок кода, можно вынести */
var myNamespace = {};
myNamespace.round = function(number, precision) {
var factor = Math.pow(10, precision);
var tempNumber = number * factor;
var roundedTempNumber = Math.round(tempNumber);
return roundedTempNumber / factor;
};
/* Конец блока */
var newMaxPrice = (this.priceDiff*this.rightPercent)/100;
newMaxPrice = (this.maxPrice - newMaxPrice).toFixed(this.precision);
if (newMaxPrice != this.maxPrice && newMaxPrice > 1000)
this.maxInput.value = myNamespace.round(newMaxPrice, -2);
else if (newMaxPrice != this.maxPrice)
this.maxInput.value = newMaxPrice;
else
this.maxInput.value = "";
smartFilter.keyup(this.maxInput);
};
@VasiliuKr
Copy link
Author

Код, необходимый для округления шага в инпутах числового ползунка в умном фильтре битрикс. В данном примере округляются значения инуптов, которые больше 1000 до сотен. Например, 19836 в 19800, но 24 останется 24. В коде есть повторяющиеся куски кода, которые можно отрефакторить.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment