Skip to content

Commit fc20e94

Browse files
committed
Improve initRow
1 parent 6fe0ae7 commit fc20e94

File tree

1 file changed

+43
-46
lines changed

1 file changed

+43
-46
lines changed

src/bootstrap-table.js

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@
7474
return flag ? str : ''
7575
},
7676

77-
getPropertyFromOther (list, from, to, value) {
78-
for (let item in list) {
79-
if (item[from] === value) {
80-
return item[to]
77+
getFieldTitle (list, value) {
78+
for (let item of list) {
79+
if (item.field === value) {
80+
return item.title
8181
}
8282
}
8383
return ''
@@ -1638,7 +1638,7 @@
16381638

16391639
if (attributes) {
16401640
for (key in attributes) {
1641-
htmlAttributes.push(Utils.sprintf('%s="%s"', key, Utils.escapeHTML(attributes[key])))
1641+
htmlAttributes.push(`${key}="${Utils.escapeHTML(attributes[key])}"`)
16421642
}
16431643
}
16441644

@@ -1648,15 +1648,15 @@
16481648
if (k === 'index') {
16491649
return
16501650
}
1651-
data_ += Utils.sprintf(' data-%s="%s"', k, v)
1651+
data_ += ` data-${k}="${v}"`
16521652
})
16531653
}
16541654

16551655
html.push('<tr',
16561656
Utils.sprintf(' %s', htmlAttributes.length ? htmlAttributes.join(' ') : undefined),
16571657
Utils.sprintf(' id="%s"', $.isArray(item) ? undefined : item._id),
16581658
Utils.sprintf(' class="%s"', style.classes || ($.isArray(item) ? undefined : item._class)),
1659-
Utils.sprintf(' data-index="%s"', i),
1659+
` data-index="${i}"`,
16601660
Utils.sprintf(' data-uniqueid="%s"', item[this.options.uniqueId]),
16611661
Utils.sprintf('%s', data_),
16621662
'>'
@@ -1672,7 +1672,7 @@
16721672
if (Utils.calculateObjectValue(null, this.options.detailFilter, [i, item])) {
16731673
html.push(`
16741674
<a class="detail-icon" href="#">
1675-
<i class="${this.options.iconsPrefix} ${this.options.icons.detailOpen}"></i>,
1675+
<i class="${this.options.iconsPrefix} ${this.options.icons.detailOpen}"></i>
16761676
</a>
16771677
`)
16781678
}
@@ -1688,6 +1688,7 @@
16881688
let cellStyle = {}
16891689
let id_ = ''
16901690
let class_ = this.header.classes[j]
1691+
let style_ = ''
16911692
let data_ = ''
16921693
let rowspan_ = ''
16931694
let colspan_ = ''
@@ -1712,8 +1713,9 @@
17121713
value_ = Utils.escapeHTML(value_)
17131714
}
17141715

1715-
style = Utils.sprintf('style="%s"', csses.concat(this.header.styles[j]).join('; '))
1716-
1716+
if (csses.concat([this.header.styles[j]]).length) {
1717+
style_ = ` style="${csses.concat([this.header.styles[j]]).join('; ')}"`
1718+
}
17171719
// handle td's id and class
17181720
if (item[`_${field}_id`]) {
17191721
id_ = Utils.sprintf(' id="%s"', item[`_${field}_id`])
@@ -1733,14 +1735,14 @@
17331735
cellStyle = Utils.calculateObjectValue(this.header,
17341736
this.header.cellStyles[j], [value_, item, i, field], cellStyle)
17351737
if (cellStyle.classes) {
1736-
class_ = Utils.sprintf(' class="%s"', cellStyle.classes)
1738+
class_ = ` class="${cellStyle.classes}"`
17371739
}
17381740
if (cellStyle.css) {
17391741
const csses_ = []
17401742
for (const key in cellStyle.css) {
17411743
csses_.push(`${key}: ${cellStyle.css[key]}`)
17421744
}
1743-
style = Utils.sprintf('style="%s"', csses_.concat(this.header.styles[j]).join('; '))
1745+
style_ = ` style="${csses_.concat(this.header.styles[j]).join('; ')}"`
17441746
}
17451747

17461748
value = Utils.calculateObjectValue(column,
@@ -1752,51 +1754,46 @@
17521754
if (k === 'index') {
17531755
return
17541756
}
1755-
data_ += Utils.sprintf(' data-%s="%s"', k, v)
1757+
data_ += ` data-${k}="${v}"`
17561758
})
17571759
}
17581760

