UNPKG

2.62 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 {_dateToUTCString,_getField} from './row-util';
7/**
8 *获取row中某一列的值
9 */
10const getValue = function (fieldName) {
11 return _getField(this,fieldName)['value']
12}
13
14/**
15 * 获取子表值 ,如果fieldName对应了一个子表,返回该子表的行数组
16 * @param fieldName
17 */
18const getChildValue = function(fieldName){
19 var nameArr = fieldName.split('.');
20 var _name = nameArr[0];
21 for (var i = 0, count = nameArr.length; i<count; i++){
22 var _value = this.getValue(_name);
23 //最后一级
24 if (i == count -1){
25 if (_value instanceof u.DataTable){
26 return _value.rows.peek();
27 }else{
28 return _value;
29 }
30 }else{
31 if (_value instanceof u.DataTable){
32 _value = _value.getCurrentRow();
33 if (!_value)
34 return '';
35 else
36 return _value.getChildValue(fieldName.replace(_name + '.', ''))
37 }else{
38 _name = _name + '.' + nameArr[i+1];
39 }
40
41 }
42 }
43 return '';
44};
45
46/**
47 * @private
48 * 提交数据到后台
49 */
50const getData = function () {
51 var data = ko.toJS(this.data)
52 var meta = this.parent.getMeta()
53 for (var key in meta) {
54 if (meta[key] && meta[key].type) {
55 if (meta[key].type == 'date' || meta[key].type == 'datetime') {
56 if(key.indexOf('.')>0){//大于0说明是多级json
57 var keys=key.split('.');
58 var _keyValue=data;
59 for(var i=0,count=keys.length;i<count;i++){
60 _keyValue=_keyValue[keys[i]];
61 }
62 _keyValue.value =_dateToUTCString(_keyValue.value);
63
64 }else{
65 data[key].value = _dateToUTCString(data[key].value)
66 }
67 } else if(meta[key].type == 'child') {
68 var chiddt = this.getValue(key),
69 rs = chiddt.rows(),
70 cds = [];
71 for(var i=0;i < rs.length;i++) {
72 cds.push(rs[i].getData());
73 }
74 data[key].value = JSON.stringify(cds);
75 }
76 }
77 }
78 return {'id': this.rowId, 'status': this.status, data: data}
79}
80
81const getEmptyData = function () {
82 return {'id': this.rowId, 'status': this.status, data: {}}
83};
84
85export {
86 getValue,
87 getChildValue,
88 getData,
89 getEmptyData
90}
\No newline at end of file