1 var HtmlTableImport =
function(table){
7 HtmlTableImport.prototype.parseTable =
function(){
9 element =
self.table.element,
10 options =
self.table.options,
11 columns = options.columns,
12 headers = element.getElementsByTagName(
"th"),
13 rows = element.getElementsByTagName(
"tbody")[0],
17 self.hasIndex =
false;
19 self.table.options.htmlImporting.call(this.table);
21 rows = rows ? rows.getElementsByTagName(
"tr") : [];
24 self._extractOptions(element, options);
27 self._extractHeaders(headers, rows);
29 self._generateBlankHeaders(headers, rows);
34 for(var index = 0; index < rows.length; index++){
35 var row = rows[index],
36 cells = row.getElementsByTagName(
"td"),
41 item[options.index] = index;
44 for(var i = 0; i < cells.length; i++){
46 if(typeof this.fieldIndex[i] !==
"undefined"){
47 item[this.fieldIndex[i]] = cell.innerHTML;
56 var newElement = document.createElement(
"div");
59 var attributes = element.attributes;
63 for(var i in attributes){
64 if(typeof attributes[i] ==
"object"){
65 newElement.setAttribute(attributes[i].name, attributes[i].value);
70 element.parentNode.replaceChild(newElement, element);
74 self.table.options.htmlImported.call(this.table);
78 this.table.element = newElement;
82 HtmlTableImport.prototype._extractOptions =
function(element, options, defaultOptions){
83 var attributes = element.attributes;
84 var optionsArr = defaultOptions ? Object.assign([], defaultOptions) : Object.keys(options);
87 optionsArr.forEach(
function(item){
88 optionsList[item.toLowerCase()] = item;
91 for(var index in attributes){
92 var attrib = attributes[index];
95 if(attrib && typeof attrib ==
"object" && attrib.name && attrib.name.indexOf(
"tabulator-") === 0){
96 name = attrib.name.replace(
"tabulator-",
"");
98 if(typeof optionsList[name] !==
"undefined"){
99 options[optionsList[name]] = this._attribValue(attrib.value);
106 HtmlTableImport.prototype._attribValue =
function(value){
107 if(value ===
"true"){
111 if(value ===
"false"){
119 HtmlTableImport.prototype._findCol =
function(title){
120 var match = this.table.options.columns.find(
function(column){
121 return column.title === title;
124 return match ||
false;
128 HtmlTableImport.prototype._extractHeaders =
function(headers, rows){
129 for(var index = 0; index < headers.length; index++){
130 var header = headers[index],
132 col = this._findCol(header.textContent),
138 col = {title:header.textContent.trim()};
142 col.field = header.textContent.trim().toLowerCase().replace(
" ",
"_");
145 width = header.getAttribute(
"width");
147 if(width && !col.width) {
152 attributes = header.attributes;
155 this._extractOptions(header, col, Column.prototype.defaultOptionList);
157 for(var i in attributes){
158 var attrib = attributes[i],
161 if(attrib && typeof attrib ==
"object" && attrib.name && attrib.name.indexOf(
"tabulator-") === 0){
163 name = attrib.name.replace(
"tabulator-",
"");
165 col[name] = this._attribValue(attrib.value);
169 this.fieldIndex[index] = col.field;
171 if(col.field ==
this.table.options.index){
172 this.hasIndex =
true;
176 this.table.options.columns.push(col);
183 HtmlTableImport.prototype._generateBlankHeaders =
function(headers, rows){
184 for(var index = 0; index < headers.length; index++){
185 var header = headers[index],
186 col = {title:
"", field:
"col" + index};
188 this.fieldIndex[index] = col.field;
190 var width = header.getAttribute(
"width");
196 this.table.options.columns.push(col);
200 Tabulator.prototype.registerModule(
"htmlTableImport", HtmlTableImport);