ReactJS: मॉडलिंग द्वि-दिशात्मक अनंत स्क्रॉलिंग


114

हमारे आवेदन विषम वस्तुओं की बड़ी सूची नेविगेट करने के लिए अनंत स्क्रॉल का उपयोग करता है। कुछ झुर्रियाँ हैं:

  • हमारे उपयोगकर्ताओं के लिए 10,000 वस्तुओं की सूची होना और 3k + के माध्यम से स्क्रॉल करना आवश्यक है।
  • ये समृद्ध वस्तुएं हैं, इसलिए ब्राउज़र प्रदर्शन के अस्वीकार्य होने से पहले हम केवल DOM में कुछ सौ कर सकते हैं।
  • आइटम अलग-अलग ऊंचाइयों के हैं।
  • आइटम में चित्र हो सकते हैं और हम उपयोगकर्ता को एक विशेष तिथि तक कूदने की अनुमति देते हैं। यह मुश्किल है क्योंकि उपयोगकर्ता उस सूची में एक बिंदु पर कूद सकता है जहां हमें व्यूपोर्ट के ऊपर छवियों को लोड करने की आवश्यकता होती है, जो लोड होने पर सामग्री को नीचे धकेल देती है। संभाल करने में असफल होने का मतलब है कि उपयोगकर्ता किसी तिथि को कूद सकता है, लेकिन फिर उसे पहले की तारीख में स्थानांतरित कर दिया जाएगा।

ज्ञात, अपूर्ण समाधान:

मैं एक पूर्ण समाधान के लिए कोड की तलाश नहीं कर रहा हूं (हालांकि यह बहुत अच्छा होगा।) इसके बजाय, मैं इस स्थिति को मॉडल करने के लिए "रिएक्ट तरीका" की तलाश कर रहा हूं। स्क्रॉल स्थिति स्थिति है या नहीं? सूची में अपनी स्थिति बनाए रखने के लिए मुझे किस राज्य पर नज़र रखनी चाहिए? मुझे किस राज्य को रखने की आवश्यकता है ताकि मैं एक नया रेंडर ट्रिगर करूं जब मैं नीचे या ऊपर की तरफ स्क्रॉल करूं जो कि प्रदान किया गया है?

जवाबों:


116

यह एक अनंत तालिका और एक अनंत स्क्रॉल परिदृश्य का मिश्रण है। इसके लिए मुझे जो सबसे अच्छा अमूर्त मिला वह निम्नलिखित है:

अवलोकन

एक <List>घटक बनाएं जो सभी बच्चों की एक सरणी लेता है । चूंकि हम उन्हें प्रस्तुत नहीं करते हैं, इसलिए उन्हें आवंटित करना और उन्हें छोड़ देना वास्तव में सस्ता है। यदि 10k आवंटन बहुत बड़ा है, तो आप इसके बजाय एक फ़ंक्शन ले सकते हैं जो एक सीमा लेता है और तत्वों को वापस करता है।

<List>
  {thousandelements.map(function() { return <Element /> })}
</List>

आपका Listघटक इस बात पर नज़र रख रहा है कि स्क्रॉल स्थिति क्या है और केवल उन बच्चों को प्रस्तुत करती है जो देखने में हैं। यह पिछली वस्तुओं को नकली करने के लिए शुरुआत में एक बड़ा खाली div जोड़ता है जो प्रदान नहीं किए जाते हैं।

अब, दिलचस्प हिस्सा यह है कि एक बार एक Elementघटक प्रदान किया जाता है, आप इसकी ऊंचाई को मापते हैं और इसे अपने में संग्रहीत करते हैं List। यह आपको स्पेसर की ऊंचाई की गणना करने और यह जानने में मदद करता है कि कितने तत्वों को प्रदर्शित किया जाना चाहिए।

छवि

आप कह रहे हैं कि जब छवि लोड हो रही है तो वे सब कुछ "नीचे" कूदते हैं। इसका समाधान आपके img टैग में छवि आयाम सेट करना है <img src="..." width="100" height="58" />:। इस तरह ब्राउज़र को यह जानने के लिए डाउनलोड करने के लिए इंतजार नहीं करना पड़ता कि वह किस आकार में प्रदर्शित होने जा रहा है। इसके लिए कुछ बुनियादी सुविधाओं की आवश्यकता है लेकिन यह वास्तव में इसके लायक है।

