数据网格中的动态代码排序数据

时间:2014-12-02 16:21:36

标签: livecode

我已成功获取超过20000条记录的数据,下面是代码。我遇到的问题是我需要通过Title对数据网格进行排序。我的数据网格是带有2个字段label1和label2的表单。 如何按标题对数据进行排序?
这是我的代码

Global mydbid
local sCursorID, sRowCursorID
local sRecordFields
local sRecordCount

on preopenCard

     resetgrid
     uiPopulateListGroup
     ## Initialize the UI

     pass preopencard
end preopenCard





command uiPopulateListGroup
   ## Connect to database and get records
   CloseCursor


   OpenDatabase
   OpenMoviesCursor

   ## Cache number of records so we can display useful info 
   ## to the user
   put revNumberOfRecords(sCursorID) into sRecordCount


   ## Track time it takes
   put the seconds into theStart

   lock screen

   ## Setting the dgNumberOfRecords of a data grid turns on dynamic 
   ## record fetching. Rather than storing the data that is being displayed
   ## in an internal array the data grid fires off the GetDataForLine message
   ## in which you return the data for the appropriate line.
   set the dgNumberOfRecords of group "DataGrid1" to sRecordCount


   unlock screen
end uiPopulateListGroup


on shortme
     dispatch "SortDataByKey" to group "DataGrid1" with "title", "text", "descending", "false"
     dispatch "RefreshList" to group "DataGrid1" 
end shortme

## this message is sent to the Data Grid but we handle it here
## so that you can see all handlers in one spot
command GetDataForLine pLine, @pOutData
   ## Revolution 3.5 added revMoveToRecord to revDB. This makes it really
   ## easy to navigate to the proper record for pLine
     revMoveToRecord sCursorID, pLine - 1 -- 0 based record navigation

  put revDatabaseColumnNumbered(sCursorId, 1) into theRowID

   ## The rowID is stored in the cursor. Open another cursor that contains all fields for this record.
   --put revQueryDatabase(mydbid,"SELECT * FROM datadrinks WHERE rowid = " & theRowID)  into sRowCursorID

   put revQueryDatabase(mydbid,"select title,content from datadrinks where rowid = " & theRowID )  into sRowCursorID

   if sRowCursorID is an integer then
      ## Now convert record in the row cursor to an array for the data grid
        put ConvertCurrentRowToArray() into pOutData


      revCloseCursor sRowCursorID
   end if
end GetDataForLine


function ConvertCurrentRowToArray
   local theArray

   if sRecordFields is empty then
      ## Cache the fields in the cursor in a script local variable.
      put revDatabaseColumnNames(sRowCursorID) into sRecordFields
   end if

   ## Converts current record in the cursor into a single dimensional array.
   ## Note we are using the cached script local sRecordFields
   repeat for each item theField in sRecordFields
      put revDatabaseColumnNamed(sRowCursorID, theField) into theArray[theField]
   end repeat

   return theArray
end ConvertCurrentRowToArray


command OpenDatabase
   set the wholematches to true

   if mydbid is not an integer OR mydbid is not among the items of revOpenDatabases() then
      put the filename of this stack into thePath
      set the itemdelimiter to slash

      put "drinksfull.sqlite" into the last item of thePath
      put revOpenDatabase("sqlite",thePath,,,,) into mydbid

      if mydbid is not an integer then
         answer "Error connecting to the database:" && mydbid & "."
         put empty into mydbid
         exit to top
      end if
   end if
end OpenDatabase


command CloseDatabase
    try
        revCloseDatabase mydbid
    catch e
    end try
end CloseDatabase


command OpenMoviesCursor
   ## Selecting 50,000 records can take a bit of time. The method used in this 
   ## example selects the rowid of the 50,000 records and stores that in a cursor.
   ## In GetDataForLine the rowid is used to fetch the entire record. This is much faster.

     //put revQueryDatabase(mydbid,"SELECT rowid FROM datadrinks") into sCursorID
     put revQueryDatabase(mydbid,"SELECT rowid FROM datadrinks where centralcat like 'cock%' ") into sCursorID

    if sCursorID is not an integer then
        answer "Error opening cursor:" && sCursorID & "."
        exit to top
    end if
end OpenMoviesCursor


command CloseCursor
    try
        revCloseCursor sCursorID
    catch e
    end try
end CloseCursor


#command uiViewRecord pID
#     put "SELECT * from datadrinks WHERE ID = " & pID into theSQL
#     put revDataFromQuery(tab, cr, mydbid, theSQL) into theData

#end uiViewRecord

on resetgrid
     send "ResetControl" to group "DataGrid1"
     put empty into sRecordFields
     put empty into sRowCursorID
     put empty into sCursorID
     put empty into sRecordCount
end resetgrid


on closeCard
          put empty into sRecordFields
     ## Close the database connection as we don't need it any longer
          CloseDatabase
end closeCard

1 个答案:

答案 0 :(得分:0)

我通常更清楚地排序,通常提取“dgData”,它将返回制表符并返回分隔文本,排序,然后恢复。

但是你可以在DataGrid里面排序。请参阅第226页的DG指南。

克雷格纽曼