search

Home  >  Q&A  >  body text

javascript - Sortable.js 插件 怎么保存排序后的顺序?

Sortable.js 插件 怎么保存排序后的顺序?
不是jquery ui里的.

<ul id="items">
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
</ul>

li都是动态生成的. 该怎么保存排序 和 输出新的排序?

自带属性里的这个属性怎么用? 怎么好像没效.
dataIdAttr: 'data-id',

迷茫迷茫2791 days ago565

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-11 10:11:52

    请参考文档:https://github.com/RubaXa/Sor...

    <ul>
        <li data-id="1">order</li>
        <li data-id="2">save</li>
        <li data-id="3">restore</li>
    </ul>
    Sortable.create(el, {
        group: "localStorage-example",
        store: {
            /**
             * Get the order of elements. Called once during initialization.
             * @param   {Sortable}  sortable
             * @returns {Array}
             */
            get: function (sortable) {
                var order = localStorage.getItem(sortable.options.group.name);
                return order ? order.split('|') : [];
            },
    
            /**
             * Save the order of elements. Called onEnd (when the item is dropped).
             * @param {Sortable}  sortable
             */
            set: function (sortable) {
                // 这里是获取新的排序数组
                var order = sortable.toArray();
                localStorage.setItem(sortable.options.group.name, order.join('|'));
            }
        }
    })

    reply
    0
  • Cancelreply