How can I insert a new line in a table view ?

Hallo,

I have a table view with a name-column and several columns for numbers; the numbers are to add in a sum-column. This is OK.
When I insert a new name into the list, the name apeares sorted in the table and the number-columns are editable too.
But from this line on the result is in the sum-column under that line.
I don’t understand that. What can I do?

Heiner

Here are the extractions of handlers:



on cell value changed theObject row theRow table column tableColumn value theValue
	if theObject is table view "tableview" of scroll view "scrollview " of split view "splitView" of box "splitView" of window "main" then
		set theDataSource to mydataSource
		tell theObject
			set sum to (contents of data cell "sum" of data row theRow of theDataSource) -- starts with 0
			try
				set sum to sum + theValue
			end try
			set selectedDataRow to selected data row of theObject
			--log selectedDataRow
			set contents of data cell "sum" of selectedDataRow to sum
			set theValue to 0
		end tell
	end if
end cell value changed


on newInsertion()
	--  ... 
	set theRow to make new data row at the end of the data rows of mydataSource -- data source; glob. Var.
	set contents of data cell "name" of theRow to theName -- read from a dialog
	set contents of data cell "sum" of theRow to 0
	
	--  ...
end newInsertion


Suppose you had a table with 5 columns labelled “name”, “num1”, “num2”, “num3”, “sum”… the following would keep the sum column proper.

on cell value changed theObject row theRow table column tableColumn value theValue
	set theDataSource to data source of table view "tableview" of scroll view "scrollview " of split view "splitView" of box "splitView" of window "main"
	set num1 to contents of data cell "num1" of data row theRow of theDataSource
	set num2 to contents of data cell "num2" of data row theRow of theDataSource
	set num3 to contents of data cell "num3" of data row theRow of theDataSource
	set contents of data cell "sum" of data row theRow of theDataSource to (num1 + num2 + num3)
end cell value changed

Hallo Regulus 6633,

thank you for your answer!

In my case it’s a little more complicated. I have at the beginning 12 columns and the user can add any columns to that. But that’s not the main problem.
Up to that it works. The problem starts when you add a new name-line between the current lines (sorted). Than the result (sum) of the new line apeares not in the new line but in the line under it. I tried some update commands. Nothing!
Meanwhile I think there is a bug in the ‘cell value changed’ - handler when you use the theRow-Variable: It will keep the old value of the row and is not to updating.
So I mad a workaround with the ‘selected data row’-property.

Heiner

If you have a variable number of columns then just get the count of the columns, and use a repeat loop to add the values. Since the name column is always first and the sum column is always last your repeat loop would go from 2 to (number of columns - 1). Add those values to get the sum.

As far as your problem with theRow variable, I can’t say why your sum is getting added to the wrong row. I’d have to see the project to figure out why. If you want to post your project somewhere I’ll take a look at it. It sounds like you can work around it though by using the selected data row instead as you’ve explained.

I was just looking at the applescript studio terminology reference guide. It talks about having a sorted data source. It says the data source actually keeps the data in its unsorted order but presents the data in the table sorted. In order to access the data in its sorted order you use “sorted data rows”.

So that may have something to do with your problem with theRow variable. Maybe you need to add the word sorted wherever you use “data row”, for example…
set num1 to contents of data cell “num1” of sorted data row theRow of theDataSource

As far as the repeat loop concerned I did so with ‘selected data row’. It works.

As far as the code line

set myValue to contents of data cell i of sorted data row theRow of data source of theObject

concerned I get an error message: “A class name can’t go after this property”. The compiler dosn’t like the expression ‘sorted data row’.

Heiner