यदि आप पहले से आकार नहीं जान सकते हैं, तो onloadश्रोताओं को अपनी छवि में जोड़ें और जब यह लोड हो जाए तो इसके प्रदर्शित आयाम को मापें और संग्रहीत पंक्ति की ऊंचाई को अपडेट करें और स्क्रॉल स्थिति की भरपाई करें।

यादृच्छिक तत्व पर कूदना

यदि आपको सूची में एक यादृच्छिक तत्व पर कूदने की आवश्यकता है जो स्क्रॉल स्थिति के साथ कुछ प्रवंचना की आवश्यकता है, क्योंकि आप बीच में तत्वों के आकार को नहीं जानते हैं। मैं आपको जो करने का सुझाव देता हूं वह यह है कि आपके द्वारा पहले से गणना की गई तत्वों की ऊँचाइयों को औसत करने के लिए और अंतिम ज्ञात ऊंचाई + (तत्वों की संख्या * औसत) की स्क्रॉल स्थिति पर जाएं।

चूंकि यह सटीक नहीं है कि जब आप अंतिम ज्ञात अच्छी स्थिति में वापस पहुंचते हैं तो यह समस्या पैदा करने वाला है। जब कोई विरोध होता है, तो इसे ठीक करने के लिए केवल स्क्रॉल स्थिति बदलें। यह स्क्रॉल बार को थोड़ा स्थानांतरित करने वाला है, लेकिन उसे बहुत अधिक प्रभावित नहीं करना चाहिए।

प्रतिक्रिया विशेषण

आप सभी प्रदान किए गए तत्वों को एक कुंजी प्रदान करना चाहते हैं ताकि वे रेंडरर्स में बनाए रहें। दो रणनीतियाँ हैं: (1) में केवल n कुंजियाँ हैं (0, 1, 2, ... n) जहाँ n अधिकतम तत्व हैं जिन्हें आप प्रदर्शित कर सकते हैं और उनकी स्थिति का उपयोग कर सकते हैं modulo n। (2) प्रति तत्व एक अलग कुंजी है। यदि सभी तत्व एक समान संरचना साझा करते हैं तो उनके डोम नोड्स का पुन: उपयोग करने के लिए (1) का उपयोग करना अच्छा है। यदि वे तब उपयोग नहीं करते हैं (2)।

मेरे पास केवल प्रतिक्रिया स्थिति के दो टुकड़े होंगे: पहले तत्व का सूचकांक और प्रदर्शित होने वाले तत्वों की संख्या। वर्तमान स्क्रॉल स्थिति और सभी तत्वों की ऊंचाई सीधे जुड़ी होगी this। उपयोग करते समय setStateआप वास्तव में एक रेंडरर कर रहे हैं जो केवल तब होना चाहिए जब रेंज बदल जाए।

इस उत्तर में वर्णित कुछ तकनीकों का उपयोग करके यहां अनंत सूची का एक उदाहरण दिया गया है। यह कुछ काम होने जा रहा है, लेकिन प्रतिक्रिया निश्चित रूप से एक अनंत सूची को लागू करने का एक अच्छा तरीका है :)


4
यह एक कमाल की तकनीक है। धन्यवाद! मुझे यह मेरे एक घटक पर काम कर रहा है। हालाँकि, मेरे पास एक और घटक है जिसे मैं इसे लागू करना चाहूंगा, लेकिन पंक्तियों में एक सुसंगत ऊँचाई नहीं है। जब तक आप एक बेहतर विचार नहीं है, मैं प्रदर्शन / गणना की गणना करने के लिए अपने उदाहरण को बढ़ाने के लिए काम कर रहा हूं।
मलांग

मैंने इसे एक मोड़ के साथ लागू किया है, और एक मुद्दे में भाग गया है: मेरे लिए, जो रिकॉर्ड मैं प्रस्तुत कर रहा हूं, वे कुछ जटिल डोम हैं, और उनमें से # के कारण उन सभी को ब्राउज़र में लोड करना समझदारी नहीं है, इसलिए मैं हूं समय-समय पर async भ्रूण कर रहे हैं। किसी कारण से, जब मैं स्क्रॉल करता हूं और स्थिति बहुत दूर तक कूद जाती है (कहते हैं कि मैं स्क्रीन और वापस चला जाता हूं), तो सूचीबॉडी फिर से प्रस्तुत नहीं करता है, भले ही राज्य बदलता है। किसी भी विचार क्यों यह हो सकता है? अन्यथा महान उदाहरण!
SleepyProgrammer

1
आपका JSField वर्तमान में एक त्रुटि फेंकता है: बिना संदर्भ दिए गए संदर्भ: उत्पन्न परिभाषित नहीं है
Meglio

