Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
gorthik

Filtrar dados de uma tabela

Recommended Posts

gorthik

table.filter = function(o,filters,args) {
	var cell;
	args = args || {};

	var t = this.resolve(o,args);
	var tdata = this.tabledata[t.id];

	// If new filters were passed in, apply them to the table's list of filters
	if (!filters) {
		// If a null or blank value was sent in for 'filters' then that means reset the table to no filters
		tdata.filters = null;
	}
	else {
		// Allow for passing a select list in as the filter, since this is common design
		if (filters.nodeName=="SELECT" && filters.type=="select-one" && filters.selectedIndex>-1) {
			filters={ 'filter':filters.options[filters.selectedIndex].value };
		}
		// Also allow for a regular input
		if (filters.nodeName=="INPUT" && filters.type=="text") {
			filters={ 'filter':"/^"+filters.value+"/" };
		}
		// Force filters to be an array
		if (typeof(filters)=="object" && !filters.length) {
			filters = [filters];
		}

		// Convert regular expression strings to RegExp objects and function strings to function objects
		for (var i=0,L=filters.length; i<L; i++) {
			var filter = filters[i];
			if (typeof(filter.filter)=="string") {
				// If a filter string is like "/expr/" then turn it into a Regex
				if (filter.filter.match(/^\/(.*)\/$/)) {	

					filter.filter = new RegExp(RegExp.$1);
					filter.filter.regex=true;
				}
				// If filter string is like "function (x) { ... }" then turn it into a function
				else if (filter.filter.match(/^function\s*\(([^\)]*)\)\s*\{(.*)}\s*$/)) {
					filter.filter = Function(RegExp.$1,RegExp.$2);
				}
			}
			// If some non-table object was passed in rather than a 'col' value, resolve it 
			// and assign it's column index to the filter if it doesn't have one. This way, 
			// passing in a cell reference or a select object etc instead of a table object 
			// will automatically set the correct column to filter.
			if (filter && !def(filter.col) && (cell=getParent(o,"TD","TH"))) {
				filter.col = this.getCellIndex(cell);
			}

			// Apply the passed-in filters to the existing list of filters for the table, removing those that have a filter of null or ""
			if ((!filter || !filter.filter) && tdata.filters) {
				delete tdata.filters[filter.col];
			}
			else {
				tdata.filters = tdata.filters || {};
				tdata.filters[filter.col] = filter.filter;
			}
		}
		// If no more filters are left, then make sure to empty out the filters object
		for (var j in tdata.filters) { var keep = true; }
		if (!keep) {
			tdata.filters = null;
		}
	}		
	// Everything's been setup, so now scrape the table rows
	return table.scrape(o);
};

A questão é que tenho este código, contudo a filtragem que ele faz é case sensitive. Já tentei perceber e tentar compor para que o transforme em case INsensitive mas não estou com grande sorte.

Será que algum entendido na matéria me poderia dar um apoiozinho nisto?

Obrigado desde já  :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.