Sending a data filtering code is very simple and useful to take _ javascript skills

Source: Internet
Author: User
Sending a data filtering code is very simple and useful.

<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> <! -- Saved from url = (0058) http://www.barelyfitz.com/projects/filterlist/example.html --> <HTML> <HEAD> <TITLE> Filterlist Example </TITLE> <META http-equiv = Content-Type content = "text/html; charset = gb2312 "> <script type =" text/javascript "> <! --/* = =================* $ Id: filterlist. js, v 1.3 2003/10/08 17:13:49 pat Exp $ Copyright 2003 Patrick Fitzgerald http://www.barelyfitz.com/webdesign/articles/filterlist/ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. this program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A P Articular purpose. see the GNU General Public License for more details. you shoshould have written ed a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc ., 59 Temple Place, Suite 330, Boston, MA? -1307 USA * ============================================ ================= */function filterlist (selectobj) {// ============================================== ================/// PARAMETERS // ================================== ============================================/// html select object // For example, set this to document. myform. myselect this. selectobj = selectobj; // Flags for regexp matching. // "I" = ignore case; "" = do not ignore case // Yo U can use the set_ignore_case () method to set this. flags = 'I'; // Which parts of the select list do you want to match? This. match_text = true; this. match_value = false; // You can set the hook variable to a function that // is called whenever the select list is filtered. // For example: // this. hook = function () {// alert ()} // Flag for debug alerts // Set to true if you are having problems. this. show_debug = false; // ================================================ ================/// METHODS // ================================= ============ ========================/// ------------------------------------------------------------ This. init = function () {// This method initilizes the object. // This method is called automatically when you create the object. // You shocould call this again if you alter the selectobj parameter. if (! This. selectobj) return this. debug ('selectobj not defined'); if (! This. selectobj. options) return this. debug ('selectobj. options not defined '); // Make a copy of the select list options array this. optionscopy = new Array (); if (this. selectobj & this. selectobj. options) {for (var I = 0; I <this. selectobj. options. length; I ++) {// Create a new Option this. optionscopy [I] = new Option (); // Set the text for the Option this. optionscopy [I]. text = selectobj. options [I]. tex T; // Set the value for the Option. // If the value wasn't set in the original select list, // then use the text. if (selectobj. options [I]. value) {this. optionscopy [I]. value = selectobj. options [I]. value;} else {this. optionscopy [I]. value = selectobj. options [I]. text ;}}}// -------------------------------------------------------- this. reset = function () {// This method resets the select list to Original state. // It also unselects all of the options. this. set ('');} // ------------------------------------------------------ this. set = function (pattern) {// This method removes all of the options from the select list, // then adds only the options that match the pattern regexp. // It also unselects all of the options. var loop = 0, index = 0, regexp, e; if (! This. selectobj) return this. debug ('selectobj not defined'); if (! This. selectobj. options) return this. debug ('selectobj. options not defined '); // Clear the select list so nothing is displayed this. selectobj. options. length = 0; // Set up the regular expression. // If there is an error in the regexp, // then return without selecting any items. try {// Initialize the regexp = new RegExp (pattern, this. flags);} catch (e) {// There was an error creating the regexp. // If the user specified a function hook, // call it now, then return if (typeof this. hook = 'function') {this. hook ();} return;} // Loop through the entire select list and // add the matching items to the select list for (loop = 0; loop <this. optionscopy. length; loop ++) {// This is the option that we're re currently testing var option = this. optionscopy [loop]; // Check if we have a match if (this. match_text & regexp. test (option. text) | (this. match_value & regexp. test (option. value) {// We have a match, so add this option to the select list // and increment the index this. selectobj. options [index ++] = new Option (option. text, option. value, false) ;}// If the user specified a function hook, // call it now if (typeof this. hook = 'function') {this. hook () ;}// -------------------------------------------------------- this. set_ignore_case = function (value) {// This method sets the regexp flags. // If value is true, sets the flags to "I ". // If value is false, sets the flags "". if (value) {this. flags = 'I';} else {this. flags = ''; }}// ------------------------------------------------------ this. debug = function (msg) {if (this. show_debug) {alert ('filterlist: '+ msg );}} // ================================================ ==============/// Initialize the object // ============================= ================================ this. init ();} // --> script <META content = "MSHTML 6.00.2900.3020" name = GENERATOR> </HEAD> <BODY> Filterlist Example </P> </BODY> </HTML>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.