Skip to content

二维表变量数据管理接口 Table

具体函数名及描述如下:

序号函数名函数描述
1UpdateAllValue(...)更新整张表
2Clear(...)清空表格
3InsertValue(...)在末尾插入一行数据
4InsertValueByRow(...)在指定行插入一行数据
5GetValue(...)获取表格数据
6GetAllValue(...)获取整张表
7SetValue(...)设置单元格或整行数据
8RemoveRow(...)删除行
9GetValuesByCol(...)获取指定列的所有行数据
10GetRows(...)获取行数
11GetCols(...)获取列数
12GetColIndex(...)获取列索引
13GetRowIndex(...)查找行号
14GetRowIndexs(...)查找全部行号
15GetTableColKeys(...)获取列名列表

UpdateAllValue

  • 参数及类型:
    • varId:string 表ID
    • playerId:number / nil 玩家Uin(全局表传nil)
    • value:table 新表数据
  • 返回值及类型:
  • 该方法的其他说明: 更新整张表
  • 具体使用案例如下:
lua
local ret = Data.Table:UpdateAllValue(varId, playerId, value)

Clear

  • 参数及类型:
    • varId:string 表ID
    • playerId:number / nil 玩家Uin(全局表传nil)
  • 返回值及类型:
  • 该方法的其他说明: 清空表格
  • 具体使用案例如下:
lua
   local isSuccess = Data.Table:Clear(varId)

   if isSuccess then

       print("清除表格成功")

   end

   local isSuccessPlayer = Data.Table:Clear(playerVarId, Player:GetHostUin())

   if isSuccessPlayer then

       print("清除玩家表格成功")

   end

InsertValue

  • 参数及类型:
    • varId:string 表ID
    • playerId:number / nil 玩家Uin(全局表传nil)
    • ...:any列值依次传入(中间不可传 nil)
  • 返回值及类型:
  • 该方法的其他说明: 在表末尾追加一行,各列按参数从左到右依次填入不可传nil
  • 具体使用案例如下:
lua
    -- 传参规则(变量id , 玩家uin(全局传nil), 第一列填充数据, 第二列填充数据, 第三列填充数据,...)

    local isSuccess = Data.Table:InsertValue(varId, nil, 2, "aa", false)

    if isSuccess then

        print("插入表格值成功")

    end

    -- 传参规则(变量id , 玩家uin(全局传nil), 第一列填充数据, 第二列填充数据, 第三列填充数据,...)

    local isSuccessPlayer = Data.Table:InsertValue(playerVarId, Player:GetHostUin(), 2, "aaa", false)

    if isSuccessPlayer then

        print("插入玩家表格值成功")

    end

InsertValueByRow

  • 参数及类型:
    • varId:string 表ID
    • playerId:number / nil 玩家Uin(全局表传nil)
    • value:table 列值
    • row:number / nil 插入位置(nil 表示插到末尾)
  • 返回值及类型:
  • 该方法的其他说明: 在指定行号插入一行;列可用数字下标或列名做 key,如{[1] = xx,...} or {[key] = xx,...}。(列值不能传空表,至少要有一列数据)
  • 具体使用案例如下:
lua
    local data = {

        [1] = 2,  -- 第一列填充的数据

        [2] = "sss3", -- 第二列填充的数据

        [3] = false, -- 第三列填充的数据

        -- [4] = {x = 1,y = 7,z = 1}, -- 第四列填充的数据

        -- 。。。。。 根据实际变量的列类型填充

    }

    local isSuccess = Data.Table:InsertValueByRow(varId, nil, data, 1) -- 在第一行插入一行数据

    if isSuccess then

        print("按行插入表格值成功")

    end

    local data2 = {

        [1] = 2, -- 第一列填充的数据

        [2] = "sssa", -- 第二列填充的数据

        [3] = false, -- 第三列填充的数据

        -- [4] = {x = 1,y = 7,z = 1}, -- 第四列填充的数据

        -- 。。。。。 根据实际变量的列类型填充

    }

    local isSuccessPlayer = Data.Table:InsertValueByRow(playerVarId, Player:GetHostUin(), data2) -- 在最后一行插入一行数据

    if isSuccessPlayer then

        print("按行插入玩家表格值成功")

    end

GetValue

  • 参数及类型:
  • 返回值及类型:
    • value:any / nil 单元格值或整行表
  • 该方法的其他说明: 获取表格数据,不写 col 时返回整行(表)。
  • 具体使用案例如下:
lua
    local value = Data.Table:GetValue(varId, nil, 2, 2)

    if value then

        print("获取表格值", value)

    end

    local playerValue = Data.Table:GetValue(playerVarId, Player:GetHostUin(), 2, 1)

    if playerValue then

        print("获取玩家表格值", playerValue)

    end

GetAllValue

  • 参数及类型:
    • varId:string 表ID
    • playerId:number / nil 玩家Uin(全局表传nil)
  • 返回值及类型:
    • value:table / nil 整张表数据
  • 该方法的其他说明: 获取整张表数据
  • 具体使用案例如下:
lua
    -- 获取后的数据格式是

    -- {

    --     {10, "RRR", 0},-- 第一行    

    --     {9, "sss", 0}, -- 第二行

    --     {0, "fff", 0}, -- 第三行

    -- }

    local allValues = Data.Table:GetAllValue(varId)

    if allValues then

        print("获取表格所有值", allValues)

    end

    local playerAllValues = Data.Table:GetAllValue(playerVarId, Player:GetHostUin())

    if playerAllValues then

        print("获取玩家表格所有值", playerAllValues)

    end

SetValue

  • 参数及类型:
    • varId:string 表ID
    • playerId:number / nil 玩家Uin(全局表传nil)
    • row:number / table 行号或行号数组
    • col:number / string / nil 列号或列名(可省略)
    • value:any新值或整行表
  • 返回值及类型:
  • 该方法的其他说明: 设置单元格或整行数据,不写 col 时可以把 value 当成整行一次性写入
  • 具体使用案例如下:
lua
local ret = Data.Table:SetValue(varId, playerId, row, col, value)

RemoveRow

  • 参数及类型:
  • 返回值及类型:
  • 该方法的其他说明: 按行号删除一行或多行数据
  • 具体使用案例如下:
lua
    local isSuccess = Data.Table:RemoveRow(varId, nil, 1) -- 移除第一行

    if isSuccess then

        print("移除表格行成功")

    end

    local isSuccessPlayer = Data.Table:RemoveRow(playerVarId, Player:GetHostUin(), {2, 4}) -- 移除第二行和第四行

    if isSuccessPlayer then

        print("移除玩家表格行成功")

    end

GetValuesByCol

  • 参数及类型:
  • 返回值及类型:
    • ret:table / nil 该列所有行数据
  • 该方法的其他说明: 获取指定列所有行的数据
  • 具体使用案例如下:
lua
    local values = Data.Table:GetValuesByCol(varId, nil, 1)

    if values then

        print("获取表格列值", values)

    end

    local playerValues = Data.Table:GetValuesByCol(playerVarId, Player:GetHostUin(), 2)

    if playerValues then

        print("获取玩家表格列值", playerValues)

    end

GetRows

  • 参数及类型:
    • varId:string 表ID
    • playerId:number / nil 玩家Uin(全局表传nil)
  • 返回值及类型:
  • 该方法的其他说明: 获取当前表有多少行
  • 具体使用案例如下:
lua
    local rows = Data.Table:GetRows(varId)

    if rows then

        print("获取表格行数",rows)

    end

    local size = Data.Table:GetRows(playerVarId, Player:GetHostUin())

    if size then

        print("获取玩家表格行数",size)

    end

GetCols

  • 参数及类型:
    • varId:string 表ID
    • playerId:number / nil 玩家Uin(全局表传nil)
  • 返回值及类型:
  • 该方法的其他说明: 获取当前表有多少列
  • 具体使用案例如下:
lua
    local cols = Data.Table:GetCols(varId)

    if cols then

        print("获取表格列数",cols)

    end

    local num = Data.Table:GetCols(playerVarId, Player:GetHostUin())

    if num then

        print("获取玩家表格列数",num)

    end

GetColIndex

  • 参数及类型:
    • varId:string 表ID
    • playerId:number / nil 玩家Uin(全局表传nil)
    • key:any列名或列 key
  • 返回值及类型:
  • 该方法的其他说明: 根据列名或 key 查列号是第几列
  • 具体使用案例如下:
lua
    local colIndex = Data.Table:GetColIndex(varId, nil, "名称")

    if colIndex then

        print("获取表格列索引", colIndex)

    end

    local playerColIndex = Data.Table:GetColIndex(playerVarId, Player:GetHostUin(), "分数")

    if playerColIndex then

        print("获取玩家表格列索引", playerColIndex)

    end

GetRowIndex

  • 参数及类型:
    • varId:string 表ID
    • playerId:number / nil 玩家Uin(全局表传nil)
    • col:number / string 列号或列名
    • value:any要匹配的值
    • cmp:function / nil 比较函数(可为nil,function(a,value) return a == value end),默认判断值相等ction
  • 返回值及类型:
  • 该方法的其他说明: 在某一列里找第一个和 value 匹配的行号(默认同 ==,可自写比较函数)
  • 具体使用案例如下:
lua
    local rowIndex = Data.Table:GetRowIndex(varId, nil, 2, "sss")

    if rowIndex then

        print("获取表格行索引", rowIndex)

    end

    local playerRowIndex = Data.Table:GetRowIndex(playerVarId, Player:GetHostUin(), 3, true)

    if playerRowIndex then

        print("获取玩家表格行索引", playerRowIndex)

    end

GetRowIndexs

  • 参数及类型:
    • varId:string 表ID
    • playerId:number / nil 玩家Uin(全局表传nil)
    • col:number / string 列号或列名
    • value:any要匹配的值
    • cmp:function / nil 比较函数(可为nil,function(a,value) return a == value end),默认判断值相等ction
  • 返回值及类型:
    • ret:table / nil 行号列表
  • 该方法的其他说明: 在某一列里找出所有和 value 匹配的行号
  • 具体使用案例如下:
lua
    local rowIndexs = Data.Table:GetRowIndexs(varId, nil, 2, "sss")

    if rowIndexs then

        for key, value in pairs(rowIndexs) do

            print("获取表格行索引", value )

        end

    end

    local playerRowIndexs = Data.Table:GetRowIndexs(playerVarId, Player:GetHostUin(), 3, false)

    if playerRowIndexs then

        for key, value in pairs(playerRowIndexs) do

            print("获取玩家表格行索引", value )

        end

    end

GetTableColKeys

  • 参数及类型:
  • 返回值及类型:
    • ret:table / nil 列名列表,失败为 nil
  • 该方法的其他说明: 获取整张表的列名列表
  • 具体使用案例如下:
lua
    local colKeys = Data.Table:GetTableColKeys(varId)