javascript - Ext 或 Ext_scaffold 中的 bool 字段已损坏?

标签 javascript mysql ruby-on-rails extjs scaffold

我正在尝试使用 ext_scaffold 为 Rails 支持的 Web 应用程序生成 UI 元素。

但是,我遇到了 bool 字段行为的问题。例如,当我这样做时

./script/generate ext_scaffold product name:string description:text sku:integer available:boolean

它正确构建迁移并生成带有 bool 字段复选框的用户界面。

但是,选中此框只会修改正在创建的对象。如果记录已存在,则复选框的状态准确反射(reflect)该字段。但是,编辑它失败 - 即,选中或取消选中它并保存记录不会更改它。

深入研究代码,我们发现:

 items: [
    { fieldLabel: scaffoldPanel.labels['product[name]'], name: 'product[name]', xtype: 'textfield' },
    // ... other fields ...
    { fieldLabel: scaffoldPanel.labels['product[available]'], name: 'product[available]', xtype: 'checkbox', inputValue: '1' }, { xtype: 'hidden', name: 'product[available]', value: '0' }
    ],

我感觉问题是 Rails 或 Ext 对同名元素感到困惑。无论哪种方式,创建记录后,单击复选框不会执行任何操作(选中或未选中,该字段仍为“0”。)

经过一番尝试后,结果发现,如果我们将该隐藏字段放置在复选框上方,则无论编辑后该字段现在设置为 true 是什么。也不是完全理想的行为。

还有其他人遇到过这个问题吗?有什么解决方法吗?谢谢。

更新:

请注意,即使选中了该字段的复选框,发送的 POST 请求也会指示该字段设置为 false。所有其他字段类型都会得到适当更新...

更新2:

好吧,找到了一个很棒的 blog post describing Semergence's solution to this problem ,但我仍然不太能够让它工作......我已经调整了他的解决方案如下:

   onOk: function() { 

      // ....

      if (scaffoldPanel.getFormPanel().currentMode == 'edit') {
        // set up request for Rails create action
        submitOptions.params._method = 'PUT';
        submitOptions.url = submitOptions.url + '/' + selected.data.id;
      }

  // ----- checkbox serializer ------
     var serializedForm = scaffoldPanel.getFormPanel().getForm();

 // because unchecked checkboxes do not submit values, manually force a value of 0  
     serializedForm.items.each(function(f) {  
        //alert("Checking \"" + f.getName() "\" field...");
        if (f.isFormField && f.getXType() == 'checkbox' && !f.getValue()) {  
            alert("Auto setting \"" + f.getName() + "\" field to 0...");
            serializedForm.getValues()[f.getName()] = '0';  
        }
      });
// ------- end serializer -------

     serializedForm.submit(submitOptions);
     //scaffoldPanel.getFormPanel().getForm().submit(submitOptions);  
   },

现在警报已正确抛出,指示正确的字段并断言它将设置为零。

除非这不会发生在实际发送的 POST 请求中——事实上,如果未选中该复选框,则它在 POST 中根本没有值。

有人可以帮我理解这里发生了什么吗?

最佳答案

好吧,事实证明这是 Ext 框架的一个已知错误。如果您遇到同样的问题,请访问 Extjs 论坛,那里概述了几种解决方案。最简单的实现就是扩展复选框类:

