📄 filteringtable.js
字号:
/* Copyright (c) 2004-2006, The Dojo Foundation All Rights Reserved. Licensed under the Academic Free License version 2.1 or above OR the modified BSD license. For more information on Dojo licensing, see: http://dojotoolkit.org/community/licensing.shtml*/dojo.provide("dojo.widget.FilteringTable");dojo.require("dojo.date.format");dojo.require("dojo.collections.Store");dojo.require("dojo.html.*");dojo.require("dojo.html.util");dojo.require("dojo.html.style");dojo.require("dojo.html.selection");dojo.require("dojo.event.*");dojo.require("dojo.widget.*");dojo.require("dojo.widget.HtmlWidget");dojo.widget.defineWidget( "dojo.widget.FilteringTable", dojo.widget.HtmlWidget, function(){ // summary // Initializes all properties for the widget. this.store=new dojo.collections.Store(); //declare per instance changeable widget properties this.valueField="Id"; this.multiple=false; this.maxSelect=0; this.maxSortable=1; // how many columns can be sorted at once. this.minRows=0; this.defaultDateFormat = "%D"; this.isInitialized=false; this.alternateRows=false; this.columns=[]; this.sortInformation=[{ index:0, direction:0 }]; // CSS definitions this.headClass=""; this.tbodyClass=""; this.headerClass=""; this.headerUpClass="selectedUp"; this.headerDownClass="selectedDown"; this.rowClass=""; this.rowAlternateClass="alt"; this.rowSelectedClass="selected"; this.columnSelected="sorted-column"; },{ // dojo widget properties isContainer: false, templatePath: null, templateCssPath: null, // methods. getTypeFromString: function(/* string */s){ // summary // Gets a function based on the passed string. var parts = s.split("."), i = 0, obj = dj_global; do{ obj = obj[parts[i++]]; } while (i < parts.length && obj); return (obj != dj_global) ? obj : null; // function }, // custom data access. getByRow: function(/*HTMLTableRow*/row){ // summary // Returns the data object based on the passed row. return this.store.getByKey(dojo.html.getAttribute(row, "value")); // object }, getDataByRow: function(/*HTMLTableRow*/row){ // summary // Returns the source data object based on the passed row. return this.store.getDataByKey(dojo.html.getAttribute(row, "value")); // object }, getRow: function(/* Object */ obj){ // summary // Finds the row in the table based on the passed data object. var rows = this.domNode.tBodies[0].rows; for(var i=0; i<rows.length; i++){ if(this.store.getDataByKey(dojo.html.getAttribute(rows[i], "value")) == obj){ return rows[i]; // HTMLTableRow } } return null; // HTMLTableRow }, getColumnIndex: function(/* string */fieldPath){ // summary // Returns index of the column that represents the passed field path. for(var i=0; i<this.columns.length; i++){ if(this.columns[i].getField() == fieldPath){ return i; // integer } } return -1; // integer }, getSelectedData: function(){ // summary // returns all objects that are selected. var data=this.store.get(); var a=[]; for(var i=0; i<data.length; i++){ if(data[i].isSelected){ a.push(data[i].src); } } if(this.multiple){ return a; // array } else { return a[0]; // object } }, isSelected: function(/* object */obj){ // summary // Returns whether the passed object is currently selected. var data = this.store.get(); for(var i=0; i<data.length; i++){ if(data[i].src == obj){ return true; // boolean } } return false; // boolean }, isValueSelected: function(/* string */val){ // summary // Returns the object represented by key "val" is selected. var v = this.store.getByKey(val); if(v){ return v.isSelected; // boolean } return false; // boolean }, isIndexSelected: function(/* number */idx){ // summary // Returns the object represented by integer "idx" is selected. var v = this.store.getByIndex(idx); if(v){ return v.isSelected; // boolean } return false; // boolean }, isRowSelected: function(/* HTMLTableRow */row){ // summary // Returns if the passed row is selected. var v = this.getByRow(row); if(v){ return v.isSelected; // boolean } return false; // boolean }, reset: function(){ // summary // Resets the widget to its initial internal state. this.store.clearData(); this.columns = []; this.sortInformation = [ {index:0, direction:0} ]; this.resetSelections(); this.isInitialized = false; this.onReset(); }, resetSelections: function(){ // summary // Unselects all data objects. this.store.forEach(function(element){ element.isSelected = false; }); }, onReset:function(){ // summary // Stub for onReset event. }, // selection and toggle functions select: function(/*object*/ obj){ // summary // selects the passed object. var data = this.store.get(); for(var i=0; i<data.length; i++){ if(data[i].src == obj){ data[i].isSelected = true; break; } } this.onDataSelect(obj); }, selectByValue: function(/*string*/ val){ // summary // selects the object represented by key "val". this.select(this.store.getDataByKey(val)); }, selectByIndex: function(/*number*/ idx){ // summary // selects the object represented at index "idx". this.select(this.store.getDataByIndex(idx)); }, selectByRow: function(/*HTMLTableRow*/ row){ // summary // selects the object represented by HTMLTableRow row. this.select(this.getDataByRow(row)); }, selectAll: function(){ // summary // selects all objects. this.store.forEach(function(element){ element.isSelected = true; }); }, onDataSelect: function(/* object */obj){ // summary // Stub for onDataSelect event. }, toggleSelection: function(/*object*/obj){ // summary // Flips the selection state of passed obj. var data = this.store.get(); for(var i=0; i<data.length; i++){ if(data[i].src == obj){ data[i].isSelected = !data[i].isSelected; break; } } this.onDataToggle(obj); }, toggleSelectionByValue: function(/*string*/val){ // summary // Flips the selection state of object represented by val. this.toggleSelection(this.store.getDataByKey(val)); }, toggleSelectionByIndex: function(/*number*/idx){ // summary // Flips the selection state of object at index idx. this.toggleSelection(this.store.getDataByIndex(idx)); }, toggleSelectionByRow: function(/*HTMLTableRow*/row){ // summary // Flips the selection state of object represented by row. this.toggleSelection(this.getDataByRow(row)); }, toggleAll: function(){ // summary // Flips the selection state of all objects. this.store.forEach(function(element){ element.isSelected = !element.isSelected; }); }, onDataToggle: function(/* object */obj){ // summary // Stub for onDataToggle event. }, // parsing functions, from HTML to metadata/SimpleStore _meta:{ field:null, format:null, filterer:null, noSort:false, sortType:"String", dataType:String, sortFunction:null, filterFunction:null, label:null, align:"left", valign:"middle", getField:function(){ return this.field || this.label; }, getType:function(){ return this.dataType; } }, createMetaData: function(/* object */obj){ // summary // Take a JSON-type structure and make it into a ducktyped metadata object. for(var p in this._meta){ // rudimentary mixin if(!obj[p]){ obj[p] = this._meta[p]; } } if(!obj.label){ obj.label=obj.field; } if(!obj.filterFunction){ obj.filterFunction=this._defaultFilter; } return obj; // object }, parseMetadata: function(/* HTMLTableHead */head){ // summary // Parses the passed HTMLTableHead element to create meta data. this.columns=[]; this.sortInformation=[]; var row = head.getElementsByTagName("tr")[0]; var cells = row.getElementsByTagName("td"); if (cells.length == 0){ cells = row.getElementsByTagName("th"); } for(var i=0; i<cells.length; i++){ var o = this.createMetaData({ }); // presentation attributes if(dojo.html.hasAttribute(cells[i], "align")){ o.align = dojo.html.getAttribute(cells[i],"align"); } if(dojo.html.hasAttribute(cells[i], "valign")){ o.valign = dojo.html.getAttribute(cells[i],"valign"); } if(dojo.html.hasAttribute(cells[i], "nosort")){ o.noSort = (dojo.html.getAttribute(cells[i],"nosort")=="true"); } if(dojo.html.hasAttribute(cells[i], "sortusing")){ var trans = dojo.html.getAttribute(cells[i],"sortusing"); var f = this.getTypeFromString(trans); if (f != null && f != window && typeof(f)=="function"){ o.sortFunction=f; } } o.label = dojo.html.renderedTextContent(cells[i]); if(dojo.html.hasAttribute(cells[i], "field")){ o.field=dojo.html.getAttribute(cells[i],"field"); } else if(o.label.length > 0){ o.field=o.label; } else { o.field = "field" + i; } if(dojo.html.hasAttribute(cells[i], "format")){ o.format=dojo.html.getAttribute(cells[i],"format"); } if(dojo.html.hasAttribute(cells[i], "dataType")){ var sortType = dojo.html.getAttribute(cells[i],"dataType"); if(sortType.toLowerCase()=="html" || sortType.toLowerCase()=="markup"){ o.sortType = "__markup__"; // always convert to "__markup__" }else{ var type = this.getTypeFromString(sortType); if(type){ o.sortType = sortType; o.dataType = type; } } } // TODO: set up filtering mechanisms here. if(dojo.html.hasAttribute(cells[i], "filterusing")){ var trans = dojo.html.getAttribute(cells[i],"filterusing"); var f = this.getTypeFromString(trans); if (f != null && f != window && typeof(f)=="function"){ o.filterFunction=f; } } this.columns.push(o); // check to see if there's a default sort, and set the properties necessary if(dojo.html.hasAttribute(cells[i], "sort")){ var info = { index:i, direction:0 }; var dir = dojo.html.getAttribute(cells[i], "sort"); if(!isNaN(parseInt(dir))){ dir = parseInt(dir); info.direction = (dir != 0) ? 1 : 0; }else{ info.direction = (dir.toLowerCase() == "desc") ? 1 : 0; } this.sortInformation.push(info); } } if(this.sortInformation.length == 0){ this.sortInformation.push({ index:0, direction:0 }); } else if (this.sortInformation.length > this.maxSortable){ this.sortInformation.length = this.maxSortable; } }, parseData: function(/* HTMLTableBody */body){ // summary // Parse HTML data into native JSON structure for the store. if(body.rows.length == 0 && this.columns.length == 0){ return; // there's no data, ignore me. } // create a data constructor based on what we've got for the fields. var self=this; this["__selected__"] = []; var arr = this.store.getFromHtml(this.columns, body, function(obj, row){ obj[self.valueField] = dojo.html.getAttribute(row, "value"); if(dojo.html.getAttribute(row, "selected")=="true"){ self["__selected__"].push(obj); } }); this.store.setData(arr); for(var i=0; i<this["__selected__"].length; i++){ this.select(this["__selected__"][i]); } this.renderSelections(); delete this["__selected__"]; // say that we are already initialized so that we don't kill anything this.isInitialized=true; }, // standard events onSelect: function(/* HTMLEvent */e){ // summary // Handles the onclick event of any element. var row = dojo.html.getParentByType(e.target,"tr"); if(dojo.html.hasAttribute(row,"emptyRow")){ return; } var body = dojo.html.getParentByType(row,"tbody"); if(this.multiple){ if(e.shiftKey){ var startRow; var rows=body.rows; for(var i=0;i<rows.length;i++){ if(rows[i]==row){ break; } if(this.isRowSelected(rows[i])){ startRow=rows[i]; } } if(!startRow){ startRow = row; for(; i<rows.length; i++){ if(this.isRowSelected(rows[i])){ row = rows[i]; break; } } } this.resetSelections(); if(startRow == row){ this.toggleSelectionByRow(row); } else { var doSelect = false; for(var i=0; i<rows.length; i++){ if(rows[i] == startRow){ doSelect=true; } if(doSelect){ this.selectByRow(rows[i]); } if(rows[i] == row){ doSelect = false; } } } } else { this.toggleSelectionByRow(row); } } else { this.resetSelections(); this.toggleSelectionByRow(row); } this.renderSelections(); }, onSort: function(/* HTMLEvent */e){ // summary // Sort the table based on the column selected. var oldIndex=this.sortIndex; var oldDirection=this.sortDirection;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -