找回密码
 立即注册

QQ登录

只需一步,快速开始

一抹阳光。

注册会员

1

主题

4

帖子

11

积分

注册会员

积分
11
最新发帖
一抹阳光。
注册会员   /  发表于:2020-5-19 16:34  /   查看:3975  /  回复:8
10金币
wj-flex-gird 中单元格编辑开始的事件,创建一个文本编辑器,把输入的code放到编辑器中,
单元格编辑结束后的事件,根据输入code去DB取数据,取到name,并通过itemSource绑到grid表里。
焦点在单元格中(编辑中)时,显示的是编辑器里的code,失去焦点(编辑结束)时,显示的是name。

最佳答案

查看完整内容

回填判断出了点问题,参考这段: 注意代码中的注释,如果需要替换获取后端数据,请在注释处获取。

8 个回复

倒序浏览
最佳答案
最佳答案
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2020-5-19 16:34:27
来自 7#
本帖最后由 KevinChen 于 2020-5-20 14:37 编辑

回填判断出了点问题,参考这段:

注意代码中的注释,如果需要替换获取后端数据,请在注释处获取。
  1. <div class="blockcode"><blockquote>import 'bootstrap.css';
  2. import '@grapecity/wijmo.styles/wijmo.css';
  3. import './styles.css';
  4. import * as wjGrid from '@grapecity/wijmo.grid';
  5. import * as wjInput from '@grapecity/wijmo.input';
  6. import * as wjCore from '@grapecity/wijmo';
  7. //
  8. // *** CustomGridEditor class (transpiled from TypeScript) ***
  9. //

  10. var _toString = function(){
  11.     return this.name;
  12. }
  13. var code_name_source = [
  14.     {code: 1, name: "壹", toString:_toString},{code: 2, name: "贰", toString:_toString},{code: 3, name: "叁", toString:_toString}
  15.     ];

  16. export class CustomGridEditor {
  17.     /**
  18.      * Initializes a new instance of a CustomGridEditor.
  19.      */
  20.     constructor(flex, binding, edtClass, options) {
  21.         // save references
  22.         this._grid = flex;
  23.         this._col = flex.columns.getColumn(binding);
  24.         // create editor
  25.         this._ctl = new edtClass(document.createElement('div'), options);
  26.         // connect grid events
  27.         flex.beginningEdit.addHandler(this._beginningEdit, this);
  28.         flex.sortingColumn.addHandler(() => {
  29.             this._commitRowEdits();
  30.         });
  31.         flex.scrollPositionChanged.addHandler(() => {
  32.             if (this._ctl.containsFocus()) {
  33.                 flex.focus();
  34.             }
  35.         });
  36.         flex.selectionChanging.addHandler((s, e) => {
  37.             if (e.row != s.selection.row) {
  38.                 this._commitRowEdits();
  39.             }
  40.         });
  41.         // connect editor events
  42.         this._ctl.addEventListener(this._ctl.hostElement, 'keydown', (e) => {
  43.             switch (e.keyCode) {
  44.                 case wjCore.Key.Tab:
  45.                 case wjCore.Key.Enter:
  46.                     e.preventDefault(); // TFS 255685
  47.                     this._closeEditor(true);
  48.                     this._grid.focus();
  49.                     // forward event to the grid so it will move the selection
  50.                     var evt = document.createEvent('HTMLEvents');
  51.                     evt.initEvent('keydown', true, true);
  52.                     'altKey,metaKey,ctrlKey,shiftKey,keyCode'.split(',').forEach((prop) => {
  53.                         evt[prop] = e[prop];
  54.                     });
  55.                     this._grid.hostElement.dispatchEvent(evt);
  56.                     break;
  57.                 case wjCore.Key.Escape:
  58.                     this._closeEditor(false);
  59.                     this._grid.focus();
  60.                     break;
  61.             }
  62.         });
  63.         // close the editor when it loses focus
  64.         this._ctl.lostFocus.addHandler(() => {
  65.             setTimeout(() => {
  66.                 if (!this._ctl.containsFocus()) {
  67.                     this._closeEditor(true); // apply edits and close editor
  68.                     this._grid.onLostFocus(); // commit item edits if the grid lost focus
  69.                 }
  70.             });
  71.         });
  72.         // commit edits when grid loses focus
  73.         this._grid.lostFocus.addHandler(() => {
  74.             setTimeout(() => {
  75.                 if (!this._grid.containsFocus() && !CustomGridEditor._isEditing) {
  76.                     this._commitRowEdits();
  77.                 }
  78.             });
  79.         });
  80.         // open drop-down on f4/alt-down
  81.         this._grid.addEventListener(this._grid.hostElement, 'keydown', (e) => {
  82.             // open drop-down on f4/alt-down
  83.             this._openDropDown = false;
  84.             if (e.keyCode == wjCore.Key.F4 ||
  85.                 (e.altKey && (e.keyCode == wjCore.Key.Down || e.keyCode == wjCore.Key.Up))) {
  86.                 var colIndex = this._grid.selection.col;
  87.                 if (colIndex > -1 && this._grid.columns[colIndex] == this._col) {
  88.                     this._openDropDown = true;
  89.                     this._grid.startEditing(true);
  90.                     e.preventDefault();
  91.                 }
  92.             }
  93.             // commit edits on Enter (in case we're at the last row, TFS 268944)
  94.             if (e.keyCode == wjCore.Key.Enter) {
  95.                 this._commitRowEdits();
  96.             }
  97.         }, true);
  98.         // close editor when user resizes the window
  99.         // REVIEW: hides editor when soft keyboard pops up (TFS 326875)
  100.         window.addEventListener('resize', () => {
  101.             if (this._ctl.containsFocus()) {
  102.                 this._closeEditor(true);
  103.                 this._grid.focus();
  104.             }
  105.         });
  106.     }
  107.     // gets an instance of the control being hosted by this grid editor
  108.     get control() {
  109.         return this._ctl;
  110.     }
  111.     // handle the grid's beginningEdit event by canceling the built-in editor,
  112.     // initializing the custom editor and giving it the focus.
  113.     _beginningEdit(grid, args) {
  114.         // check that this is our column
  115.         if (grid.columns[args.col] != this._col) {
  116.             return;
  117.         }
  118.         // check that this is not the Delete key
  119.         // (which is used to clear cells and should not be messed with)
  120.         var evt = args.data;
  121.         if (evt && evt.keyCode == wjCore.Key.Delete) {
  122.             return;
  123.         }
  124.         // cancel built-in editor
  125.         args.cancel = true;
  126.         // save cell being edited
  127.         this._rng = args.range;
  128.         CustomGridEditor._isEditing = true;
  129.         // initialize editor host
  130.         var rcCell = grid.getCellBoundingRect(args.row, args.col), rcBody = document.body.getBoundingClientRect(), ptOffset = new wjCore.Point(-rcBody.left, -rcBody.top), zIndex = (args.row < grid.frozenRows || args.col < grid.frozenColumns) ? '3' : '';
  131.         wjCore.setCss(this._ctl.hostElement, {
  132.             position: 'absolute',
  133.             left: rcCell.left - 1 + ptOffset.x,
  134.             top: rcCell.top - 1 + ptOffset.y,
  135.             width: rcCell.width + 1,
  136.             height: grid.rows[args.row].renderHeight + 1,
  137.             borderRadius: '0px',
  138.             zIndex: zIndex,
  139.         });
  140.         // initialize editor content
  141.         if (!wjCore.isUndefined(this._ctl['text'])) {
  142.             var cell_val = grid.getCellData(this._rng.row, this._rng.col, true);
  143.             var edit_val = null;
  144.             code_name_source.forEach(function(item){
  145.                 if(item.name == cell_val){
  146.                     edit_val = item.code;
  147.                 }
  148.             });
  149.             if(!edit_val){
  150.                 edit_val = grid.getCellData(this._rng.row, this._rng.col, true);
  151.             }
  152.             this._ctl['text'] = edit_val;
  153.         }
  154.         else {
  155.             throw 'Can\'t set editor value/text...';
  156.         }
  157.         // start editing item
  158.         var ecv = grid.editableCollectionView, item = grid.rows[args.row].dataItem;
  159.         if (ecv && item && item != ecv.currentEditItem) {
  160.             setTimeout(function () {
  161.                 grid.onRowEditStarting(args);
  162.                 ecv.editItem(item);
  163.                 grid.onRowEditStarted(args);
  164.             }, 50); // wait for the grid to commit edits after losing focus
  165.         }
  166.         // activate editor
  167.         document.body.appendChild(this._ctl.hostElement);
  168.         this._ctl.focus();
  169.         setTimeout(() => {
  170.             // get the key that triggered the editor
  171.             var key = (evt && evt.charCode > 32)
  172.                 ? String.fromCharCode(evt.charCode)
  173.                 : null;
  174.             // get input element in the control
  175.             var input = this._ctl.hostElement.querySelector('input');
  176.             // send key to editor
  177.             if (input) {
  178.                 if (key) {
  179.                     input.value = key;
  180.                     wjCore.setSelectionRange(input, key.length, key.length);
  181.                     var evtInput = document.createEvent('HTMLEvents');
  182.                     evtInput.initEvent('input', true, false);
  183.                     input.dispatchEvent(evtInput);
  184.                 }
  185.                 else {
  186.                     input.select();
  187.                 }
  188.             }
  189.             // give the control focus
  190.             if (!input && !this._openDropDown) {
  191.                 this._ctl.focus();
  192.             }
  193.             // open drop-down on F4/alt-down
  194.             if (this._openDropDown && this._ctl instanceof wjInput.DropDown) {
  195.                 this._ctl.isDroppedDown = true;
  196.                 this._ctl.dropDown.focus();
  197.             }
  198.         }, 50);
  199.     }
  200.     // close the custom editor, optionally saving the edits back to the grid
  201.     _closeEditor(saveEdits) {
  202.         if (this._rng) {
  203.             var flexGrid = this._grid, ctl = this._ctl, host = ctl.hostElement;
  204.             // raise grid's cellEditEnding event
  205.             var e = new wjGrid.CellEditEndingEventArgs(flexGrid.cells, this._rng);
  206.             flexGrid.onCellEditEnding(e);
  207.             // save editor value into grid
  208.             if (saveEdits) {
  209.                 if (!wjCore.isUndefined(ctl['value'])) {
  210.                     let val = ctl['value'];
  211.                     var setVal = null;
  212.                     // 先从code_name_source里面找
  213.                     code_name_source.forEach(function(item){
  214.                         if(item.code == val){
  215.                             setVal = item;
  216.                         }
  217.                     });
  218.                     if(!setVal){
  219.                         setVal = val;
  220.                     }
  221.                     this._grid.setCellData(this._rng.row, this._rng.col, setVal);
  222.                 }
  223.                 else if (!wjCore.isUndefined(ctl['text'])) {
  224.                     let val = ctl['text'];
  225.                     var setVal = null;
  226.                     // 先从code_name_source里面找
  227.                     code_name_source.forEach(function(item){
  228.                         if(item.code == val){
  229.                             setVal = item;
  230.                         }
  231.                     });
  232.                     if(!setVal){
  233.                         setVal = val;
  234.                     }
  235.                     this._grid.setCellData(this._rng.row, this._rng.col, setVal);
  236.                 }
  237.                 else {
  238.                     throw 'Can\'t get editor value/text...';
  239.                 }
  240.                 this._grid.invalidate();
  241.             }
  242.             // close editor and remove it from the DOM
  243.             if (ctl instanceof wjInput.DropDown) {
  244.                 ctl.isDroppedDown = false;
  245.             }
  246.             host.parentElement.removeChild(host);
  247.             this._rng = null;
  248.             CustomGridEditor._isEditing = false;
  249.             // raise grid's cellEditEnded event
  250.             flexGrid.onCellEditEnded(e);
  251.         }
  252.     }
  253.     // commit row edits, fire row edit end events (TFS 339615)
  254.     _commitRowEdits() {
  255.         var flexGrid = this._grid, ecv = flexGrid.editableCollectionView;
  256.         this._closeEditor(true);
  257.         if (ecv && ecv.currentEditItem) {
  258.             var e = new wjGrid.CellEditEndingEventArgs(flexGrid.cells, flexGrid.selection);
  259.             ecv.commitEdit();
  260.             setTimeout(() => {
  261.                 flexGrid.onRowEditEnding(e);
  262.                 flexGrid.onRowEditEnded(e);
  263.                 flexGrid.invalidate();
  264.             });
  265.         }
  266.     }
  267. }
  268. //
  269. document.readyState === 'complete' ? init() : window.onload = init;
  270. //
  271. function init() {
  272.     //
  273.     // create some random data
  274.     var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(',');
  275.     var products = [
  276.         { id: 0, name: 'Widget', unitPrice: 23.43 },
  277.         { id: 1, name: 'Gadget', unitPrice: 12.33 },
  278.         { id: 2, name: 'Doohickey', unitPrice: 53.07 }
  279.     ];
  280.     var data = [];
  281.     var dt = new Date();
  282.     for (var i = 0; i < 100; i++) {
  283.         data.push({
  284.             id: i,
  285.             date: new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60),
  286.             time: new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60),
  287.             country: countries[Math.floor(Math.random() * countries.length)],
  288.             product: products[Math.floor(Math.random() * products.length)].name,
  289.             amount: Math.random() * 10000 - 5000,
  290.             discount: Math.random() / 4
  291.         });
  292.     }
  293.     //
  294.     // grid with custom editors
  295.     var theGrid = new wjGrid.FlexGrid('#theGrid', {
  296.         keyActionTab: 'CycleOut',
  297.         autoGenerateColumns: false,
  298.         itemsSource: data,
  299.         columns: [
  300.             { header: 'ID', binding: 'id', width: 40, isReadOnly: true },
  301.             { header: 'Date', binding: 'date', format: 'd' },
  302.             { header: 'Time', binding: 'time', format: 't' },
  303.             { header: 'Country', binding: 'country' },
  304.             { header: 'Product', binding: 'product' },
  305.             { header: 'Amount', binding: 'amount', format: 'n2' }
  306.         ],
  307.     });

  308.     /**
  309.      * 改为AutoComplete
  310.     */
  311.     new CustomGridEditor(theGrid, 'country', wjInput.AutoComplete, {
  312.         itemsSource: [],
  313.         showDropDownButton:false
  314.     });
  315. }
  316. //# sourceMappingURL=CustomGridEditor.js.map