# patch.js -- include before creating any checkboxes
Ext.ns('Ext.ux.form');
Ext.ux.form.XCheckbox = Ext.extend(Ext.form.Checkbox, {
 offCls:'xcheckbox-off'
,onCls:'xcheckbox-on'
,disabledClass:'xcheckbox-disabled'
,submitOffValue:'false'
,submitOnValue:'true'
,checked:false

,onRender:function(ct) {
    // call parent
    Ext.ux.form.XCheckbox.superclass.onRender.apply(this, arguments);

    // save tabIndex remove & re-create this.el
    var tabIndex = this.el.dom.tabIndex;
    var id = this.el.dom.id;
    this.el.remove();
    this.el = ct.createChild({tag:'input', type:'hidden', name:this.name, id:id});

    // update value of hidden field
    this.updateHidden();

    // adjust wrap class and create link with bg image to click on
    this.wrap.replaceClass('x-form-check-wrap', 'xcheckbox-wrap');
    this.cbEl = this.wrap.createChild({tag:'a', href:'#', cls:this.checked ? this.onCls : this.offCls});

    // reposition boxLabel if any
    var boxLabel = this.wrap.down('label');
    if(boxLabel) {
        this.wrap.appendChild(boxLabel);
    }

    // support tooltip
    if(this.tooltip) {
        this.cbEl.set({qtip:this.tooltip});
    }

    // install event handlers
    this.wrap.on({click:{scope:this, fn:this.onClick, delegate:'a'}});
    this.wrap.on({keyup:{scope:this, fn:this.onClick, delegate:'a'}});

    // restore tabIndex
    this.cbEl.dom.tabIndex = tabIndex;
} // eo function onRender

,onClick:function(e) {
    if(this.disabled || this.readOnly) {
        return;
    }
    if(!e.isNavKeyPress()) {
        this.setValue(!this.checked);
    }
} // eo function onClick

,onDisable:function() {
    this.cbEl.addClass(this.disabledClass);
    this.el.dom.disabled = true;
} // eo function onDisable

,onEnable:function() {
    this.cbEl.removeClass(this.disabledClass);
    this.el.dom.disabled = false;
} // eo function onEnable

,setValue:function(val) {
    if('string' == typeof val) {
        this.checked = val === this.submitOnValue;
    }
    else {
        this.checked = !(!val);
    }

    if(this.rendered && this.cbEl) {
        this.updateHidden();
        this.cbEl.removeClass([this.offCls, this.onCls]);
        this.cbEl.addClass(this.checked ? this.onCls : this.offCls);
    }
    this.fireEvent('check', this, this.checked);

} // eo function setValue

,updateHidden:function() {
    this.el.dom.value = this.checked ? this.submitOnValue : this.submitOffValue;
} // eo function updateHidden

,getValue:function() {
    return this.checked;
} // eo function getValue

}); // eo extend

// register xtype
Ext.reg('xcheckbox', Ext.ux.form.XCheckbox);

 // eo file 

您还需要一些用于新复选框的 CSS:

.xcheckbox-wrap {
 line-height: 18px;
 padding-top:2px;
}
.xcheckbox-wrap a {
 display:block;
 width:16px;
 height:16px;
 float:left;
}
.x-toolbar .xcheckbox-wrap {
 padding: 0 0 2px 0;
}
.xcheckbox-on {
 background:transparent url(../javascripts/ext/resources/images/default/menu/checked.gif) no-repeat 0 0;
}
.xcheckbox-off {
 background:transparent url(../javascripts/ext/resources/images/default/menu/unchecked.gif) no-repeat 0 0;
}
.xcheckbox-disabled {
 opacity: 0.5;
 -moz-opacity: 0.5;
 filter: alpha(opacity=50);
 cursor:default;
}

最后,您可能想要修复 ext-scaffold 以生成这些新的 bool 值 xcheckbox,并且不生成隐藏字段。我修改了 ext_scaffold_panel.js 如下:

    baseParams: scaffoldPanel.baseParams,
    items: [
<%= attributes.inject([]) do |items, a|
 item =  "        { fieldLabel: scaffoldPanel.labels['#{class_name.demodulize.underscore}[#{a.name}]']"
 item << ", name: '#{class_name.demodulize.underscore}[#{a.name}]'"
 item << case a.field_type
   when :text_field      then [:integer, :float, :decimal].include?(a.type) ? ", xtype: 'numberfield'" : ", xtype: 'textfield'"
   when :text_area       then ", xtype: 'textarea'"
   when :date_select     then ", xtype: 'xdatefield'"
   when :datetime_select then ", xtype: 'xdatetime'"
   when :check_box       then ", xtype: 'xcheckbox', inputValue: '1' //// }, { xtype: 'hidden', name: '#{class_name.demodulize.underscore}[#{a.name}]', value: '0'"
 end
 item << " }"
 items << item
end.join(",\n")
%>
    ],

希望这可以帮助其他遇到此问题的人!

关于javascript - Ext 或 Ext_scaffold 中的 bool 字段已损坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1267667/

相关文章:

Java.lang.IllegalStateException : Already attached

ruby-on-rails - 如何更改路线名称 rails 4

ruby-on-rails - Rails 查询随机模型,直到属性达到一个数量

php - 区分联合内的两个查询

PHP PDO 删除早于 'x'(用户定义)天的条目

ruby-on-rails - rails : convert UTC DateTime to another time zone

javascript - 没有 href、onclick 等的按钮

javascript - 在灯箱中的图像上添加热点链接

javascript - 带有jQuery的鼠标悬停淡入淡出效果按钮

javascript - meteor react : User Object undefined at initial page load