简体   繁体   English

ExtJS 4,自定义Ext.grid.Panel的布尔列值

[英]ExtJS 4, customize boolean column value of Ext.grid.Panel

Good day, i have a grid with boolean column: 美好的一天,我有一个布尔列的网格:

 var grid = Ext.create('Ext.grid.Panel', {
      ...
      columns: [{
           dataIndex: 'visibleForUser',
           text: 'Visible',
           editor: {
              xtype: 'checkboxfield',
              inputValue: 1 // <-- this option has no effect
           }
      },
      ...

Grid's store is remote via JSON proxy. Grid的商店通过JSON代理远程存储。 When i save or update row, the resulting JSON look like: 当我保存或更新行时,生成的JSON看起来像:

{... visibleForUser: false, ... }

As you see, ExtJS serializes checkbox value as true or false JSON terms. 如您所见,ExtJS将复选框值序列化为truefalse JSON术语。 I need to customize this and serialize to, say, 1 and 0 , any suggestion how to accomplish this ? 我需要自定义这个并序列化为10 ,任何建议如何实现这一点? Thank you. 谢谢。

I've just changed my checkboxes system-wide to always act/respond to 0 and 1 : 我刚刚在系统范围内更改了我的复选框以始终执行/响应01

Ext.onReady(function(){
    // Set the values of checkboxes to 1 (true) or 0 (false), 
    // so they work with json and SQL's BOOL field type
    Ext.override(Ext.form.field.Checkbox, { 
        inputValue: '1',
        uncheckedValue: '0'
    });
});

But you can just add this configs per checkbox. 但你可以在每个复选框中添加此配置。

Ext JS 4.1.1 has a new serialize config on record fields. Ext JS 4.1.1在记录字段上有一个新的serialize配置。 When a writer is preparing the record data, the serialize method is called to produce the output value instead of just taking the actual field value. 当编写器正在准备记录数据时,将调用serialize方法以生成输出值,而不是仅获取实际字段值。 So you could do something like this: 所以你可以这样做:

fields: [{
    name: "visibleForUser",
    type: "boolean",
    serialize: function(v){
        return v ? 1 : 0;
    }
    /* other fields */
}]

I try to avoid overriding default component behavior whenever possible. 我尽量避免覆盖默认组件行为。 As I mentioned, this only works in 4.1.1 (it was introduced in 4.1.0 but I believe it was broken). 正如我所提到的,这仅适用于4.1.1(它是在4.1.0中引入的,但我相信它已被破坏)。 So if you're using an earlier version, one of the other answers would suit you better. 因此,如果您使用的是早期版本,其他一个答案将更适合您。

You can try to override getValue 您可以尝试覆盖getValue

Ext.define('Ext.form.field.Checkbox', {
    override : 'Ext.form.field.Checkbox',
    getValue: function () {
        return this.checked ? 1 : 0;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM