Copy data from one table to another

I have a search results table from which I want to copy a row on selection to another table.

Ex: I have a “person” table with two columns, person_id and person_name. User can add a row in person table using ‘add person’ button. But instead of adding a blank row to the table, I want to open a modal dialog with search panel and search results table, on which a row can be selected and then added to “person” table.

In search results table, I have an “add” button in every row, and I used following code in on-click event:

var rowId = $(this).up('tr').id;
var rowModel = CAF.model(rowId);

var tableId = $(this).up('table').id;
var tableModel = CAF.model(tableId);

var personTableModel = CAF.model('#{caf:cid("personTable")}'); 
personTableModel .add(0, rowModel);

This code adds a blank row instead of values from search table’s selected row.

Further, is there any way to select columns that I can copy from selected row to another table. Say, if search results table contains person_id, first_name, last_name, ssn, then I want to copy ony person_id and first_name to “person” table.

To automatically populate the controls of a newly added row, the default behavior is to copy the value of controls with the same relative ID from the original row model into the new row.

So, if you use the same IDs for the controls under the source table and target table, it should do it automatically for you.

Otherwise, you can populate the new row with data by supplying your own script with something like this:


var tableId = $(this).up('table').id;  
var tableModel = CAF.model(tableId);  

var personTableModel = CAF.model('#{caf:cid("personTable")}');   

//populate the "values" for the controls in the new row where "field1" and "field2" 
// are the relative Ids of controls in the new row
var newRowData = {
        values: {
            field1: "myvalue1",
            field2: "myvalue2"
        }
};
personTableModel.add(0, newRowData);