jquery-ui - jQuery UI 可使用 React.js 进行排序

标签 jquery-ui reactjs jquery-ui-sortable reactjs-flux

我在 React 中有一个由 jQuery UI 提供支持的可排序列表。当我拖放列表中的项目时,我想更新数组,以便将列表的新顺序存储在那里。然后使用更新后的数组重新渲染页面。即 this.setState({data: _todoList});

目前,当您拖放某个项目时,jQuery UI DnD 可以工作,但该项目在 UI 中的位置不会改变,即使页面使用更新后的数组重新呈现也是如此。即在 UI 中,即使定义其位置的数组已成功更新,该项目也会恢复到其原来在列表中的位置。

如果您拖放该项目两次,它就会移动到正确的位置。

    // Enable jQuery UI Sortable functionality
    $(function() {
      $('.bank-entries').sortable({
        axis: "y",
        containment: "parent",
        tolerance: "pointer",
        revert: 150,
        start: function (event, ui) {
            ui.item.indexAtStart = ui.item.index();
        },
        stop: function (event, ui) {
            var data = {
                indexStart: ui.item.indexAtStart,
                indexStop: ui.item.index(),
                accountType: "bank"
            };
            AppActions.sortIndexes(data);
        },
      });
    });

    // This is the array that holds the positions of the list items
    var _todoItems = {bank: []};

    var AppStore = assign({}, EventEmitter.prototype, {
      getTodoItems: function() {
        return _todoItems;
      },
      emitChange: function(change) {
        this.emit(change);
      },
      addChangeListener: function(callback) {
        this.on(AppConstants.CHANGE_EVENT, callback);
      },
      sortTodo: function(todo) {
        // Dynamically choose which Account to target
        targetClass = '.' + todo.accountType + '-entries';

        // Define the account type
        var accountType = todo.accountType;

        // Loop through the list in the UI and update the arrayIndexes
        // of items that have been dragged and dropped to a new location
        // newIndex is 0-based, but arrayIndex isn't, hence the crazy math
        $(targetClass).children('form').each(function(newIndex) {
          var arrayIndex = Number($(this).attr('data-array-index'));
          if (newIndex + 1 !== arrayIndex) {
            // Update the arrayIndex of the element
            _todoItems[accountType][arrayIndex-1].accountData.arrayIndex = newIndex + 1;
          }
        });

        // Sort the array so that updated array items move to their correct positions
        _todoItems[accountType].sort(function(a, b){
          if (a.accountData.arrayIndex > b.accountData.arrayIndex) {
            return 1;
          }
          if (a.accountData.arrayIndex < b.accountData.arrayIndex) {
            return -1;
          }
          // a must be equal to b
          return 0;
        });

        // Fire an event that re-renders the UI with the new array
        AppStore.emitChange(AppConstants.CHANGE_EVENT);
      },
    }


  function getAccounts() {
    return { data: AppStore.getTodoItems() }
  }

  var Account = React.createClass({
      getInitialState: function(){
          return getAccounts();
      },
      componentWillMount: function(){
          AppStore.addChangeListener(this._onChange);

          // Fires action that triggers the initial load
          AppActions.loadComponentData();
      },
      _onChange: function() {
          console.log('change event fired');
          this.setState(getAccounts());
      },
      render: function(){
          return (
              <div className="component-wrapper">
                  <Bank data={this.state.data} />
              </div>
          )
      }
  });

最佳答案

技巧是在 Sortable 的 stop 事件中调用 sortable('cancel'),然后让 React 更新 DOM。

componentDidMount() {
    this.domItems = jQuery(React.findDOMNode(this.refs["items"]))
    this.domItems.sortable({
        stop: (event, ui) => {
            // get the array of new index (http://api.jqueryui.com/sortable/#method-toArray)
            const reorderedIndexes = this.domItems.sortable('toArray', {attribute: 'data-sortable'}) 
            // cancel the sort so the DOM is untouched
            this.domItems.sortable('cancel')
            // Update the store and let React update (here, using Flux)
            Actions.updateItems(Immutable.List(reorderedIndexes.map( idx => this.state.items.get(Number(idx)))))
        }
    })
}

关于jquery-ui - jQuery UI 可使用 React.js 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29725136/

相关文章:

jquery - 使用 jQuery-UI Sortable 控制占位符高度

javascript - 将逗号和哈希分隔的字符串转换为数组

JQuery UI .sortable 按钮可从当前列表中删除并返回到原始列表

jquery - 在拖动 JQuery UI 时更改可排序元素的大小

reactjs - 如何在 React 中处理来自 D3 的大量 DOM 操作以利用其 virtualDOM

javascript - 如何在 Bootstrap 侧导航顶部显示 jQuery-ui 自动完成?

javascript - 如何在显示 block 上使用 ease Out Bounce 动画?

jquery-ui - jQuery datepicker 没有出现在 Firefox 中

django - 如何让 Django 和 ReactJS 协同工作?

javascript - handleChange 事件与本地存储中的数据不匹配