瀏覽代碼

Table: row-key support for multi-level props(#5607)

* Let getRowIdentity simply support the _.get method similar to lodash.

* Complete doc

* Update table.md

* Update table.md
song 8 年之前
父節點
當前提交
4e073d1bb1
共有 3 個文件被更改,包括 11 次插入3 次删除
  1. 1 1
      examples/docs/en-US/table.md
  2. 1 1
      examples/docs/zh-CN/table.md
  3. 9 1
      packages/table/src/util.js

+ 1 - 1
examples/docs/en-US/table.md

@@ -1704,7 +1704,7 @@ For table of numbers, you can add an extra row at the table footer displaying ea
 | current-row-key | key of current row, a set only prop | string,number | — | — |
 | row-class-name | function that returns custom class names for a row, or a string assigning class names for every row | Function(row, index)/String | — | — |
 | row-style | function that returns custom style for a row,  or a string assigning custom style for every row | Function(row, index)/Object | — | — |
-| row-key | key of row data, used for optimizing rendering. Required if `reserve-selection` is on | Function(row)/String | — | — |
+| row-key | key of row data, used for optimizing rendering. Required if `reserve-selection` is on. When its type is String, multi-level access is supported, e.g. `user.info.id`, but `user.info[0].id` is not supported, in which case `Function` should be used. | Function(row)/String | — | — |
 | empty-text | Displayed text when data is empty. You can customize this area with `slot="empty"` | String | — | No Data |
 | default-expand-all | whether expand all rows by default, only works when the table has a column type="expand" | Boolean | — | false |
 | expand-row-keys | set expanded rows by this prop, prop's value is the keys of expand rows, you should set row-key before using this prop | Array | — | |

+ 1 - 1
examples/docs/zh-CN/table.md

@@ -1765,7 +1765,7 @@
 | current-row-key | 当前行的 key,只写属性 | String,Number | — | — |
 | row-class-name | 行的 className 的回调方法,也可以使用字符串为所有行设置一个固定的 className。 | Function(row, index)/String | — | — |
 | row-style | 行的 style 的回调方法,也可以使用一个固定的 Object 为所有行设置一样的 Style。 | Function(row, index)/Object | — | — |
-| row-key | 行数据的 Key,用来优化 Table 的渲染;在使用 reserve-selection 功能的情况下,该属性是必填的 | Function(row)/String | — | — |
+| row-key | 行数据的 Key,用来优化 Table 的渲染;在使用 reserve-selection 功能的情况下,该属性是必填的。类型为 String 时,支持多层访问:`user.info.id`,但不支持 `user.info[0].id`,此种情况请使用 `Function`。 | Function(row)/String | — | — |
 | empty-text | 空数据时显示的文本内容,也可以通过 `slot="empty"` 设置 | String | — | 暂无数据 |
 | default-expand-all | 是否默认展开所有行,当 Table 中存在 type="expand" 的 Column 的时候有效 | Boolean | — | false |
 | expand-row-keys | 可以通过该属性设置 Table 目前的展开行,需要设置 row-key 属性才能使用,该属性为展开行的 keys 数组。| Array | — | |

+ 9 - 1
packages/table/src/util.js

@@ -85,7 +85,15 @@ export const mousewheel = function(element, callback) {
 export const getRowIdentity = (row, rowKey) => {
   if (!row) throw new Error('row is required when get row identity');
   if (typeof rowKey === 'string') {
-    return row[rowKey];
+    if (rowKey.indexOf('.') < 0) {
+      return row[rowKey];
+    }
+    let key = rowKey.split('.');
+    let current = row;
+    for (let i = 0; i < key.length; i++) {
+      current = current[key[i]];
+    }
+    return current;
   } else if (typeof rowKey === 'function') {
     return rowKey.call(null, row);
   }