Work on search page search record function encapsulation (store to local)

Source: Internet
Author: User

//!* package adds search record function (Newrecord:str, gets history when parameter is empty; Num: Record number, default is 12;)function Addhisrecord (NewRecord, num) {num= num | | A; //to get locally stored records        varHisrecord_str = Window.localStorage.getItem ('Hisrecord_str'); //When the obtained value is NULL, create an empty array to store it in        if(typeofHisrecord_str = ='Object'){            varHisrecord = []; Hisrecord_str= Hisrecord.join ('|'); Window.localStorage.setItem ('Hisrecord_str', HISRECORD_STR); }        //Converting to arraysHisrecord = Hisrecord_str.split ('|'); //Empty Array When Hisrecord_str is an empty string        if( !hisrecord_str)        {Hisrecord.pop (); }        //when the argument is not empty, determine:        if(NewRecord) {//1. When the incoming value does not exist in the array, the new record is added to the first;            if(Hisrecord.indexof (newrecord) = =-1) {hisrecord.unshift (NewRecord); //When the record is less than NUM, add to the first, and when it is greater than or equal to NUM, add to the first and delete the bottom                if(Hisrecord.length >num)                {Hisrecord.pop (); }            }Else{                //2. When the incoming value is present in the array, the record of the corresponding position is deleted and the new record is added to the first;                varIndexrecord =Hisrecord.indexof (NewRecord); Hisrecord.splice (Indexrecord,1);            Hisrecord.unshift (NewRecord); }            //Convert to string stored locallyHisrecord_str = Hisrecord.join ('|'); Window.localStorage.setItem ('Hisrecord_str', HISRECORD_STR); }Else{//returns the acquired history when the argument is empty, Type:array;            returnHisrecord; }    }
//!* Package Delete search record function (index:0,1,2,..., ' all ';)function Removehisrecord (index) {//to get locally stored records        varHisrecord_str = Window.localStorage.getItem ('Hisrecord_str'); //Converting to arrays        varHisrecord = Hisrecord_str.split ('|'); //Empty Array When Hisrecord_str is an empty string        if( !hisrecord_str)        {Hisrecord.pop (); }Else if(Index = =' All') {Hisrecord.splice (0, hisrecord.length); }Else{hisrecord.splice (index,1); }        //store and render processed data to a pageHisrecord_str = Hisrecord.join ('|'); Window.localStorage.setItem ('Hisrecord_str', HISRECORD_STR); Hideorshowhisrecord (HISRECORD_STR);//Hide History section//template_data (Hisrecord, ' #search-item-tmpl3 ', ' #searchBox3 ');}
//!* Package Query and render search record function when entering pagefunction Hisrecord () {varHisrecord =Addhisrecord (); varHisrecord_str =Hisrecord.join (); //rendering when records exist        if(HISRECORD_STR) {template_data (Hisrecord,'#search-item-tmpl1','#searchBox1'); }        //Hideorshowhisrecord (HISRECORD_STR);//control display and hide}

// render data to the page according to the template (data: What to render, Template: Templates, Box: boxes to be rendered    ) // need to introduce dot.js template engine     function Template_data (data, template, box) {        var searchitemtmpl = dot.template ($ (template). html ());        $ (box). HTML (Searchitemtmpl (data));    }

Work on search page search record function encapsulation (store to local)

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.