Making One Row Appear After Another
This example can be run in C:\Program Files\Q\Examples\Cola.Q (this may be located on a different place on your computer depending upon how Q was installed). Create a table of Q5. Brand associations by SUMMARY.
- table.rowIndex('Coke') finds the row number in the table with the label of Coke.
- The code looks up the position of the Coke row and the Pepsi row and then instructs Q to rearrange the table so that the Coke row appears after the Pepsi row. That is, the rowIndex code works out the numbers of the rows and the instructions are given to Q in terms of row numbers.
- moveRowAfter and rowIndex are functions written for use in Q and are documented in.
- This code will only work if the table contains rows called Coke and Pepsi. If the tables does not contain these rows an error will be returned:
- The cause of this error is that table.rowIndex('Coke') and table.rowIndex('Pepsi') return values of -1 when these text strings cannot be found. That that the bottom line of the error message identifies that the precise location where the error was encounted (line 1, column 6). A better bit of code which avoids the possibility of this error is:
var coke_row = table.rowIndex('Coke'); var pepsi_row = table.rowIndex('Pepsi'); if (coke_row != -1 && pepsi_row != -1) table.moveRowAfter(coke_row, pepsi_row);
- Note that:
- Custom Sorting of Rows in a Table for specifying the complete ordering of rows in the table.