17591761
if (column.checkbox || column.radio) {
17601762
type = column.checkbox ? 'checkbox' : type
17611763
type = column.radio ? 'radio' : type
17621764

1765+
const c = column['class'] || ''
17631766
text = [
1764-
Utils.sprintf(
1765-
this.options.cardView ? '<div class="card-view %s">' : '<td class="bs-checkbox %s">',
1766-
column['class'] || '')
1767-
]
1768-
text.push(`
1769-
<input
1770-
${Utils.sprintf(' data-index="%s"', i)}
1771-
${Utils.sprintf(' name="%s"', this.options.selectItemName)}
1772-
${Utils.sprintf(' type="%s"', type)}
1773-
${Utils.sprintf(' value="%s"', item[this.options.idField])}
1774-
${Utils.sprintf(' checked="%s"', value === true || (value_ || (value && value.checked)) ? 'checked' : undefined)}
1775-
${Utils.sprintf(' disabled="%s"', !column.checkboxEnabled || (value && value.disabled) ? 'disabled' : undefined)} />
1776-
`)
1777-
text.push(this.header.formatters[j] && typeof value === 'string' ? value : '')
1778-
text.push(this.options.cardView ? '</div>' : '</td>')
1767+
this.options.cardView
1768+
? `<div class="card-view ${c}">`
1769+
: `<td class="bs-checkbox ${c}">`,
1770+
`<input
1771+
data-index="${i}"
1772+
name="${this.options.selectItemName}"
1773+
type="${type}"
1774+
value="${item[this.options.idField]}"
1775+
${value === true || (value_ || (value && value.checked)) ? 'checked="checked"' : ''}
1776+
${!column.checkboxEnabled || (value && value.disabled) ? 'disabled="disabled"' : ''} />`,
1777+
this.header.formatters[j] && typeof value === 'string' ? value : '',
1778+
this.options.cardView ? '</div>' : '</td>'
1779+
].join('')
17791780

17801781
item[this.header.stateField] = value === true || (!!value_ || (value && value.checked))
17811782
} else {
17821783
value = typeof value === 'undefined' || value === null
17831784
? this.options.undefinedText : value
17841785

1785-
text = this.options.cardView ? [
1786-
'<div class="card-view">',
1787-
this.options.showHeader ? Utils.sprintf('<span class="title" %s>%s</span>', style, Utils.getPropertyFromOther(this.columns, 'field', 'title', field)) : '',
1788-
Utils.sprintf('<span class="value">%s</span>', value),
1789-
'</div>'
1790-
].join('') : [Utils.sprintf('<td%s %s %s %s %s %s %s>',
1791-
id_, class_, style, data_, rowspan_, colspan_, title_),
1792-
value,
1793-
'</td>'
1794-
].join('')
1786+
if (this.options.cardView) {
1787+
const cardTitle = this.options.showHeader
1788+
? `<span class="title"${style}>${Utils.getFieldTitle(this.columns, field)}</span>` : ''
17951789

1796-
// Hide empty data on Card view when smartDisplay is set to true.
1797-
if (this.options.cardView && this.options.smartDisplay && value === '') {
1798-
// Should set a placeholder for event binding correct fieldIndex
1799-
text = '<div class="card-view"></div>'
1790+
text = `<div class="card-view">${cardTitle}<span class="value">${value}</span></div>`
1791+
1792+
if (this.options.smartDisplay && value === '') {
1793+
text = '<div class="card-view"></div>'
1794+
}
1795+
} else {
1796+
text = `<td${id_}${class_}${style_}${data_}${rowspan_}${colspan_}${title_}>${value}</td>`
18001797
}
18011798
}
18021799

@@ -1952,8 +1949,8 @@
19521949
}
19531950

19541951
for (const key in events) {
1955-
this.$body.find('>tr:not(.no-records-found)').each(function () {
1956-
const $tr = $(this)
1952+
this.$body.find('>tr:not(.no-records-found)').each(e => {
1953+
const $tr = $(e.currentTarget)
19571954
const $td = $tr.find(this.options.cardView ? '.card-view' : 'td').eq(fieldIndex)
19581955
const index = key.indexOf(' ')
19591956
const name = key.substring(0, index)
@@ -2309,8 +2306,8 @@
23092306

23102307
$footerTd = this.$tableFooter.find('td')
23112308

2312-
this.$body.find('>tr:first-child:not(.no-records-found) > *').each(function (i) {
2313-
const $this = $(this)
2309+
this.$body.find('>tr:first-child:not(.no-records-found) > *').each((i, el) => {
2310+
const $this = $(el)
23142311

23152312
$footerTd.eq(i).find('.fht-cell').width($this.innerWidth())
23162313
})

0 commit comments

Comments
 (0)