Index
The HT for Web
provides the table component class ht.widget.TableView
, which displays the property information of the Data
type object in the DataModel
container, and supports sorting and filtering functions.
Through tableView = new ht.widget.TableView(dataModel);
initialize building a table component object, dataModel
parameter is a data model bound to a table component, the table component constructor function will create a new data model for binding when the model is empty.
The getColumnModel()
function of the table component returns the column model object, which is essentially a DataModel
type object, except that the object is only used to add ht.Column
type object, ht.Column
type of parent is ht. Data
, adds a function interface associated with the attribute definition.
So what the user needs to do is build the ht.Column
object based on the attribute information to be displayed, and then added to the columns model returned by the tableView.getColumnModel()
function, so that the information about Data
in the tableView.getDataModel()
model will displayed by the configuration of the ht.Column
stored in tableView.getColumnModel()
.
ht.Column
class inherits from ht.Data
, you cannot set up a parent-child relationship with the following property functions:
getName()
and setName(name)
Gets and sets name
attribute, which combine accessType
attributes to achieve access to Data
propertiesgetDisplayName()
and setDisplayName(displayName)
Gets and sets the column name contents of the header, and display name
values if emptygetIcon()
and setIcon('icon')
Gets and sets table header's column name to the left of the icon displayedgetWidth()
and setWidth(80)
Gets and sets column width, the default setWidth
function allows the minimum width of 16
to avoid columns too narrowisVisible()
and setVisible(true/false)
Gets and sets whether columns are visiblegetColor()
and setColor(color)
Gets and sets the text color of the column name of the headerisEditable()
and setEditable(true/false)
Gets and sets whether the column is editable, the default is false
isBatchEditable()
and setBatchEditable(true/false)
Gets and sets whether the column allows more than a batch edit, the default is true
getAccessType()
and setAccessType(type)
Gets and sets the attribute type of the column:null
: Default type, such as name
is age
, gets and sets by using getAge()
and setAge(98)
get/set
or is/set
style
: Such as name
is age
, gets and sets by using getStyle('age')
and setStyle('age', 98)
field
: Such as name
is age
, gets and sets by using data.age
and data.age = 98
attr
: Such as name
is age
, gets and sets by using getAttr('age')
and setAttr('age', 98)
valueType
value type is used to prompt the component to provide the appropriate renderer
rendering, the appropriate editing control, and the necessary type conversions when changing values:null
: Default type, display as textstring
: String type, display as textboolean
: Boolean type, display as check boxcolor
: Color type, displayed in a way that fills the background colorint
: Integral type, text editor changes value automatically through parseInt
conversionnumber
: Float type, text editor changes value automatically through parseFloat
conversiongetAlign()
and setAlign('left'/'center'/'right')
Determines the horizontal alignment of the text rendering, the default is left
isNullable()
and setNullable(true/false)
Determines whether the property is empty, the default is true
, set to false
to avoid entering null
or undefined
isEmptiable()
and setEmptiable(true/false)
Determines whether the property is empty string, the default is false
, to avoid entering an empty string, and an empty string into undefined
getSortOrder()
and setSortOrder('desc'/'asc')
Gets and sets column ascending and descending order state, click on the table header to automatically change its valueisSortable()
and setSortable(true/false)
Gets and sets columns can be sorted for flags, the default is true
getSortFunc()
and setSortFunc(function(v1, v2, d1, d2){return 1/-1/0})
Gets and sets column sort function for custom sort logiccolumn.getValue = function(data, column, view){return value}
Customize get value functioncolumn.setValue = function(data, column, value, view){}
Customize set value functioncolumn.drawCell = function (g, data, selected, column, x, y, w, h, view){}
Customize cell renderingcolumn.formatValue = function(value)
is generally used to format digital conversions more easily, and can be overloaded with customcolumn.getToolTip = function(data, tableView)
Custom tooltip contentEnumeration is a common property-editing selection application, rendering the drop-down list at edit time, so HT
takes a lot of scenarios for enumerated type attributes, setEnum(params)
functions to set a single json
parameter, or to set parameter information setEnum(enumValues, enumLabels, enumIcons, enumEditable, enumStrict)
, the following are common cases:
setEnum(['C','C++','JS'])
Passing a numerical array setEnum([1,2,3], ['C','C++','JS'])
Passing a numeric and text array setEnum([1,2,3], ['C','C++','JS'], ['c_icon', 'c++_icon', 'js_icon'])
Passing values, text and icon arrayssetEnum({values:[1,2,3]})
Passing a numeric arraysetEnum({values:[1,2,3], labels:['C','C++','JS']})
Passing values and text array setEnum({values:[1,2,3], labels:['C','C++','JS'], icons:['c_icon', 'c++_icon', 'js_icon']})
Passing values, text and icon arrays
HT
automatically detects whether the user has introduced Form Plugins, if theht.widget.ComboBox
component of the form plug-in is introduced, then using it as an editor, otherwise using theselect
component of the nativehtml
, because of the nativehtml
select
drop-down component is text-only, so many of the parameters above work only for theht.widget.ComboBox
component.
enumValues
: Enum value arrayenumLabels
: Enum text arrayenumIcons
: Enum icon arrayenumEditable
: Enum whether the drop-down editor allowed input, the default is false
enumStrict
: Does the value match use strict ===
to compare, the default is true
, if false
uses ===
to compareForm Plug-in ht.widget.Slider
is also a common and easy-to-use editing component, and this HT
also adds the setting of the corresponding column properties of the type, through getSlider()
and setSlider(parmas)
can specify the slide bar information that the column renders in edit state.
The above example warning level information stored in the alarmSeverity
property of the attr
type of the Data
object, the first column set the ·setSortFunc sort function, to set the
Cleared alarm level to the top, the other alarm level value higher in the upper layer of the effect, also called
tableView.setSortColumn` specifies the current row sequence.
var column = new ht.Column();
column.setName("alarmSeverity");
column.setAccessType('attr');
column.setSortFunc(function(v1, v2, d1, d2){
if(v1 === v2){
return 0;
}
// keep 'Cleared' on top
if(v1 === 0){
return -1;
}
if(v2 === 0){
return 1;
}
// compare value
if(v1 > v2){
return -1;
}else{
return 1;
}
});
columnModel.add(column);
tableView.setSortColumn(column);
The second column overloads the column.getValue
custom to get the value, depending on the attr
type's property alarmSeverity
value, the corresponding alert level color is found through the configuration of the map
object. By setting valueType
as the color
type, HT
automatically renders the property in a way filled with a cell background color.
column = new ht.Column();
column.setValueType('color');
column.getValue = function(data){
var alarmSeverity = data.getAttr('alarmSeverity'),
color = map[alarmSeverity].color;
return tableView.isSelected(data) ? ht.Default.darker(color) : color;
};
columnModel.add(column);
The third column overloads the column.drawCell
custom cell rendering effect, which returns the row index tableView.getRowIndex(data)
, draw the index information between cells by ht.Default.drawText
and draws a different row background color based on the parity of the index.
column = new ht.Column();
column.drawCell = function (g, data, selected, column, x, y, w, h, tableView) {
var index = tableView.getRowIndex(data);
// draw background
var color = index % 2 === 0 ? '#ECF0F1' : '#3498DB';
g.fillStyle = selected ? ht.Default.darker(color) : color;
g.beginPath();
g.rect(x, y, w, h);
g.fill();
// draw label
color = selected ? 'white' : 'black';
ht.Default.drawText(g, 'row ' + index, null, color, x, y, w, h, 'center');
};
columnModel.add(column);
Column fourth overloads the column.drawCell
custom cell rendering effect, according to the alarmSeverity
value of attr
, find the corresponding alarm level name and color information for rendering by the map
object's configuration.
column = new ht.Column();
column.setWidth(200);
column.drawCell = function (g, data, selected, column, x, y, w, h, tableView) {
var alarmSeverity = data.getAttr('alarmSeverity'),
info = map[alarmSeverity],
color = info.color;
// draw background
g.fillStyle = selected ? ht.Default.darker(color) : color;
g.beginPath();
g.rect(x, y, w, h);
g.fill();
// draw label
color = selected ? 'white' : 'black';
ht.Default.drawText(g, info.name, null, color, x, y, w, h, 'center');
};
columnModel.add(column);
ht.widget.TableHeader
table header components are often combined with TableView
and TreeTableView
to present Column
information and provide Column
with positive and negative sort switches, column widths stretching, And the change of the column order position.
Through tableHeader = new ht.widget.TableHeader(tableView/treeTableView);
initialize building a header component object that can introduce tableView
or treeTableView
table component object is bound, tableHeader
will automatically listen to the column model of tableView.getColumnModel()
, when the user clicks the sort, or the column width change and the column order change and so on, the corresponding modification to the column model Column
attribute, the header component is automatically refreshed when the user modifies the Column
attribute through the API
.
getSortDescIcon()
and setSortDescIcon(icon)
Gets and sets header column descending icongetSortAscIcon()
and setSortAscIcon(icon)
Gets and sets table header column ascending icongetMoveBackground()
and setMoveBackground(color)
Gets and sets column header background color when moving a columngetInsertColor()
and setInsertColor(color)
Gets and sets the hint color of the insert position when moving a columnisColumnLineVisible()
and setColumnLineVisible(true/false)
Gets and sets whether the column lines are visible, the default is true
isColumnLineColor()
and setColumnLineColor(color)
Gets and sets line colorisResizable()
and setResizable(true/false)
Gets and sets whether the column widths are changable, the default is true
isMovable()
and setMovable(true/false)
Gets and sets whether the column order allow movement change, the default is true
getTableView()
Gets table components bound in table headergetLabel(column)
Gets the header text information, and the default returns column.toLabel()
, which can be overloaded with customgetLabelFont(column)
Gets the header font, which can be overloaded with customgetLabelColor(column)
Gets the header color information, and the default returns column.getColor()
, which can be overloaded with customgetLabelAlign(column)
Gets the column header text horizontal alignment, the column.getAlign()
value is considered and can be overloaded with customdrawColumn(g, column, x, y, width, height)
Draw column headers, which can be overloaded with customThe above example constructs two ht.widget.TableHeader
header components that bind the same tableView
object, so that two headers can achieve an interactive synchronization effect.
tableHeader1 = new ht.widget.TableHeader(tableView);
tableHeader2 = new ht.widget.TableHeader(tableView);
borderPane = new ht.widget.BorderPane();
borderPane.setTopView(tableHeader1);
borderPane.setCenterView(tableView);
borderPane.setBottomView(tableHeader2);
TableView
provides a function of addColumns
and setColumns
, which can be easily added Column
by json
format in bulk, and the following code is used in the above example:
tableView.addColumns([
{
displayName: 'Severity',
name: 'alarmSeverity',
accessType: 'attr',
sortOrder: 'desc',
tag: 'sortableColumn',
sortFunc: function(v1, v2, d1, d2){
if(v1 === v2){
return 0;
}
// keep 'Cleared' on top
if(v1 === 0){
return -1;
}
if(v2 === 0){
return 1;
}
// compare value
if(v1 > v2){
return -1;
}else{
return 1;
}
}
},
// ...
]);
In the example, only the first column allows sorting, and the sortable
property of the other columns is set to false
, which is used to sort the column in order to initialize the display, and the code specifically sets the tag
logo for the first column, through tableView.getColumnModel().getDataByTag(' sortableColumn')
finds the column object.
tableView.setSortColumn(tableView.getColumnModel().getDataByTag('sortableColumn'));
Note
tableView.getColumnModel()
is theDataModel
,Column
is also inherited fromData
type, so have thetag
and other related operations of data container.
The previous section example uses the BorderPane
container to combine TableView
and TableHeader
objects, most of which are in need of integrated use, for which HT
provides ht.widget.TablePane
and ht.widget.TreeTablePane
, the two components built the TableView
and TreeTableView
objects separately and also built a TableHeader
object, the TableHeader
component displayed in the top, and the TableView
and TreeTableView
components displayed in the bottom.
ht.widget.TablePane
and ht.widget.TreeTablePane
constructors can introduce the TableView
and TreeTableView
objects, and automatically create a TableView
and TreeTableView
objects, other common functions are as follows:
getTableView()
Gets table componentsgetTableHeader()
Gets table header componentsgetDataModel()
Gets the data model of the table component bindingsgetColumnModel()
Gets the column model of the table component bindingsaddColumns(array)
Using json
array parameters to add column information in bulksetColumns(array)
Using json
array parameters to add column information in bulk, clear all column while the parameter is emptyThe above example inherits from ht.Column
expands com.hightopo.LanguageColumn
and com.hightopo.LanguageColumn
two column types, the primary purpose is to initialize the necessary parameter configuration information in a constructor, that can avoid the large number of duplicate configuration information when the same type column needs to be defined in many places, and facilitates the uniform modification of parameter information.
com = {};
com.hightopo = {};
var LanguageColumn = com.hightopo.LanguageColumn = function(){
LanguageColumn.superClass.constructor.call(this);
this.setName('Language');
this.setAccessType('attr');
this.setEnum({values: [1, 2, 3, 4, 5, 6], labels: ['C', 'C++', 'Java', 'JS', 'AS', 'C#']});
this.setEditable(true);
this.setSortFunc(function(v1, v2, d1, d2){
if(v1 === v2){
return 0;
}
if(v1 === 2){
return 1;
}
if(v2 === 2){
return -1;
}
if(v1 === 5){
return -1;
}
if(v2 === 5){
return 1;
}
return v1 - v2;
});
};
ht.Default.def('com.hightopo.LanguageColumn', ht.Column, {
});
var SexColumn = com.hightopo.SexColumn = function(){
SexColumn.superClass.constructor.call(this);
this.setName('Sex');
this.setAccessType('attr');
this.setEnum(['Male', 'Female']);
this.setEditable(true);
};
ht.Default.def('com.hightopo.SexColumn', ht.Column, {
});
With the above class encapsulation, the configuration of the column properties reduces many parameters, notice the className
keyword in the following code, the HT
will be based on this className
special keyword information to replace the default ht.Column
type, the attribute component also replaces the default ht.Property
type by className
special keyword information.
tablePane.addColumns([
{
name: 'id',
width: 60,
tag: 'id'
},
{
className: 'com.hightopo.LanguageColumn',
width: 100,
tag: 'language'
},
{
className: 'com.hightopo.SexColumn',
width: 100,
tag: 'sex'
}
]);
propertyView.addProperties([
{
name: 'id'
},
{
className: 'com.hightopo.LanguageProperty',
editable: true
},
{
className: 'com.hightopo.SexProperty',
editable: true
}
]);
By tablePane.getTableHeader().setResizable(false)
, the header is set to an immutable column width:
tablePane.getTableHeader().setResizable(false);
Through the tablePane.addViewListener
to isten tablePane
component event, when the beginValidate
event is triggered, each column width is allocated according to the current component widths, and the tag
parameter has been set while defined column
, so that the corresponding column can be found through columnModel.getDataByTag('language')
:
tablePane.addViewListener(function(e){
if(e.kind === 'beginValidate'){
var columnModel = tablePane.getColumnModel(),
width = tablePane.getWidth();
columnModel.getDataByTag('id').setWidth(width * 0.2);
columnModel.getDataByTag('language').setWidth(width * 0.4);
columnModel.getDataByTag('sex').setWidth(width * 0.4);
}
});
Table component class ht.widget.TableView
main configurable properties and functions are as follows:
enableToolTip()
and disableToolTip()
Enable and disable the tooltipisDisabled()
and setDisabled(true/false, iconURL)
Gets and sets the entire component in an unusable stateaddTopPainter(func)
and removeTopPainter(func)
Adds and removes top-level painter function(g){...}
addBottomPainter(func)
and removeBottomPainter(func)
Adds and removes bottom-level painter function(g){...}
isEditable()
and setEditable(true/false)
Sets whether the property editable, the default is false
isBatchEditable()
and setBatchEditable(true/false)
Sets whether this property allows more than a batch edit, the default is true
getRowHeight()
and setRowHeight(20)
Gets and sets row heightisRowLineVisible()
and setRowLineVisible(true/false)
Gets and sets whether line lines are visible, the default is true
getRowLineColor()
and setRowLineColor(color)
Gets and sets row line colorisColumnLineVisible()
and setColumnLineVisible(true/false)
Gets and sets whether the column line is visible, the default is true
getColumnLineColor()
and setColumnLineColor(color)
Gets and sets the column line colorgetSortColumn()
and setSortColumn(column)
Gets and sets the current row sequencegetSortFunc()
and setSortFunc(sortFunc)
Gets and sets the sort function, the default value is empty, and its value works without sortColumn
getVisibleFunc()
and setVisibleFunc()
Gets and sets visible filters, which can filter data
objects in DataModel
getScrollBarColor()
and setScrollBarColor(color)
Gets and sets scroll bar colorgetScrollBarSize()
and setScrollBarSize(6)
Gets and sets the scroll bar widthisAutoHideScrollBar()
and setAutoHideScrollBar(true/false)
Gets and sets whether the scroll bar is automatically hidden and the default is true
isCheckModel()
and setCheckMode(true/false)
Gets and sets whether the check
mode, the default is false
, if true
, automatically inserts checkColumn
columngetColumnModel()
Table component built-in a DataModel
type column model for storing column
object informationonColumnClicked(column)
Called when header has been clicked, can be overloaded for subsequent processing, such as remote sorting functiongetCheckIcon(data)
Returns the Check
icon correspond to the data
object, which can be overloaded with custom check
icon, which is valid in checkMode
modegetFocusData()
, setFocusData(data)
and setFocusDataById(id)
In checkMode
, in addition to the check
state, there can be clicked on the line of the focus
statusgetDataAt(pointOrEvent)
Inputting logical coordinate point or interactive event event
parameter, returns corresponding data
object or nullonDataDoubleClicked(data)
Called when the data
row is double-clicked, can be overloaded to respond to the double-click event onDataClicked(data)
Called when the data
row is clicked, can be overloaded to respond to the click event getLabelFont(data, column, value)
Returns the corresponding cell text font, which can be overloaded with customgetLabelColor(data, column, value)
Returns the corresponding cell text color, which can be overloaded with customgetSelectBackground(data)
and setSelectBackground(color)
Gets and sets row selected background colorgetStartRowIndex()
Returns the starting row index of the currently visible areagetEndRowIndex()
Returns the end row index of the currently visible areagetRowDatas()
Returns an array of currently displayed Data
objects that have been sorted and filteredgetRowIndex(data)
Returns the row index where the data
object is locatedgetRowSize()
Returns the total number of currently visible rowsisVisible(data)
Determines whether the data
object is visible and can be overloaded with customgetDataModel()
Gets the binding DataModel
setDataModel(dataModel)
Binding new DatModel
makeVisible(data)
This function triggers the component to scroll to the visible area where the data
object appearsinvalidateModel()
This function triggers the component reordering filter loading data, and the generic component is automatically called unless the data changes but the event is not dispatchedredraw()
Redraw refresh, note that the function does not trigger a reload of the data modelinvalidateData(data)
Calls the function to redraw the row where the data
object is locateddrawRowBackground(g, data, selected, x, y, width, height)
Draws a row background color, only when the row is selected to fill the selected background color in default, can be overloaded with customdrawCell(g, data, selected, column, x, y, width, height)
Draws cells and can be overloaded with custom cell renderingdrawCheckColumnCell(g, data, selected, column, x, y, width, height, view)
Draws check
column cells, which can be overloaded with customisCellEditable(data, column)
Determines whether cells are editable and can be overloaded with customgetCurrentSortFunc()
The function defaults return the sortFunc
function and returns its corresponding sort function when sortColumn
is not emptyhandleDragAndDrop(event, state)
The function defaults to NULL, and if the function is overloaded, the pan
component feature will be closedevent
Mouse or Touch
interaction eventstate
Current status, there will be prepare
-begin
-between
-end
four processesThe following is the default implementation of the getCurrentSortFunc
function, while called the sortFunc
of the TableView
, the two parameters are d1
and d2
, two different Data
objects, while called Column
sortFunc
is introduced the v1,v2,d1,d2
four parameters, that is, the first two parameters of the Data
object corresponding to the value of the column.
getCurrentSortFunc: function () {
var column = this._sortColumn;
if (column && column.isSortable()) {
var func = column.getSortFunc(),
tableView = this,
order = 'asc' === column.getSortOrder() ? 1 : -1;
if (!func) {
func = ht.Default.sortFunc;
}
return function (d1, d2) {
var v1 = tableView.getValue(d1, column),
v2 = tableView.getValue(d2, column);
return func.call(tableView, v1, v2, d1, d2) * order;
};
}
return this._sortFunc;
}
The following code uses the tableHeader.getView().style
to get the bottom div
component of the header, using the css
repeat-x
to tile the progressive color background.
tableHeader = tablePane.getTableHeader();
tableHeader.getView().style.background = 'url(images/header.png) repeat-x';
The following code overloads the drawRowBackground
function, drawing the effect of the table row alternating zebra.
tableView.drawRowBackground = function(g, data, selected, x, y, width, height){
if(tableView.isSelected(data)){
g.fillStyle = '#87A6CB';
}
else if(tableView.getRowIndex(data) % 2 === 0){
g.fillStyle = '#F1F4F7';
}
else{
g.fillStyle = '#FAFAFA';
}
g.beginPath();
g.rect(x, y, width, height);
g.fill();
};
The following code sets the visible filter, the filter determines whether the Data
object is visible by the element selection state of the toolbar
, the filter only set once, and the filter rule changes as the toolbar state changes, but the state of the toolbar element does not distribute any events. So the action
of the tool bar button shows that the tableView.invalidateModel();
notification table is being updated for data reload.
tableView.setVisibleFunc(function(data){
var nation = data.a('nation'),
sex = data.a('sex');
// filter nation
if(!toolbar.getItemById('nation-all').selected){
if(toolbar.getItemById('uk').selected && nation !== 0){
return false;
}
if(toolbar.getItemById('usa').selected && nation !== 1){
return false;
}
if(toolbar.getItemById('mexico').selected && nation !== 2){
return false;
}
}
// filter sex
if(!toolbar.getItemById('sex-all').selected){
if(toolbar.getItemById('man').selected && sex !== 0){
return false;
}
if(toolbar.getItemById('woman').selected && sex !== 1){
return false;
}
}
return true;
});
The following code overloads the drawCell
function in the added column, through ht.Default.drawStretchImage
function draws the corresponding icon at the center of the cell, and the third parameter of the drawStretchImage
function can be introduced in the type of fill
, uniform
or centerUniform
.
tablePane.addColumns([
{
name: 'index',
displayName: 'Index',
accessType: 'attr',
align: 'center'
},
{
name: 'nation',
displayName: 'Nation',
accessType: 'attr',
align: 'center',
drawCell: function (g, data, selected, column, x, y, w, h, view) {
var image = ht.Default.getImage('images/' + nations[data.a('nation')] + '.png');
ht.Default.drawStretchImage(g, image, 'centerUniform', x, y, w, h);
}
},
{
name: 'sex',
displayName: 'Sex',
accessType: 'attr',
align: 'center',
drawCell: function (g, data, selected, column, x, y, w, h, view) {
var image = ht.Default.getImage('images/' + sexs[data.a('sex')] + '.png');
ht.Default.drawStretchImage(g, image, 'centerUniform', x, y, w, h);
}
}
]);