UNPKG

3.08 kBJavaScriptView Raw
1/**
2 * Module : kero dataTable row getData
3 * Author : liuyk(liuyk@yonyou.com)
4 * Date : 2016-08-08 13:54:01
5 */
6import {
7 rowUtilFunObj
8} from './row-util';
9/**
10 * 获取row中某一字段的值
11 * @memberof Row
12 * @param {string} fieldName 字段名
13 * @return {string} 字段值
14 * @example
15 * row.getValue('field1')
16 */
17const getValue = function(fieldName) {
18 return rowUtilFunObj._getField(this, fieldName)['value']
19}
20
21//获取子表值 ,如果fieldName对应了一个子表,返回该子表的行数组
22const getChildValue = function(fieldName) {
23 var nameArr = fieldName.split('.');
24 var _name = nameArr[0];
25 for (var i = 0, count = nameArr.length; i < count; i++) {
26 var _value = this.getValue(_name);
27 //最后一级
28 if (i == count - 1) {
29 if (_value instanceof u.DataTable) {
30 return _value.rows.peek();
31 } else {
32 return _value;
33 }
34 } else {
35 if (_value instanceof u.DataTable) {
36 _value = _value.getCurrentRow();
37 if (!_value)
38 return '';
39 else
40 return _value.getChildValue(fieldName.replace(_name + '.', ''))
41 } else {
42 _name = _name + '.' + nameArr[i + 1];
43 }
44
45 }
46 }
47 return '';
48};
49
50
51/**
52 * 获取数据信息
53 * @memberof Row
54 * @return {object} 格式如下:{'id': this.rowId, 'status': this.status, data: data}
55 * @example
56 * row.getData()
57 */
58const getData = function() {
59 var data = ko.toJS(this.data)
60 var meta = this.parent.getMeta()
61 for (var key in meta) {
62 if (meta[key] && meta[key].type) {
63 if (meta[key].type == 'date' || meta[key].type == 'datetime') {
64 if (key.indexOf('.') > 0) { //大于0说明是多级json
65 var keys = key.split('.');
66 var _keyValue = data;
67 for (var i = 0, count = keys.length; i < count; i++) {
68 _keyValue = _keyValue[keys[i]];
69 }
70 _keyValue.value = rowUtilFunObj._dateToUTCString(_keyValue.value);
71
72 } else {
73 data[key].value = rowUtilFunObj._dateToUTCString(data[key].value)
74 }
75 } else if (meta[key].type == 'child') {
76 var chiddt = this.getValue(key),
77 rs = chiddt.rows(),
78 cds = [];
79 for (var i = 0; i < rs.length; i++) {
80 cds.push(rs[i].getData());
81 }
82 data[key].value = JSON.stringify(cds);
83 }
84 }
85 }
86 return {
87 'id': this.rowId,
88 'status': this.status,
89 data: data
90 }
91}
92
93// 获取空数据
94const getEmptyData = function() {
95 return {
96 'id': this.rowId,
97 'status': this.status,
98 data: {}
99 }
100};
101
102export const rowGetDataFunObj = {
103 getValue: getValue,
104 getChildValue: getChildValue,
105 getData: getData,
106 getEmptyData: getEmptyData
107}