function printTable(table , level) if (#table == 0) then return end level = level or 1 local indent = "" for i = 1, level do indent = indent.." " end if key ~= "" then print(indent..key.." ".."=".." ".."{") else print(indent .. "{") end key = "" for k,v in pairs(table) do if type(v) == "table" then key = k PrintTable(v, level + 1) else local content = string.format("%s%s = %s", indent .. " ",tostring(k), tostring(v)) print(content) end end print(indent .. "}") end
时间: 2024-04-17 20:23:10 浏览: 9
这段代码是一个用于打印 Lua 表格(table)的函数。函数名为 `printTable`,接受两个参数:`table` 表示要打印的表格,`level` 表示当前的缩进级别。
首先,函数会检查表格是否为空,如果为空则直接返回。然后初始化缩进字符串 `indent` 为空字符串,并根据 `level` 的值生成相应数量的空格。
接下来,如果存在键值 `key`,则打印带有键值的表格开始符号 `{`,否则只打印表格开始符号 `{`。然后将 `key` 重置为空字符串。
然后使用 `pairs` 遍历表格的键值对。如果值的类型是表格,则将键值 `k` 赋给 `key`,并递归调用 `printTable` 函数打印子表格。否则,将键值 `k` 和值 `v` 格式化成字符串,并打印。
最后,打印表格结束符号 `}`,并根据缩进级别进行缩进。
相关问题
--随机开奖颜色 function gamelogic:OpenResult() --红白颜色的次数 local tRed = 0 local tWhite = 0 local Result = {} for i = 1, 4, 1 do --随机开1或2 1代表红 2代表白 local RandNum = math.random(1,2) table.insert(Result, RandNum) if RandNum %2 == 0 then tRed = tRed + 1 else tWhite = tWhite + 1 end end return tRed, tWhite end --开奖区域 function gamelogic:OpenCardArry(nRed, nWhite) local AreaWinLose = 0 local nMultiple = 0 if nRed %2 == 0 or nWhite %2 == 0 then AreaWinLose = 1 end if nRed %2 ~= 0 or nWhite %2 ~= 0 then AreaWinLose = 2 end if nRed == 4 then AreaWinLose = 3 end if nRed == 4 then AreaWinLose = 4 end if nRed == 1 and nWhite == 3 then AreaWinLose = 5 end if nRed == 3 and nWhite == 1 then AreaWinLose = 6 end if AreaWinLose == 1 or AreaWinLose == 3 or AreaWinLose == 4 then nMultiple = 15 else nMultiple = 3 end return AreaWinLose, nMultiple end 不用位运算有没有其他的方法实现
是的,可以使用取模运算 `%` 来代替位运算符 `&` 和 `|`。例如,将 `RandNum & 1` 替换为 `RandNum % 2`,将 `RandNum | 0` 替换为 `RandNum`。取模运算和位运算的结果在这种情况下是等价的。因此,可以将代码修改为以下内容:
```
function gamelogic:OpenResult()
--红白颜色的次数
local tRed = 0
local tWhite = 0
local Result = {}
for i = 1, 4, 1 do
--随机开1或2 1代表红 2代表白
local RandNum = math.random(1,2)
table.insert(Result, RandNum)
if RandNum % 2 == 0 then
tRed = tRed + 1
else
tWhite = tWhite + 1
end
end
return tRed, tWhite
end
--开奖区域
function gamelogic:OpenCardArry(nRed, nWhite)
local AreaWinLose = 0
local nMultiple = 0
if nRed % 2 == 0 or nWhite % 2 == 0 then
AreaWinLose = 1
end
if nRed % 2 ~= 0 or nWhite % 2 ~= 0 then
AreaWinLose = 2
end
if nRed == 4 then
AreaWinLose = 3
end
if nRed == 4 then
AreaWinLose = 4
end
if nRed == 1 and nWhite == 3 then
AreaWinLose = 5
end
if nRed == 3 and nWhite == 1 then
AreaWinLose = 6
end
if AreaWinLose == 1 or AreaWinLose == 3 or AreaWinLose == 4 then
nMultiple = 15
else
nMultiple = 3
end
return AreaWinLose, nMultiple
end
```
easyui grid = datagrid('#datagrid' 父子table
EasyUI的DataGrid组件是一个非常常用的数据列表展示组件。关于“父子table”的问题,不太清楚你具体指的是什么,但是DataGrid组件支持嵌套子表格,可以通过设置子表格的属性来实现。具体步骤如下:
1. 在DataGrid中定义一个列为“subdg”,用来放置子表格。
2. 通过formatter属性来设置subdg列的显示内容,一般使用一个按钮或者图标来表示展开子表格的操作。
3. 在formatter中绑定一个事件,用来处理展开子表格的逻辑。
4. 在子表格中定义一个与父表格相关联的字段,一般使用rowIndex或者id等来表示。
5. 使用loadFilter属性来定义数据过滤器,用来处理父子表格数据的关系。
下面是一个示例代码,可以参考一下:
```javascript
$('#datagrid').datagrid({
url: 'data.json',
columns: [[
{field:'id',title:'ID',width:100},
{field:'name',title:'Name',width:100},
{field:'subdg',title:'Details',width:100,formatter: function(value,row,index){
return '<a href="#" class="easyui-linkbutton" onclick="showDetails('+index+')">Show Details</a>';
}}
]],
loadFilter: function(data){
//处理数据,将子表格数据装配到父表格中
$.each(data.rows, function(i, row){
row.subdg = '<table><tr><td>ID:</td><td>'+row.id+'</td></tr><tr><td>Name:</td><td>'+row.name+'</td></tr></table>';
});
return data;
}
});
function showDetails(index){
$('#datagrid').datagrid('expandRow',index);
var subdg = $('#datagrid').datagrid('getRowDetail',index).find('table');
subdg.datagrid({
url: 'data.json?parentId='+index,
columns: [[
{field:'id',title:'ID',width:100},
{field:'name',title:'Name',width:100},
]],
onLoadSuccess:function(data){
//处理数据,将子表格数据装配到父表格中
var parentRow = $('#datagrid').datagrid('getRows')[index];
parentRow.subdg = subdg;
$('#datagrid').datagrid('collapseRow',index).datagrid('expandRow',index);
}
});
}
```
希望能够帮到你!