|
|
| 第142行: |
第142行: |
| -- 返回以逗号和空格分隔的歌曲ID列表 | | -- 返回以逗号和空格分隔的歌曲ID列表 |
| return table.concat(matchingKeys, ', ') | | return table.concat(matchingKeys, ', ') |
| end
| |
| --6. 表格生成函数
| |
| function p.generateListTable(f)
| |
| local args = require('Module:ProcessArgs').merge(true)
| |
|
| |
| local conditionField = mw.text.trim(args.field or '') -- 用于筛选的字段
| |
| local conditionValue = args.value -- 用于筛选的值
| |
| if type(conditionValue) == 'string' then
| |
| conditionValue = mw.text.trim(conditionValue)
| |
| end
| |
| local displayFieldsString = mw.text.trim(args.columns or '') -- 要显示的字段列表
| |
|
| |
| if conditionField == '' or conditionValue == nil or displayFieldsString == '' then
| |
| return i18n['error_generate_table_args']
| |
| end
| |
|
| |
| local songData = GetValuesTable()
| |
| local displayFields = mw.text.split(displayFieldsString, ',')
| |
| local matchingKeys = {}
| |
|
| |
| -- 查找匹配的键
| |
| for songId, songEntry in pairs(songData) do
| |
| local currentValue = ValueFromValuesByKey(songEntry, conditionField)
| |
| local checkValue = currentValue
| |
|
| |
| -- 针对布尔值的特殊处理,允许用户输入 'true'/'false'/'是'/'否' 来匹配
| |
| if type(currentValue) == 'boolean' then
| |
| local lowerTarget = type(conditionValue) == 'string' and string.lower(conditionValue) or conditionValue
| |
| if lowerTarget == 'true' or lowerTarget == '是' then
| |
| checkValue = true
| |
| elseif lowerTarget == 'false' or lowerTarget == '否' then
| |
| checkValue = false
| |
| end
| |
| end
| |
|
| |
| if checkValue == conditionValue then
| |
| table.insert(matchingKeys, songId)
| |
| end
| |
| end
| |
|
| |
| table.sort(matchingKeys)
| |
|
| |
| if #matchingKeys == 0 then
| |
| return '未找到符合条件的歌曲。'
| |
| end
| |
|
| |
| local output = {}
| |
|
| |
| -- 构造表格头部
| |
| table.insert(output, '{| class="wikitable sortable"')
| |
| table.insert(output, '|-')
| |
|
| |
| for _, field in ipairs(displayFields) do
| |
| field = mw.text.trim(field)
| |
| table.insert(output, '! ' .. (field))
| |
| end
| |
|
| |
| -- 构造表格行
| |
| for _, songId in ipairs(matchingKeys) do
| |
| local songEntry = songData[songId]
| |
| table.insert(output, '|-')
| |
|
| |
| -- 第一列: Song ID
| |
| table.insert(output, '| ' .. songId)
| |
|
| |
| -- 后续列: 数据字段
| |
| for _, field in ipairs(displayFields) do
| |
| field = mw.text.trim(field)
| |
| local rawValue = ValueFromValuesByKey(songEntry, field)
| |
| local formattedValue = formatSingleValue(rawValue, field)
| |
| table.insert(output, '| ' .. formattedValue)
| |
| end
| |
| end
| |
|
| |
| -- 闭合表格
| |
| table.insert(output, '|}')
| |
|
| |
| return table.concat(output, '\n')
| |
| end | | end |
| return p | | return p |