复制代码


回复 使用道具 举报
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2020-5-19 16:43:01
2#
你好,您的描述很详细,这个问题需要时间来制作Demo,明天中午前给你答复。
回复 使用道具 举报
一抹阳光。
注册会员   /  发表于:2020-5-19 16:48:11
3#
KevinChen 发表于 2020-5-19 16:43
你好,您的描述很详细,这个问题需要时间来制作Demo,明天中午前给你答复。

好的,谢谢
回复 使用道具 举报
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2020-5-20 09:17:47
4#
不客气,中午之前给你回复。
回复 使用道具 举报
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2020-5-20 12:32:47
5#
参考以下代码:

  1. import 'bootstrap.css';
  2. import '@grapecity/wijmo.styles/wijmo.css';
  3. import './styles.css';
  4. import * as wjGrid from '@grapecity/wijmo.grid';
  5. import * as wjInput from '@grapecity/wijmo.input';
  6. import * as wjCore from '@grapecity/wijmo';
  7. //
  8. // *** CustomGridEditor class (transpiled from TypeScript) ***
  9. //

  10. var _toString = function(){
  11.     return this.name;
  12. }
  13. var code_name_source = [
  14.     {code: 1, name: "壹", toString:_toString},{code: 2, name: "贰", toString:_toString},{code: 3, name: "叁", toString:_toString}
  15.     ];

  16. export class CustomGridEditor {
  17.     /**
  18.      * Initializes a new instance of a CustomGridEditor.
  19.      */
  20.     constructor(flex, binding, edtClass, options) {
  21.         // save references
  22.         this._grid = flex;
  23.         this._col = flex.columns.getColumn(binding);
  24.         // create editor
  25.         this._ctl = new edtClass(document.createElement('div'), options);
  26.         // connect grid events
  27.         flex.beginningEdit.addHandler(this._beginningEdit, this);
  28.         flex.sortingColumn.addHandler(() => {
  29.             this._commitRowEdits();
  30.         });
  31.         flex.scrollPositionChanged.addHandler(() => {
  32.             if (this._ctl.containsFocus()) {
  33.                 flex.focus();
  34.             }
  35.         });
  36.         flex.selectionChanging.addHandler((s, e) => {
  37.             if (e.row != s.selection.row) {
  38.                 this._commitRowEdits();
  39.             }
  40.         });
  41.         // connect editor events
  42.         this._ctl.addEventListener(this._ctl.hostElement, 'keydown', (e) => {
  43.             switch (e.keyCode) {
  44.                 case wjCore.Key.Tab:
  45.                 case wjCore.Key.Enter:
  46.                     e.preventDefault(); // TFS 255685
  47.                     this._closeEditor(true);
  48.                     this._grid.focus();
  49.                     // forward event to the grid so it will move the selection
  50.                     var evt = document.createEvent('HTMLEvents');
  51.                     evt.initEvent('keydown', true, true);
  52.                     'altKey,metaKey,ctrlKey,shiftKey,keyCode'.split(',').forEach((prop) => {
  53.                         evt[prop] = e[prop];
  54.                     });
  55.                     this._grid.hostElement.dispatchEvent(evt);
  56.                     break;
  57.                 case wjCore.Key.Escape:
  58.                     this._closeEditor(false);
  59.                     this._grid.focus();
  60.                     break;
  61.             }
  62.         });
  63.         // close the editor when it loses focus
  64.         this._ctl.lostFocus.addHandler(() => {
  65.             setTimeout(() => {
  66.                 if (!this._ctl.containsFocus()) {
  67.                     this._closeEditor(true); // apply edits and close editor
  68.                     this._grid.onLostFocus(); // commit item edits if the grid lost focus
  69.                 }
  70.             });
  71.         });
  72.         // commit edits when grid loses focus
  73.         this._grid.lostFocus.addHandler(() => {
  74.             setTimeout(() => {
  75.                 if (!this._grid.containsFocus() && !CustomGridEditor._isEditing) {
  76.                     this._commitRowEdits();
  77.                 }
  78.             });
  79.         });
  80.         // open drop-down on f4/alt-down
  81.         this._grid.addEventListener(this._grid.hostElement, 'keydown', (e) => {
  82.             // open drop-down on f4/alt-down
  83.             this._openDropDown = false;
  84.             if (e.keyCode == wjCore.Key.F4 ||
  85.                 (e.altKey && (e.keyCode == wjCore.Key.Down || e.keyCode == wjCore.Key.Up))) {
  86.                 var colIndex = this._grid.selection.col;
  87.                 if (colIndex > -1 && this._grid.columns[colIndex] == this._col) {
  88.                     this._openDropDown = true;
  89.                     this._grid.startEditing(true);
  90.                     e.preventDefault();
  91.                 }
  92.             }
  93.             // commit edits on Enter (in case we're at the last row, TFS 268944)
  94.             if (e.keyCode == wjCore.Key.Enter) {
  95.                 this._commitRowEdits();
  96.             }
  97.         }, true);
  98.         // close editor when user resizes the window
  99.         // REVIEW: hides editor when soft keyboard pops up (TFS 326875)
  100.         window.addEventListener('resize', () => {
  101.             if (this._ctl.containsFocus()) {
  102.                 this._closeEditor(true);
  103.                 this._grid.focus();
  104.             }
  105.         });
  106.     }
  107.     // gets an instance of the control being hosted by this grid editor
  108.     get control() {
  109.         return this._ctl;
  110.     }
  111.     // handle the grid's beginningEdit event by canceling the built-in editor,
  112.     // initializing the custom editor and giving it the focus.
  113.     _beginningEdit(grid, args) {
  114.         // check that this is our column
  115.         if (grid.columns[args.col] != this._col) {
  116.             return;
  117.         }
  118.         // check that this is not the Delete key
  119.         // (which is used to clear cells and should not be messed with)
  120.         var evt = args.data;
  121.         if (evt && evt.keyCode == wjCore.Key.Delete) {
  122.             return;
  123.         }
  124.         // cancel built-in editor
  125.         args.cancel = true;
  126.         // save cell being edited
  127.         this._rng = args.range;
  128.         CustomGridEditor._isEditing = true;
  129.         // initialize editor host
  130.         var rcCell = grid.getCellBoundingRect(args.row, args.col), rcBody = document.body.getBoundingClientRect(), ptOffset = new wjCore.Point(-rcBody.left, -rcBody.top), zIndex = (args.row < grid.frozenRows || args.col < grid.frozenColumns) ? '3' : '';
  131.         wjCore.setCss(this._ctl.hostElement, {
  132.             position: 'absolute',
  133.             left: rcCell.left - 1 + ptOffset.x,
  134.             top: rcCell.top - 1 + ptOffset.y,
  135.             width: rcCell.width + 1,
  136.             height: grid.rows[args.row].renderHeight + 1,
  137.             borderRadius: '0px',
  138.             zIndex: zIndex,
  139.         });
  140.         // initialize editor content
  141.         if (!wjCore.isUndefined(this._ctl['text'])) {
  142.             var cell_val = grid.getCellData(this._rng.row, this._rng.col, true);
  143.             if(cell_val && cell_val.code){
  144.                 // 先从code_name_source里面找
  145.                 code_name_source.forEach(function(item){
  146.                     if(item.code == cell_val.code){
  147.                         this._ctl['text'] = item.name;
  148.                     }
  149.                 });
  150.             }else{
  151.                 this._ctl['text'] = grid.getCellData(this._rng.row, this._rng.col, true);
  152.             }
  153.         }
  154.         else {
  155.             throw 'Can\'t set editor value/text...';
  156.         }
  157.         // start editing item
  158.         var ecv = grid.editableCollectionView, item = grid.rows[args.row].dataItem;
  159.         if (ecv && item && item != ecv.currentEditItem) {
  160.             setTimeout(function () {
  161.                 grid.onRowEditStarting(args);
  162.                 ecv.editItem(item);
  163.                 grid.onRowEditStarted(args);
  164.             }, 50); // wait for the grid to commit edits after losing focus
  165.         }
  166.         // activate editor
  167.         document.body.appendChild(this._ctl.hostElement);
  168.         this._ctl.focus();
  169.         setTimeout(() => {
  170.             // get the key that triggered the editor
  171.             var key = (evt && evt.charCode > 32)
  172.                 ? String.fromCharCode(evt.charCode)
  173.                 : null;
  174.             // get input element in the control
  175.             var input = this._ctl.hostElement.querySelector('input');
  176.             // send key to editor
  177.             if (input) {
  178.                 if (key) {
  179.                     input.value = key;
  180.                     wjCore.setSelectionRange(input, key.length, key.length);
  181.                     var evtInput = document.createEvent('HTMLEvents');
  182.                     evtInput.initEvent('input', true, false);
  183.                     input.dispatchEvent(evtInput);
  184.                 }
  185.                 else {
  186.                     input.select();
  187.                 }
  188.             }
  189.             // give the control focus
  190.             if (!input && !this._openDropDown) {
  191.                 this._ctl.focus();
  192.             }
  193.             // open drop-down on F4/alt-down
  194.             if (this._openDropDown && this._ctl instanceof wjInput.DropDown) {
  195.                 this._ctl.isDroppedDown = true;
  196.                 this._ctl.dropDown.focus();
  197.             }
  198.         }, 50);
  199.     }
  200.     // close the custom editor, optionally saving the edits back to the grid
  201.     _closeEditor(saveEdits) {
  202.         if (this._rng) {
  203.             var flexGrid = this._grid, ctl = this._ctl, host = ctl.hostElement;
  204.             // raise grid's cellEditEnding event
  205.             var e = new wjGrid.CellEditEndingEventArgs(flexGrid.cells, this._rng);
  206.             flexGrid.onCellEditEnding(e);
  207.             // save editor value into grid
  208.             if (saveEdits) {
  209.                 if (!wjCore.isUndefined(ctl['value'])) {
  210.                     let val = ctl['value'];
  211.                     var setVal = null;
  212.                     // 先从code_name_source里面找
  213.                     code_name_source.forEach(function(item){
  214.                         if(item.code == val){
  215.                             setVal = item;
  216.                         }
  217.                     });
  218.                     if(!setVal){
  219.                         setVal = val;
  220.                     }
  221.                     this._grid.setCellData(this._rng.row, this._rng.col, setVal);
  222.                 }
  223.                 else if (!wjCore.isUndefined(ctl['text'])) {
  224.                     let val = ctl['text'];
  225.                     var setVal = null;
  226.                     // 先从code_name_source里面找
  227.                     code_name_source.forEach(function(item){
  228.                         if(item.code == val){
  229.                             setVal = item;
  230.                         }
  231.                     });
  232.                     if(!setVal){
  233.                         setVal = val;
  234.                     }
  235.                     this._grid.setCellData(this._rng.row, this._rng.col, setVal);
  236.                 }
  237.                 else {
  238.                     throw 'Can\'t get editor value/text...';
  239.                 }
  240.                 this._grid.invalidate();
  241.             }
  242.             // close editor and remove it from the DOM
  243.             if (ctl instanceof wjInput.DropDown) {
  244.                 ctl.isDroppedDown = false;
  245.             }
  246.             host.parentElement.removeChild(host);
  247.             this._rng = null;
  248.             CustomGridEditor._isEditing = false;
  249.             // raise grid's cellEditEnded event
  250.             flexGrid.onCellEditEnded(e);
  251.         }
  252.     }
  253.     // commit row edits, fire row edit end events (TFS 339615)
  254.     _commitRowEdits() {
  255.         var flexGrid = this._grid, ecv = flexGrid.editableCollectionView;
  256.         this._closeEditor(true);
  257.         if (ecv && ecv.currentEditItem) {
  258.             var e = new wjGrid.CellEditEndingEventArgs(flexGrid.cells, flexGrid.selection);
  259.             ecv.commitEdit();
  260.             setTimeout(() => {
  261.                 flexGrid.onRowEditEnding(e);
  262.                 flexGrid.onRowEditEnded(e);
  263.                 flexGrid.invalidate();
  264.             });
  265.         }
  266.     }
  267. }
  268. //
  269. document.readyState === 'complete' ? init() : window.onload = init;
  270. //
  271. function init() {
  272.     //
  273.     // create some random data
  274.     var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(',');
  275.     var products = [
  276.         { id: 0, name: 'Widget', unitPrice: 23.43 },
  277.         { id: 1, name: 'Gadget', unitPrice: 12.33 },
  278.         { id: 2, name: 'Doohickey', unitPrice: 53.07 }
  279.     ];
  280.     var data = [];
  281.     var dt = new Date();
  282.     for (var i = 0; i < 100; i++) {
  283.         data.push({
  284.             id: i,
  285.             date: new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60),
  286.             time: new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60),
  287.             country: countries[Math.floor(Math.random() * countries.length)],
  288.             product: products[Math.floor(Math.random() * products.length)].name,
  289.             amount: Math.random() * 10000 - 5000,
  290.             discount: Math.random() / 4
  291.         });
  292.     }
  293.     //
  294.     // grid with custom editors
  295.     var theGrid = new wjGrid.FlexGrid('#theGrid', {
  296.         keyActionTab: 'CycleOut',
  297.         autoGenerateColumns: false,
  298.         itemsSource: data,
  299.         columns: [
  300.             { header: 'ID', binding: 'id', width: 40, isReadOnly: true },
  301.             { header: 'Date', binding: 'date', format: 'd' },
  302.             { header: 'Time', binding: 'time', format: 't' },
  303.             { header: 'Country', binding: 'country' },
  304.             { header: 'Product', binding: 'product' },
  305.             { header: 'Amount', binding: 'amount', format: 'n2' }
  306.         ],
  307.     });

  308.     /**
  309.      * 改为AutoComplete
  310.     */
  311.     new CustomGridEditor(theGrid, 'country', wjInput.AutoComplete, {
  312.         itemsSource: [],
  313.         showDropDownButton:false
  314.     });
  315. }
  316. //# sourceMappingURL=CustomGridEditor.js.map
复制代码


把在线示例的app.js替换为以上代码,在country列中输入,1、2、3可以看到效果:

https://demo.grapecity.com.cn/wi ... ustomEditors/purejs
回复 使用道具 举报
一抹阳光。
注册会员   /  发表于:2020-5-20 14:05:38
6#

编辑中的时候怎么显示code不显示name,编辑结束后才显示name
回复 使用道具 举报
一抹阳光。
注册会员   /  发表于:2020-5-20 17:49:16
8#
KevinChen 发表于 2020-5-20 14:32
回填判断出了点问题,参考这段:

注意代码中的注释,如果需要替换获取后端数据,请在注释处获取。

已收到,谢谢
回复 使用道具 举报
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2020-5-20 18:13:03
9#
不客气~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部