Modul:AutosortTable: Perbedaan antara revisi

Konten dihapus Konten ditambahkan
PK2 (bicara | kontrib)
←Membuat halaman berisi '--[[ AutosortTable: Creates a table which is automatically sorted Usage: (Remove the hidden comments before use) {{#invoke: AutosortTable|create | class = wikita...'
 
PK2 (bicara | kontrib)
kTidak ada ringkasan suntingan
 
Baris 6:
{{#invoke: AutosortTable|create
| class = wikitable <!-- Class for the entire table -->
| style = width: 50%; <!-- CSS for the entire table -->
| sepseparator = -- <!-- Separator string used to separate cells; pipe (|) invalid -->
| order = 2, 1 <!-- Order for sorting preference, takes a comacomma-separated list of column numbers -->
| nsortnumeric = 2 <!-- Columns which use numeric sorting. Takes a comacomma-separated list of column numbers -->
| headerdescending = 1 -- Name -- Age <!-- TableColumns for which sort order should be descending. Takes comma-separated list of col headernumbers -->
| hidden = 2 <!-- Columns which are not to be displayed. Takes comma-separated list of col numbers -->
| --rowheader Bob= --1 20 <!-- Cell(s) in each <!-non-header Rowrow to be emitted as row header, per WP:ACCESS#Data tables. Usually just 1, but accepts comma-separated list of col numbers -->
| -- Peter -- 35 caption = Notable people by age <!-- RowTable caption per 2WP:ACCESS -->
| header = -- JohnName -- 35 Age <!-- RowTable 3header -->
| --footer = James -- 50 <!-- RowTable footer, typically a totals row or duplication of 4header -->
| background-color:- #FFDDDDBob -- Henry20 -- 45 <!-- Row 5, with CSS1 -->
| -- 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' paremterparameter is used to assign a unique key to the result so that equal rows do not cause sort errors.
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) }
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 whewhen the table is made user-sortable
-- with the 'wikitable sortable' class
cell = row:tag('td')
cell:classattr('class', 'sortbottom')
else
if rowHeading[i] -- Ordinary cellthen
-- Cell is a cellrow = row:tag('td')heading
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 occurenceoccurrence of a number anand use it. Decimal points are allowed. Scientific notation not supported.
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