On Thu, Aug 20, 2009 at 4:18 AM, Martin Hawkins<martin.hawkins at gmail.com> wrote:> I''m loading data from a SQL table using activerecord and displaying it in a > table. At the moment I''m iterating through the table,so the code is as > follows:<snip>> I want to be able to edit, add and delete. Using SEL_REPLACED I can detect > when a cell has been edited and so can update the record based on row and > column position. This is not good enough as the SQL table entries are not necessarily > unique.I don''t understand what the fact that SQL table entries aren''t necessarily unique has to do with it. When a table cell is edited, the table sends a SEL_REPLACED message to its target, and the message data is an FXTableRange instance indicating which cell(s) were modified. So you should be able to do something like this to determine which row and column was edited: table.connect(SEL_REPLACED) do |sender, sel, table_range| puts "finished editing the cell at row=#{fm.row}, col=#{fm.col}" end Hope this helps, Lyle
Lyle, Thanks for the reply. It''s about keeping the SQL table and the FX table in sync. My sql table is sorted by last name and the table ids don''t match up with the row numbers in the fx_table. I''ve created a column which is 0 wide that contains the sql_id so that when I identify the item that''s been modified (using the row and column information from SEL_REPLACED, as you suggest), I know which SQL table row to update. I then pull back the data from the SQL table so that the edited name is in the correct position and re-draw the table. The edited name is then highlighted using makPositionVisible and setCurrentItem. I saw FXDataTarget and was hoping that the rather clunky code I''ve got could be made a lot neater by using it, rather like cocoa bindings I suppose. I see that FXDataTarget can''t be used with an Array so guess I''m stuck with my sledgehammer approach. On Aug 20, 3:16?pm, Lyle Johnson <l... at lylejohnson.name> wrote:> On Thu, Aug 20, 2009 at 4:18 AM, Martin Hawkins<martin.hawk... at gmail.com> wrote: > > I''m loading data from a SQL table using activerecord and displaying it in a > > table. At the moment I''m iterating through the table,so the code is as > > follows: > > <snip> > > > I want to be able to edit, add and delete. Using SEL_REPLACED I can detect > > when a cell has been edited and so can update the record based on row and > > column position. This is not good enough as the SQL table entries are not necessarily > > unique. > > I don''t understand what the fact that SQL table entries aren''t > necessarily unique has to do with it. When a table cell is edited, the > table sends a SEL_REPLACED message to its target, and the message data > is an FXTableRange instance indicating which cell(s) were modified. So > you should be able to do something like this to determine which row > and column was edited: > > ? ? table.connect(SEL_REPLACED) do |sender, sel, table_range| > ? ? ? puts "finished editing the cell at row=#{fm.row}, col=#{fm.col}" > ? ? end > > Hope this helps, > > Lyle > _______________________________________________ > fxruby-users mailing list > fxruby-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/fxruby-users
Phillipe, Many thanks for the suggestion. The idea of adding a ''cancel all changes option'' is an interesting one and the commit at the end of all of all the changes would certainly make coding easier and neater and response times shorter as the tables grow. On Aug 20, 10:54?am, "Philippe Lang" <philippe.l... at attiksystem.ch> wrote:> Hi Martin, > > I''m not sure it''s the best idea to update your data while the user is working with the GUI. I think you have better let the user fill and change your table, and commit all the changes at the end, by comparing the activerecord content, and your table content. This way, you get an extra feature as well: the ability the CANCEL all the changes made to the table. > > When doing the comparison, you basically have to do 3 different things: > > 1) Detect that a line was deleted in the table, and delete the record properly. > > 2) Detect that a line was inserted in the table, and insert the record properly. > > 3) Detect tha a line was update in the table (one or more columns), and update the record properly. > > I suggest you use in the table a column that stores your record id. You can make it invisible if you want. > > This is how we do it inhttp://www.attiksystem.ch/beerp/beerp-the-fxruby-erp/. It works just fine. > > Good luck! > > Philippe Lang > Attik System > > ? _____ ? > > De : fxruby-users-boun... at rubyforge.org [mailto:fxruby-users-boun... at rubyforge.org] De la part de Martin Hawkins > Envoy? : jeudi, 20. ao?t 2009 11:18 > ? : fxruby-us... at rubyforge.org > Objet : [fxruby-users] Updating rows in a table > > I''m new to using fxruby, so please excuse any daft questions. > > I''m loading data from a SQL table using activerecord and displaying it in a table. At the moment I''m iterating through the table,so the code is as follows: > > def initialize(app) > ? super(app, "Library", :width => 600, :height => 400) > ? @authors = Author.find(:all) > ? table = FXTable.new(self, :opts => LAYOUT_FILL|TABLE_COL_SIZABLE) > ? table.setTableSize(@authors.length, 2) > ? table.rowHeaderMode = LAYOUT_FIX_WIDTH > ? table.rowHeaderWidth = 0 > ? table.setColumnText( 0, "Last Name") > ? table.setColumnText( 1, "First Name") > ? r, c = 0, 0 > ? @authors.each do |author| > ? ?table.setItemText(r, c, author.author_last_name) > ? ?table.setItemJustify(r, c, FXTableItem::LEFT) > ? ?table.setItemText(r, c + 1, author.author_first_name) > ? ?table.setItemJustify(r, c + 1, FXTableItem::LEFT) > ? ?r += 1 > ? end > end > > I want to be able to edit, add and delete. Using SEL_REPLACED I can detect when a cell has been edited and so can update the record based on row and column position. > This is not good enough as the SQL table entries are not necessarily unique. > I presume that I''m supposed to use FXDataTarget - but I''m really not sure how. > I haven''t even started looking at add and delete. > Any pointers would be greatly appreciated. > Martin > > _______________________________________________ > fxruby-users mailing list > fxruby-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/fxruby-users
On Aug 20, 2009, at 10:34 AM, Martin Hawkins wrote:> It''s about keeping the SQL table and the FX table in sync.<snip> Oh, OK. Yes, you''ll need to do something to keep track of which table row is associated with which database record. Instead of storing the sql_id in an invisible column, you could maybe store the sql_id in the table item''s "data" field, e.g. table.setItemData(r, c, author.sql_id) and then later: sql_id = table.getItemData(table_range.fm.row, table_range.fm.col) Hope this helps, Lyle