3
मैंने एक अपडेटेड फिडेल बनाया है , मुझे लगता है कि इसे भी काम करना चाहिए। किसी को भी सत्यापित करने के लिए परवाह है? @ मैगेलियो
एकन्यूज

1
@ThomasModeneis हाय, क्या आप लाइन 151 और 152, डिस्प्लेस्टार्ट और डिस्प्ले पर की गई गणनाओं को स्पष्ट कर सकते हैं
shortCircuit

2

http://adazzle.github.io/react-data-grid/index.html# पर एक नज़र डालें यह एक्सेल जैसी सुविधाओं और आलसी लोडिंग / अनुकूलित रेंडरिंग (लाखों पंक्तियों के लिए) के साथ एक शक्तिशाली और आकर्षक डेटाग्रिड की तरह दिखता है। समृद्ध संपादन सुविधाएँ (MIT लाइसेंस प्राप्त)। अभी तक हमारे प्रोजेक्ट में कोशिश नहीं की गई है लेकिन बहुत जल्द ऐसा होगा।

इस तरह की चीजों की खोज करने के लिए एक महान संसाधन भी http://react.rocks/ है इस मामले में, एक टैग खोज सहायक है: http://react.rocks/tag/InfiniteScroll


1

मैं विषम-मद की ऊंचाइयों के साथ एकल-दिशा अनंत स्क्रॉलिंग के लिए एक समान चुनौती का सामना कर रहा था और इसलिए मेरे समाधान से एक एनपीएम पैकेज बनाया गया था:

https://www.npmjs.com/package/react-variable-height-infinite-scroller

और एक डेमो: http://tnrich.github.io/react-variable-height-infinite-scroller/

आप तर्क के लिए स्रोत कोड देख सकते हैं, लेकिन मैंने मूल रूप से उपरोक्त उत्तर में उल्लिखित नुस्खा @Vjeux का अनुसरण किया है। मैंने अभी तक किसी विशेष आइटम पर कूदने से निपटने के लिए नहीं किया है, लेकिन मैं इसे जल्द ही लागू करने की उम्मीद कर रहा हूं।

यहाँ वर्तमान में जो कोड दिखता है, वह इस प्रकार है:

var React = require('react');
var areNonNegativeIntegers = require('validate.io-nonnegative-integer-array');

