【问题标题】:How to remove dirty flag when editing value in extjs editable grid data?在 extjs 可编辑网格数据中编辑值时如何删除脏标志?
【发布时间】:2018-01-19 22:49:44
【问题描述】:

一个 extjs 可编辑网格数据网格有以下 json 数据命名为

data.json

{
  "rows" : [ {
    "record_id" : 101,
  }, {
    "record_id" : 102,
    "data" : "",
  }, {
    "record_id" : 103,
    "data" : 62,
  }, {
    "record_id" : "104",
    "data" : "62",
  } ]
}

在单元格中切换时,某些数据单元格会显示带有脏标志。实际上,从用户的角度来看,没有任何数据被改变。这是一个演示。当然也有数据丢失,或者字符串,或者整数类型混用。这种情况发生在现实生活中,因为用户不关心编程类型等。问题是如何清理脏标志,因为实际上没有更改数据,并且在提交时,所有类型的数据都会做没有出现在 getChanges() 中?如果发生任何数据更改,例如 62 到 625,它应该显示一个脏标志;如果反向,例如 625 到 62,它不应该显示脏标志,直到单击“保存”。

这是与测试相关的其他文件。

data.html

<!-- <!DOCTYPE html> -->
<html>
<head>
<meta name="google" content="notranslate" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=10, user-scalable=yes">
<title>Gride Data Testing</title>

<script type="text/javascript" src="https://examples.sencha.com/extjs/6.5.1/examples/classic/shared/include-ext.js"></script>
<script type="text/javascript" src="https://examples.sencha.com/extjs/6.5.1/ext-all-debug.js"></script>
<script type="text/javascript" src="https://examples.sencha.com/extjs/6.5.1/examples/classic/shared/options-toolbar.js"></script>

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/extjs/6.2.0/classic/theme-triton/resources/theme-triton-all.css">

<script type="text/javascript" src="data-ext-0.js"></script>

</head>
<body>

<div id="grid-example"></div>

</body>
</html>

data-ext.js

Ext.require([
  'Ext.grid.*',
  'Ext.data.*',
  'Ext.util.*',
  'Ext.state.*' ]);

Ext.onReady(function() {

  var myStore = Ext.create('Ext.data.JsonStore', {
    // http://docs.sencha.com/extjs/6.0.2/modern/Ext.grid.column.Column.html
    storeId : 'myStore_data',
    autoLoad : true,
    autoDestroy : true,
    proxy : {
      type : 'ajax',
      url : 'data.json',  // the file is defined as the above
      reader : {
        type : 'json',
        keepRawData : true,
        rootProperty : 'rows'
      }
    },
  });

  var grid = Ext.create('Ext.grid.Panel', {
    store : Ext.data.StoreManager.lookup('myStore_data'),
    columnLines : true,
    border : true,
    title : 'Grid Data Testing',
    columns : [ {
      text : "Record ID",
      dataIndex : "record_id",
      width : 200,
      format : '0',
      editor : {
        xtype : 'numberfield',
        allowBlank : true,
        allowNull : true,
      }
    }, {
      text : "Data",
      dataIndex : "data",
      width : 200,
      format : '0',
      editor : {
        xtype : 'numberfield',
        allowBlank : true,
        allowNull : true,
      }
    } ],
    selModel : {
      selType : 'cellmodel'
    },
    height : 230,
    width : 402,
    title : 'Grid Data Testing',
    renderTo : 'grid-example',
    viewConfig : {
      stripeRows : true
    },
    plugins : {
      cellediting : {
        clicksToEdit : 1
      }
    }
  });
});

【问题讨论】:

  • 这应该是您在模型级别处理的事情。设置的脏值是多少?
  • @EvanTrimboli javascript 数据是一种 Duck Typing。模型级别有​​些冗余。最好的办法是在不丢失清晰逻辑的情况下用尽可能少的代码实现任务。
  • 为什么文本字段应该知道null'' 之间的区别。如果将值设置为null,然后调用getValue,它应该返回什么?如果将值设置为 null,然后用户输入一个空格,然后按退格键会怎样?这是一个数据级别的问题。

标签: javascript json extjs extjs6


【解决方案1】:

您需要在编辑后提交您的数据,您可以在 celleditor 插件的edit event 上这样做:

我添加了一个控件,仅当开始值和新值相同时才提交,因此如果我更改单元格中的值,则不会提交记录

如果 value 为 null,你还需要检查 originalValue 和 value 是否不为 null

如果您不需要提交,您可以使用此解决方法。 record.set 方法不是通过网格单元格编辑标记计算的。

   cellediting: {
                clicksToEdit: 1,
                listeners: {
                    edit: function (editor, context, e) {
                        if (context.originalValue === context.value ||
                        (!context.originalValue && !context.value)) {
                            context.record.set(context.field,context.value);
                            context.record.set(context.field,context.originalValue);
                        }
                    }
                }
            }

here you can see a working fiddle

代码更新

【讨论】:

  • 代码可能有一个保存动作,这不是我需要的。如果发生任何数据更改,例如 62 到 620,它应该显示一个脏标志,直到单击“保存”。
  • 我正在尝试禁用“dirtyflag”检查单元格内的值是否为空
  • 完成了,请告诉我没关系
  • 答案是有希望的。但是它失败了一些测试用例。 1.用数据填充空白字段,然后将数据删除为空白,它不应该将数据显示为脏。 2.更改任何数据,然后将数据反转回来,它也不应该将数据显示为脏
  • 小提琴为“record_id”添加了“data”:“”:101。这意味着它没有对丢失或未定义的数据进行测试。对于"record_id": 104,数据串改为整数。
猜你喜欢
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多