algolia / algoliasearch-client-javascript

⚡️ A fully-featured and blazing-fast JavaScript API client to interact with Algolia.
https://www.algolia.com/doc/api-client/javascript/getting-started/
MIT License
1.33k stars 222 forks source link

bug with Ion.RangeSlider #146

Closed ghost closed 9 years ago

ghost commented 9 years ago

When you try to change a range distance few or more times,the minimal value reset and the maximum value become NaN. Here is my solution to resolve this problem.

function bindSearchObjects(state) { 
 // Bind Sliders
    for (facetIndex = 0; facetIndex < FACETS_SLIDER.length; ++facetIndex) {
      var facetName = FACETS_SLIDER[facetIndex];
      var slider = $('#' + facetName + '-slider');
      var sliderOptions = {
        type: 'double',
        grid: true,
        min: slider.data('min'),
        max: slider.data('max'),
        from: slider.data('from'),
        to: slider.data('to'),
        prettify: function(num) {
          return  parseInt(num, 10) + ' Kr';
        },
        onFinish: function(data) {
          algoliaHelper.removeNumericRefinement(facetName, '>='); // fix
          if (data.from !== (state.getNumericRefinement(facetName, '>=') || data.min)) {
            algoliaHelper.addNumericRefinement(facetName, '>=', data.from).search();
          }
          algoliaHelper.removeNumericRefinement(facetName, '<='); // fix
          if (data.to !== (state.getNumericRefinement(facetName, '<=') || data.max)) {
            algoliaHelper.addNumericRefinement(facetName, '<=', data.to).search();
          }
        }
      };
      slider.ionRangeSlider(sliderOptions);
    }
  }
redox commented 9 years ago

Hi @Evne-front,

indeed we fixed a bug around the numeric refinements a few weeks ago and the ion.rangeslider demo wasn't reseting the numerical refinement. It seems our demo code (https://github.com/algolia/instant-search-demo/blob/master/js/app.js#L146-L153) has not been updated; I'm updating it now.

Your fix is good btw :)

redox commented 9 years ago

Arg wait, the real fix is:

        onFinish: function(data) {
          var lowerBound = state.getNumericRefinement(facetName, '>=');
          lowerBound = lowerBound && lowerBound[0] || data.min;
          if (data.from !== lowerBound) {
            algoliaHelper.removeNumericRefinement(facetName, '>=');
            algoliaHelper.addNumericRefinement(facetName, '>=', data.from).search();
          }
          var upperBound = state.getNumericRefinement(facetName, '<=');
          upperBound = upperBound && upperBound[0] || data.max;
          if (data.to !== upperBound) {
            algoliaHelper.removeNumericRefinement(facetName, '<=');
            algoliaHelper.addNumericRefinement(facetName, '<=', data.to).search();
          }
        }