
@ egjs/ list- divers
QQ 模块, 在一个阵列中添加、 删除或更改值时检查 diff .
* 安装
$ npm i @egjs/list-differ
<script src="//naver.github.io/egjs-list-differ/release/latest/dist/list-differ.min.js"></script>
页:1
- 见 文档 页 次.
- 引入列表差异以跟踪数据和跟踪变化(单位:千美元)한국어(中文(简体) )
- 如何建立跨框架构成部分(单位:千美元)한국어(中文(简体) )
• 软件包
| 软件包 | 版本 | 说明 |
|---|---|---|
| @egjs/ 儿童差异 | 一个测试DOM孩子的模块. | |
| @egjs/ react- children-differ (英语) | ||
| @egjs/ngx- 儿童- differ | ||
| @egjs/vue- children-differ (英语) |
如何使用
检查数组中的 diff
import ListDiffer, { diff } from "@egjs/list-differ";
// script => eg.ListDiffer
// Value is key
const differ = new ListDiffer([1, 2, 3, 4, 5, 6, 7], e => e);
// const result = diff([1, 2, 3, 4, 5, 6, 7], [4, 3, 6, 2, 1, 7], e => e);
const result = differ.update([4, 3, 6, 2, 1, 7]);
// List before update
// [1, 2, 3, 4, 5, 6, 7]
console.log(result.prevList);
// Updated list
// [4, 3, 6, 2, 1, 7]
console.log(result.list);
// Index array of values added to `list`
// [2]
console.log(result.added);
// Index array of values removed in `prevList`
// [5, 4]
console.log(result.removed);
// An array of index pairs of `prevList` and `list` with different indexes from `prevList` and `list`
// [[3, 0], [2, 1], [1, 3], [0, 4], [6, 5]]
console.log(result.changed);
// The subset of `changed` and an array of index pairs that moved data directly. Indicate an array of absolute index pairs of `ordered`.(Formatted by: Array<[index of prevList, index of list]>)
// [[3, 0], [2, 1], [1, 3]]
console.log(result.pureChanged);
// An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>)
// [[3, 0], [3, 1], [3, 2]]
console.log(result.ordered);
// An array of index pairs of `prevList` and `list` that have not been added/removed so data is preserved
// [[3, 0], [2, 1], [1, 3], [0, 4], [6, 5]]
console.log(result.maintained);
改变了什么?
- 已更改: 一系列指数对
prevList财务报告和审定财务报表list不同指数prevList财务报告和审定财务报表list - 纯变形: 子集
changed和一系列直接移动数据的索引对。 表示绝对指数对的数组ordered.(由: Array < [prevList的索引,列表索引] 校正)
| 已更改 | 纯已改变 : | |
|---|---|---|
| [[3、0、[2、1]、[1、3]、[0、4]、[6、5]] | [[ [3, ], [2, 1], [1, 3]]]] | |
| 预览列表 | ![]() | ![]() |
| 进程 | - 说吧 | ![]() |
| 列表 | ![]() | ![]() |
什么订单?
一系列索引配对 ordered 能够同步 list 在添加数据之前。 (原载:Array<[prevIndex,下Index])
| 删除 - > 命令 - > 添加 | |
|---|---|
| 预览列表 | ![]() |
| 删除 [5、4] | ![]() |
| 订购 | [[3, 0, [3, 1], [3, 2]]]] |
![]() | |
| 命令 [0] [3=>0] | ![]() |
| 命令 [1] [3=> 1] | ![]() |
| 命令 [2] [3=>2] | ![]() |
| 已添加 [2] | ![]() |
| 列表 | ![]() |
数据同步示例
import ListDiffer, { diff } from "@egjs/list-differ";
const prevList = [1, 2, 3, 4, 5, 6, 7];
const list = [4, 3, 6, 2, 1, 7];
// const differ = new ListDiffer(prevList, e => e);
// const result = differ.update(list);
const result = diff(prevList, list, e => e);
删除qq命令 添加
| 删除qq命令 添加 | |
|---|---|
| 预览列表 | ![]() |
| 进程 | ![]() |
| 列表 | ![]() |
- 同步列表
const nextList = prevList.slice();
result.removed.forEach(index => {
nextList.splice(index, 1);
});
result.ordered.forEach(([from, to], i) => {
nextList.splice(from, 1);
nextList.splice(to, 0, list[result.pureChanged[i][1]]);
});
result.added.forEach(index => {
nextList.splice(index, 0, list[index]);
});
// `nextList` is the same as `list`.
console.log(nextList);
- 同步 DOM
const parentElement = document.querySelector(".parent");
const children = parentElement.children;
result.removed.forEach(index => {
children[index].remove();
});
result.ordered.forEach(([from, to]) => {
parentElement.insertBefore(children[from], children[from < to ? to + 1 : to]);
});
result.added.forEach(index => {
parentElement.insertBefore(document.createElement("div"), children[index]);
});
删除 qq 维护 qq 添加
| 已保留 +++ 添加 | |
|---|---|
| 预览列表 | ![]() |
| 进程 | ![]() |
| 列表 | ![]() |
- 同步列表
const nextList: number[] = [];
result.removed.forEach(index => {
// There is a remove operation for the index.
// prevList[index];
});
result.maintained.forEach(([from, to]) => {
nextList[to] = list[to];
});
result.added.forEach(index => {
nextList[index] = list[index];
});
// `nextList` is the same as `list`.
console.log(nextList);
- 同步 DOM
const parentElement = document.querySelector(".parent");
const children = parentElement.children;
const prevChildren: Element[] = [].slice.call(parentElement.children);
result.removed.forEach(index => {
prevChildren[index].remove();
});
result.maintained.forEach(([from, to]) => {
parentElement.appendChild(prevChildren[from]);
});
result.added.forEach(index => {
parentElement.insertBefore(document.createElement("div"), children[index]);
});
捐款
见 捐款额。 。 。 .
* 反馈
请存档 问题。 。 。 .
许可证
@egjs/list-differ 在下方发布 麻省理工学院许可证。 。 。 .
Copyright (c) 2019-present NAVER Corp.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.