var InfiniteScoller = React.createClass({
  propTypes: {
    averageElementHeight: React.PropTypes.number.isRequired,
    containerHeight: React.PropTypes.number.isRequired,
    preloadRowStart: React.PropTypes.number.isRequired,
    renderRow: React.PropTypes.func.isRequired,
    rowData: React.PropTypes.array.isRequired,
  },

  onEditorScroll: function(event) {
    var infiniteContainer = event.currentTarget;
    var visibleRowsContainer = React.findDOMNode(this.refs.visibleRowsContainer);
    var currentAverageElementHeight = (visibleRowsContainer.getBoundingClientRect().height / this.state.visibleRows.length);
    this.oldRowStart = this.rowStart;
    var newRowStart;
    var distanceFromTopOfVisibleRows = infiniteContainer.getBoundingClientRect().top - visibleRowsContainer.getBoundingClientRect().top;
    var distanceFromBottomOfVisibleRows = visibleRowsContainer.getBoundingClientRect().bottom - infiniteContainer.getBoundingClientRect().bottom;
    var rowsToAdd;
    if (distanceFromTopOfVisibleRows < 0) {
      if (this.rowStart > 0) {
        rowsToAdd = Math.ceil(-1 * distanceFromTopOfVisibleRows / currentAverageElementHeight);
        newRowStart = this.rowStart - rowsToAdd;

        if (newRowStart < 0) {
          newRowStart = 0;
        } 

        this.prepareVisibleRows(newRowStart, this.state.visibleRows.length);
      }
    } else if (distanceFromBottomOfVisibleRows < 0) {
      //scrolling down, so add a row below
      var rowsToGiveOnBottom = this.props.rowData.length - 1 - this.rowEnd;
      if (rowsToGiveOnBottom > 0) {
        rowsToAdd = Math.ceil(-1 * distanceFromBottomOfVisibleRows / currentAverageElementHeight);
        newRowStart = this.rowStart + rowsToAdd;

        if (newRowStart + this.state.visibleRows.length >= this.props.rowData.length) {
          //the new row start is too high, so we instead just append the max rowsToGiveOnBottom to our current preloadRowStart
          newRowStart = this.rowStart + rowsToGiveOnBottom;
        }
        this.prepareVisibleRows(newRowStart, this.state.visibleRows.length);
      }
    } else {
      //we haven't scrolled enough, so do nothing
    }
    this.updateTriggeredByScroll = true;
    //set the averageElementHeight to the currentAverageElementHeight
    // setAverageRowHeight(currentAverageElementHeight);
  },

  componentWillReceiveProps: function(nextProps) {
    var rowStart = this.rowStart;
    var newNumberOfRowsToDisplay = this.state.visibleRows.length;
    this.props.rowData = nextProps.rowData;
    this.prepareVisibleRows(rowStart, newNumberOfRowsToDisplay);
  },

  componentWillUpdate: function() {
    var visibleRowsContainer = React.findDOMNode(this.refs.visibleRowsContainer);
    this.soonToBeRemovedRowElementHeights = 0;
    this.numberOfRowsAddedToTop = 0;
    if (this.updateTriggeredByScroll === true) {
      this.updateTriggeredByScroll = false;
      var rowStartDifference = this.oldRowStart - this.rowStart;
      if (rowStartDifference < 0) {
        // scrolling down
        for (var i = 0; i < -rowStartDifference; i++) {
          var soonToBeRemovedRowElement = visibleRowsContainer.children[i];
          if (soonToBeRemovedRowElement) {
            var height = soonToBeRemovedRowElement.getBoundingClientRect().height;
            this.soonToBeRemovedRowElementHeights += this.props.averageElementHeight - height;
            // this.soonToBeRemovedRowElementHeights.push(soonToBeRemovedRowElement.getBoundingClientRect().height);
          }
        }
      } else if (rowStartDifference > 0) {
        this.numberOfRowsAddedToTop = rowStartDifference;
      }
    }
  },

  componentDidUpdate: function() {
    //strategy: as we scroll, we're losing or gaining rows from the top and replacing them with rows of the "averageRowHeight"
    //thus we need to adjust the scrollTop positioning of the infinite container so that the UI doesn't jump as we 
    //make the replacements
    var infiniteContainer = React.findDOMNode(this.refs.infiniteContainer);
    var visibleRowsContainer = React.findDOMNode(this.refs.visibleRowsContainer);
    var self = this;
    if (this.soonToBeRemovedRowElementHeights) {
      infiniteContainer.scrollTop = infiniteContainer.scrollTop + this.soonToBeRemovedRowElementHeights;
    }
    if (this.numberOfRowsAddedToTop) {
      //we're adding rows to the top, so we're going from 100's to random heights, so we'll calculate the differenece
      //and adjust the infiniteContainer.scrollTop by it
      var adjustmentScroll = 0;

      for (var i = 0; i < this.numberOfRowsAddedToTop; i++) {
        var justAddedElement = visibleRowsContainer.children[i];
        if (justAddedElement) {
          adjustmentScroll += this.props.averageElementHeight - justAddedElement.getBoundingClientRect().height;
          var height = justAddedElement.getBoundingClientRect().height;
        }
      }
      infiniteContainer.scrollTop = infiniteContainer.scrollTop - adjustmentScroll;
    }

    var visibleRowsContainer = React.findDOMNode(this.refs.visibleRowsContainer);
    if (!visibleRowsContainer.childNodes[0]) {
      if (this.props.rowData.length) {
        //we've probably made it here because a bunch of rows have been removed all at once
        //and the visible rows isn't mapping to the row data, so we need to shift the visible rows
        var numberOfRowsToDisplay = this.numberOfRowsToDisplay || 4;
        var newRowStart = this.props.rowData.length - numberOfRowsToDisplay;
        if (!areNonNegativeIntegers([newRowStart])) {
          newRowStart = 0;
        }
        this.prepareVisibleRows(newRowStart , numberOfRowsToDisplay);
        return; //return early because we need to recompute the visible rows
      } else {
        throw new Error('no visible rows!!');
      }
    }
    var adjustInfiniteContainerByThisAmount;

    //check if the visible rows fill up the viewport
    //tnrtodo: maybe put logic in here to reshrink the number of rows to display... maybe...
    if (visibleRowsContainer.getBoundingClientRect().height / 2 <= this.props.containerHeight) {
      //visible rows don't yet fill up the viewport, so we need to add rows
      if (this.rowStart + this.state.visibleRows.length < this.props.rowData.length) {
        //load another row to the bottom
        this.prepareVisibleRows(this.rowStart, this.state.visibleRows.length + 1);
      } else {
        //there aren't more rows that we can load at the bottom so we load more at the top
        if (this.rowStart - 1 > 0) {
          this.prepareVisibleRows(this.rowStart - 1, this.state.visibleRows.length + 1); //don't want to just shift view
        } else if (this.state.visibleRows.length < this.props.rowData.length) {
          this.prepareVisibleRows(0, this.state.visibleRows.length + 1);
        }
      }
    } else if (visibleRowsContainer.getBoundingClientRect().top > infiniteContainer.getBoundingClientRect().top) {
      //scroll to align the tops of the boxes
      adjustInfiniteContainerByThisAmount = visibleRowsContainer.getBoundingClientRect().top - infiniteContainer.getBoundingClientRect().top;
      //   this.adjustmentScroll = true;
      infiniteContainer.scrollTop = infiniteContainer.scrollTop + adjustInfiniteContainerByThisAmount;
    } else if (visibleRowsContainer.getBoundingClientRect().bottom < infiniteContainer.getBoundingClientRect().bottom) {
      //scroll to align the bottoms of the boxes
      adjustInfiniteContainerByThisAmount = visibleRowsContainer.getBoundingClientRect().bottom - infiniteContainer.getBoundingClientRect().bottom;
      //   this.adjustmentScroll = true;
      infiniteContainer.scrollTop = infiniteContainer.scrollTop + adjustInfiniteContainerByThisAmount;
    }
  },

  componentWillMount: function(argument) {
    //this is the only place where we use preloadRowStart
    var newRowStart = 0;
    if (this.props.preloadRowStart < this.props.rowData.length) {
      newRowStart = this.props.preloadRowStart;
    }
    this.prepareVisibleRows(newRowStart, 4);
  },

  componentDidMount: function(argument) {
    //call componentDidUpdate so that the scroll position will be adjusted properly
    //(we may load a random row in the middle of the sequence and not have the infinte container scrolled properly initially, so we scroll to the show the rowContainer)
    this.componentDidUpdate();
  },

  prepareVisibleRows: function(rowStart, newNumberOfRowsToDisplay) { //note, rowEnd is optional
    //setting this property here, but we should try not to use it if possible, it is better to use
    //this.state.visibleRowData.length
    this.numberOfRowsToDisplay = newNumberOfRowsToDisplay;
    var rowData = this.props.rowData;
    if (rowStart + newNumberOfRowsToDisplay > this.props.rowData.length) {
      this.rowEnd = rowData.length - 1;
    } else {
      this.rowEnd = rowStart + newNumberOfRowsToDisplay - 1;
    }
    // var visibleRows = this.state.visibleRowsDataData.slice(rowStart, this.rowEnd + 1);
    // rowData.slice(rowStart, this.rowEnd + 1);
    // setPreloadRowStart(rowStart);
    this.rowStart = rowStart;
    if (!areNonNegativeIntegers([this.rowStart, this.rowEnd])) {
      var e = new Error('Error: row start or end invalid!');
      console.warn('e.trace', e.trace);
      throw e;
    }
    var newVisibleRows = rowData.slice(this.rowStart, this.rowEnd + 1);
    this.setState({
      visibleRows: newVisibleRows
    });
  },
  getVisibleRowsContainerDomNode: function() {
    return this.refs.visibleRowsContainer.getDOMNode();
  },


  render: function() {
    var self = this;
    var rowItems = this.state.visibleRows.map(function(row) {
      return self.props.renderRow(row);
    });

    var rowHeight = this.currentAverageElementHeight ? this.currentAverageElementHeight : this.props.averageElementHeight;
    this.topSpacerHeight = this.rowStart * rowHeight;
    this.bottomSpacerHeight = (this.props.rowData.length - 1 - this.rowEnd) * rowHeight;

    var infiniteContainerStyle = {
      height: this.props.containerHeight,
      overflowY: "scroll",
    };
    return (
      <div
        ref="infiniteContainer"
        className="infiniteContainer"
        style={infiniteContainerStyle}
        onScroll={this.onEditorScroll}
        >
          <div ref="topSpacer" className="topSpacer" style={{height: this.topSpacerHeight}}/>
          <div ref="visibleRowsContainer" className="visibleRowsContainer">
            {rowItems}
          </div>
          <div ref="bottomSpacer" className="bottomSpacer" style={{height: this.bottomSpacerHeight}}/>
      </div>
    );
  }
});

module.exports = InfiniteScoller;
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.