How to set checked in checkboxgroup

54 views
Skip to first unread message

Dmytro

unread,
Nov 13, 2013, 1:24:27 AM11/13/13
to net...@googlegroups.com
Hi,

Im trying to use checkboxgroup as given below. On the cload I want to set some elements to checked state, but is not able to do this. I've tried to use checked: true, value: true etc, but can get it to work.

Any ideas how can I do it?

xtype: 'checkboxgroup',
items: [
        {  attr_type: :boolean, 
           boxLabel: 'one', 
           name: 'one', value: true, field_label: '', xtype: 'checkbox', read_only: false},

Thanks in advance,
Dmytro

Praveen K

unread,
Nov 13, 2013, 1:31:56 AM11/13/13
to net...@googlegroups.com
Use checked: true configuration. Ex:

{name: 'one', checked: true, field_label: '', xtype: 'checkbox', read_only: false}

Dmytro

unread,
Nov 13, 2013, 1:49:12 AM11/13/13
to net...@googlegroups.com
checked: true does not work. Tried Basepack 0.8.4 and master with Ext 4.1.1a

Dmytro

unread,
Nov 13, 2013, 3:32:06 AM11/13/13
to net...@googlegroups.com
OK, I could make it work, but only by removing :name attribute. Without :name it gives me semi-generated parameters like 'one-inputEl' on submission.

I forgot to mention, that my checkboxes are for fields that are not in the model. They are elements of single Array, that is serialized.

I'd really like to use serialized column for several reasons: 
- this won't be searchable data, 
- there are about 2000 different attributes and 
- they are very sparse. I.e. most of the times attributes will be nil's

Is there a better way to do such thing, without creating model with 2k columns and still be able to use :name on submission?

Thank you,
Dmytro

Praveen K

unread,
Nov 14, 2013, 12:50:29 AM11/14/13
to net...@googlegroups.com
Hi Dmytro,

For this you can use one column to store all attribute values using BLOB datatype(in ruby HASH).

For Ex:

In your model declare:  

 store :data, :accessors =>
      [
        "name",
        "address",
        "phone_number", ...]

and
 
Add data column to table with data type text.

Dmytro Koval'ov

unread,
Nov 14, 2013, 4:59:52 AM11/14/13
to net...@googlegroups.com
Hi Praveen,

Thank you. Haven't used store before, I'm using 

  serialize :groups, Hash

in my model, which I understand provides similar functionality, only there's no way to spcify attribute names.

2013/11/14 Praveen K <pra...@mahaswami.com>

In your model declare:  

 store :data, :accessors =>
      [
        "name",
        "address",
        "phone_number", ...]

With this approach does Netzke supports accessor names, i.e. with :name attribute in layouts?
 
and
 
Add data column to table with data type text.

Thanks a lot again, will try it.

  Dmytro Kovalov  
  http://dmytro.github.com
 

Praveen K

unread,
Nov 15, 2013, 5:51:44 AM11/15/13
to net...@googlegroups.com
Hi Dmytro,

For Serialized data to work in netzke just override the "set_record_value_for_attribute" of "ActiveRecordAdapter" class.
And remove mass assignment condition.

For Ex:


Model:
class Task < ActiveRecord::Base
  attr_accessible :data, :done, :due, :name, :notes, :priority
  validates_presence_of :name

  store :data, :accessors =>
      [
          "first_name",
          "last_name"
      ]

end
 
Component:
class TaskForm < Netzke::Basepack::Form

  def configure(c)
    super
    c.model = "Task"
    c.items = [
        :name,
        :priority,
        :notes,
        :due,
        :done,
        :first_name,
        :last_name
    ]
    c
  end

  require 'netzke/basepack/data_adapters/active_record_adapter'

  module Netzke::Basepack::DataAdapters
    class ActiveRecordAdapter < AbstractAdapter
      def set_record_value_for_attribute(r, a, v, role = :default)
        v = v.to_time_in_current_zone if v.is_a?(Date) # convert Date to Time

        if a[:setter]
          a[:setter].call(r, v)
        elsif r.respond_to?("#{a[:name]}=")# && attribute_mass_assignable?(a[:name], role)
          r.send("#{a[:name]}=", v)
        elsif association_attr?(a)
          split = a[:name].to_s.split(/\.|__/)
          if a[:nested_attribute]
            # We want:
            #     set_value_for_attribute({:name => :assoc_1__assoc_2__method, :nested_attribute => true}, 100)
            # =>
            #     r.assoc_1.assoc_2.method = 100
            split.inject(r) { |r,m| m == split.last ? (r && r.send("#{m}=", v) && r.save) : r.send(m) }
          else
            if split.size == 2
              # search for association and assign it to r
              assoc = @model_class.reflect_on_association(split.first.to_sym)
              assoc_method = split.last
              if assoc
                if assoc.macro == :has_one
                  assoc_instance = r.send(assoc.name)
                  if assoc_instance
                    assoc_instance.send("#{assoc_method}=", v)
                    assoc_instance.save # what should we do when this fails?..
                  else
                    # what should we do in this case?
                  end
                else

                  # set the foreign key to the passed value
                  # not that if a negative value is passed, we reset the association (set it to nil)
                  r.send("#{assoc.foreign_key}=", v.to_i < 0 ? nil : v) if attribute_mass_assignable?(assoc.foreign_key, role)
                end
              else
                logger.debug "Netzke::Basepack: Association #{assoc} is not known for class #{@data_class}"
              end
            else
              logger.debug "Netzke::Basepack: Wrong attribute name: #{a[:name]}"
            end
          end
        end
      end
    end
  end
end
Reply all
Reply to author
Forward
0 new messages