Forum: wxRuby ask gridtablebase

Posted by hendra kusuma (Guest)
on 2010-09-28 14:59
(Received via mailing list)
Dear all, I need help

I intent to make a gridtablebase class to save my data temporary and
display it on a grid
Due to my assumption, if I create a gridtablebase object and connect
it to a grid object, the grid should display what's in gridtablebase
object

here is part of the code,

class Gridtablebase_ds < Wx::GridTableBase
  def initialize()
    super
    @data = []
    @col_names = ['Col1', 'Col2']
    @data << ['1', 'John']
    @data << ['1', 'Doe']
  end

  def get_value(row, col)
    (@data[row])[col]
  end

  def append_data(data=[])
    @data << data
  end

  #some other code here
end

in my other class, I use Gridtablebase_ds as follow

@ds = Detil_Gridtablebase_ds.new
@grid.set_table @ds
@grid.refresh

until now it works fine, but then I tried to add data to 
Gridtablebase_ds object

@ds.append_data ['3', 'McGyver']

but the grid doesn't change, and I can't find any clue to where the 
mistakes is
What is wrong here, perhaps I miss something

Thank you
Regards
Hendra
Posted by Alex Fenton (Guest)
on 2010-09-28 15:29
(Received via mailing list)
hi hendra

On 28/09/2010 13:58, hendra kusuma wrote:
> Due to my assumption, if I create a gridtablebase object and connect
> it to a grid object, the grid should display what's in gridtablebase
> object

yes, but ...

> until now it works fine, but then I tried to add data to Gridtablebase_ds object
>
> @ds.append_data ['3', 'McGyver']
>
> but the grid doesn't change

... there is no automatic trigger to update the visual display when the
underlying table changes. call @grid.refresh if you need to.

best
alex
Posted by hendra kusuma (Guest)
on 2010-09-29 17:54
(Received via mailing list)
On Tue, Sep 28, 2010 at 9:09 PM, Alex Fenton <alex@pressure.to> wrote:

> yes, but ...
> ... there is no automatic trigger to update the visual display when the
> underlying table changes. call @grid.refresh if you need to.
>

Thanks Alex
I already done that and it is not working
well, I found the answer from archive email anyway
back then somebody asked the same thing and the conclusion was
wxgrid seems to copy the gridtablebase object.
I guess there is no direct connection between gridtablebase object and 
the
one wxgrid copy
and due to my experiment,
I can't even modify the object with get_table method
something like wxgrid.get_table.add_data(['3', 'McGyver']) also does not
work

I think it was you who said that it is the behaviour of wxwidget, so it 
will
stay like that.
But perhaps I am wrong

I end up making a datasource object to handle the situation and
refresh the grid my remaking the gridtablebase object and re-set the 
grid
table

Thanks
Posted by hendra kusuma (Guest)
on 2010-10-08 10:33
(Received via mailing list)
On Wed, Sep 29, 2010 at 11:41 PM, hendra kusuma 
<penguinroad@gmail.com>wrote:

>>> it to a grid object, the grid should display what's in gridtablebase
>>>
> back then somebody asked the same thing and the conclusion was
> But perhaps I am wrong
>
> I end up making a datasource object to handle the situation and
> refresh the grid my remaking the gridtablebase object and re-set the grid
> table
>
> Thanks
>
>

I was wrong. You're right Alex,

it is possible to update grid with @grid.refresh
I rewrite my gridtablebase object and now it can handle @grid.refresh

here is my code

require 'rubygems'
require 'wx'

class Dataset_h < Wx::GridTableBase
    def initialize(par={})
        super()
        if par[:colnames]     then @colnames = par[:colnames] else 
@colnames
= [] end
        if par[:keys]         then @keys = par[:keys]         else @keys 
=
[]     end
        if par[:rs]         then set_rs(par[:rs])             else @rs =
[]        end

        @display_data = @rs
        @rows = @display_data.size
        @cols = @keys.size
    end

    def get_number_rows
        @rows
    rescue
        0
    end

    def get_number_cols
        @cols
    rescue
        0
    end
    def get_value(row, col)
        ((@display_data[row])[@keys[col].to_sym]).to_s
    rescue
        ""
    end

    def get_attr(row, col, attr_kind)
        Wx::GridCellAttr.new
    end

    def is_empty_cell(row, col)
        true
    end

    def get_col_label_value(col)
        @colnames[col]
    rescue
        ""
    end

    def set_keys(keys=[])
        @keys = keys
    end

    def set_rs(rs)
        @rs = []
        rs.each do |key, value|
            @rs << value
        end
    end

    def auto_rs(rs)
        set_rs(rs)
        @display_data = @rs
        @keys = rs.keys

        @colnames = []
        @keys.each do |key|
            @colnames << key.gsub('_', ' ').capitalize
        end
    end

    def filter(par)
        tmp = []
        @rs.each do |row|
            par.each do |key, value|
                if row[key.to_sym] == value
                    tmp << row
                end
            end
        end

        @display_data = tmp.uniq
        @rows =  @display_data.size
    end

    def filter_like(par)
        tmp = []
        @rs.each do |row|
            par.each do |key, value|
                if row[key.to_sym].to_s.index(value)
                    tmp << row
                end
            end
        end

        @display_data = tmp.uniq
        @rows =  @display_data.size
    end

    def add(data)
        @rs << data
    end

    def delete(row)
        @rs[row] = nil
        @rs.compact
    end

    def all
        @display_data = @rs
    end

    def first
        @display_data.first
    end

    def last
        @display_data.last
    end
end

but then I found another problem
I want this object to be a memory storage, where I can add, update, or
delete and filter rows.

but when I modify data in gridtablebase object, grid is updated, but not
quite perfectly
the grid does not update its row number (and perhaps col number, I have 
not
tried that)
so when I filtered, there is empty row below last row

even worse when I add data, if gridtablebase data row counts more than 
grid
row
grid will not append rows

is there a way to handle this? since this is so close to what I need,
I really hope there is a way to handle this problem

Thank you
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.