Modul:AutosortTable: Perbedaan antara revisi
Konten dihapus Konten ditambahkan
←Membuat halaman berisi '--[[ AutosortTable: Creates a table which is automatically sorted Usage: (Remove the hidden comments before use) {{#invoke: AutosortTable|create | class = wikita...' |
kTidak ada ringkasan suntingan |
||
Baris 6:
{{#invoke: AutosortTable|create
| class = wikitable
| style = width: 50%;
|
| order = 2, 1
|
|
| hidden = 2 <!-- Columns which are not to be displayed. Takes comma-separated list of col numbers -->
|
|
| header = --
|
|
| -- Peter -- 35 <!-- Row 2 -->
| -- John -- 35 <!-- Row 3 -->
| -- James -- 50 <!-- Row 4 -->
| background-color: #FFDDDD -- Henry -- 45 <!-- Row 5, with CSS -->
| colstyle = -- text-align:left; -- text-align:right; -- -- --
<!-- CSS to be used on content of respective columns, here 1st & 2nd -->
}}
]]
Baris 40 ⟶ 46:
local footer = args.footer
local colstyle = args.colstyle
local rowheader = args.rowheader or ""
local caption = args.caption
-- Frequently-used functions
Baris 49 ⟶ 57:
local seplen = #sep
local nsortLookup, descLookup, hiddenLookup, rowHeading = {}, {}, {}, {}
-- Create the table
Baris 57 ⟶ 65:
if class then htable:attr('class', class) end
if style then htable:attr('style', style) end
if caption then
local hcaption = htable:tag('caption')
-- Parses a row string. The key paremter is used to assign a unique key to the result so that equal rows do not cause sort errors.▼
hcaption:wikitext(caption)
end
▲ -- Parses a row string. The 'key'
local parse = function(s, key)
local css
Baris 68 ⟶ 80:
css = strSub(s, 1, firstSep - 1)
s = strSub(s, firstSep + seplen, -1)
end
▲ return { key = key, css = css, data = strSplit(s, sep, true) }
end
--[[
Writes a row to the table.
css: CSS to apply to the row
data: The data (cells) of the row
_type: Can be 'header', 'footer' or nil.
Baris 91 ⟶ 102:
cell:attr('scope', 'col')
elseif _type == 'footer' then
-- Footer: Mark as 'sortbottom' so that it does not sort
-- with the 'wikitable sortable' class
cell = row:tag('td')
cell:
else
if rowHeading[i]
-- Cell is a
cell = row:tag('th')
cell:attr('scope', 'row')
else
-- Ordinary cell
cell = row:tag('td')
end
local cellCss = colstyle and colstyle[i]
if cellCss then cell:attr('style', strTrim(cellCss)) end -- Apply the column styling, if necessary
Baris 104 ⟶ 121:
end
end
return row
end
Baris 125 ⟶ 141:
desc = strSplit(desc, '%s*,%s*')
hidden = strSplit(hidden, '%s*,%s*')
rowheader = strSplit(rowheader, '%s*,%s*')
for i, v in ipairs(order) do order[i] = tonumber(v) end
for i, v in ipairs(nsort) do nsortLookup[tonumber(v) or -1] = true end
for i, v in ipairs(desc) do descLookup[tonumber(v) or -1] = true end
for i, v in ipairs(hidden) do hiddenLookup[tonumber(v) or -1] = true end
for i, v in ipairs(rowheader) do rowHeading[tonumber(v) or -1] = true end
--Sorting comparator function.
local sortFunc = function(a, b)
Baris 138 ⟶ 156:
local ai, bi = ad[index], bd[index]
if nsortLookup[index] then
-- Numeric sort. Find the first
ai = tonumber( (ai:find('.', 1, true) and ai:match('[+-]?%d*%.%d+') or ai:match('[+-]?%d+')) or 0 )
bi = tonumber( (bi:find('.', 1, true) and bi:match('[+-]?%d*%.%d+') or bi:match('[+-]?%d+')) or 0 )
Baris 158 ⟶ 176:
writeHtml(footerData.css, footerData.data, 'footer')
end
return tostring(html)
end
return _module
|