/* * Cascade dropdown Box Jqueyr plugin, V1.2 * Copyright, Leo.liu * This plugin includes 2 non-flush cascading dropdown box Plug-ins: * Plugin One: Cascadedropdowndata is not used in Ajax interaction with the server, you need to advance all the dropdown box data read out. Demo * Method One: var dataItem = [[' All ', '-1 ', ' 0 '], [' a001 ', ' a001 ', ' 0 '], [' a002 ', ' a002 ', ' 0 '], [' a003 ', ' a003 ', ' 0 '] , [' b001 ', ' b001 ', ' a001 '], [' b002 ', ' b002 ', ' a001 '], [' b003 ', ' b003 ', ' a002 '], [' b004 ', ' b004 ', ' a003 '] , [' c001 ', ' 001 ', ' b001 '], [' c002 ', ' 002 ', ' b001 '], [' c003 ', ' 003 ', ' b002 '], [' c004 ', ' 004 ', ' b003 ']]; $.cascadedropdownbind.bind (DataItem, {sourceid: ' Select1 ', Selectvalue: ' a001 ', Parentvalue: ' 0 ', Child: {sourceid: ' Select2 ', Selectvalue: ' b002 ', Child: {sourceid: ' Select3 ', Selectvalue: ' c002 '} } }); * This method has a flaw: a requires that the Drop-down box's value not be the same as the parent in the parent-child counterpart B) cannot set the full selection rule * * Method Two: var data = [[' All ', ' 0 '], [' a001 ', ' a001 '], [' a002 ', ' a002 '], [' a003 ', ' a003 ']]; var data2 = [[' All ', ' 0 ', ' 0 '], [' b001 ', ' b001 ', ' a001 '], [' b002 ', ' b002 ', ' a001 '], [' b003 ', ' b003 ', ' a002 '], [' b004 ', ' b00 4 ', ' a003 ']]; var data3 = [[' All ', ' 0 ', ' 0 '], [' c001 ', ' 001 ', ' b001 '], [' c002 ', ' 002 ', ' b001 '], [' c003 ', ' 003 ', ' b002 '], [' c004 ', ' 004 ', ' b003 ']]; $.cascadedropdownbind.bind (data, {sourceid: ' Select1 ', Selectvalue: ' a001 '}); $.cascadedropdownbind.bind (data2, {sourceid: ' Select2 ', Selectvalue: ' b002 ', parentid: ' Select1 '}); $.cascadedropdownbind.bind (data3, {sourceid: ' Select3 ', Selectvalue: ' c002 ', parentid: ' Select2 '}); * Method three See Cascadedropdownbind.bind content */ Jquery.extend ({ The data object of the Drop-down box Cascadedropdowndata:function () { Configuration Properties ******* This.removefirst = true; Do you want to remove the first item This.appentallvalue = '; Displays all items if the parent item's value is equal to this value This.sourceid = '; This dropdown box ID This.parentid = '; Parent dropdown Box ID This.items = []; The data of the item, the two-dimensional array, the form: [[' Text ', ' value ', ' parent ID '],......]; Values and parent IDs can be omitted This.selectvalue = '; Initialize the selected item This.parentvalue = null; Used to filter out the items needed for the group from a bunch of data, typically to bind the first Drop-down box Configuration Properties ******* The following are global variables that do not need to be set externally This.child = null; var currentdrop = null; This.bind = function () { Currentdrop = $ (' # ' + This.sourceid); This.clear (); Populating data Items This.fillitem (); Set the selection if (This.selectvalue) { Currentdrop.val (This.selectvalue); } Set the Change event for the parent Drop-down box This.setchange (); }; Clean Fill Items This.clear = function () { if (This.removefirst) { Currentdrop.empty (); } else { for (var i = currentdrop[0].options.length-1 i > 0; i--) { Currentdrop[0].options[i] = null; } } }; Populating data Items This.fillitem = function () { var _parentvalue = This.parentvalue; if (This.parentid) _parentvalue = $ (' # ' + This.parentid). Val (); var all = false; if (this.appentallvalue && _parentvalue = = this.appentallvalue) all = true; $.each (This.items, function (index, item) { var val = item.length > 1? ITEM[1]: item[0]; If value is not specified, replace with text if (all | | item.length <= 2 | | item[2] = = _parentvalue) {//If the length is less than 3, it is considered that there is no parent Drop-down box to populate all items Currentdrop.append (' <option value= ' + val + ' "> ' + item[0] + ' </option> '); } }); }; Set the Change event for the parent Drop-down box This.setchange = function () { if (This.parentid) { $ (' # ' + This.parentid). Bind (' Change ', this.change); } }; Parent dropdown Box Event callback function var _this = this; This.change = function () { _this.clear (); _this.fillitem (); Currentdrop.change (); }; }, Cascadedropdownbind: { Bind:function (data, setting) { var obj = new $.cascadedropdowndata (); $.extend (obj, setting); Obj.items = data; Obj.bind (); if (setting.child) { Setting.child.parentID = Setting.sourceid This.bind (data, setting.child); } } } }); /* * Plug-in two: Ajaxdropdowndata for each child drop-down box is post to the server to fetch data binding. * The best thing about the plug-in is that it will be used to achieve efficient data caching. * Note: The cached key value is not only the value of the parent Drop-down box, but a combination of values from the top-level Drop-down box to the current parent Drop-down box, in order to cope with the different children of the same parent dropdown box * For the same reason, the form that is sent back to the server in postback also contains the values of all the parent Drop-down boxes * Use method: var firstitems = null; You can also bind the data array of the first Drop-down box to this, or set to null without changing the Drop-down box. $.ajaxdropdownbind.bindtopdrop (' Select1 ', Firstitems, NULL, FALSE, ' Select2 '); $.ajaxdropdownbind.bindcallback (' Select2 ', NULL, FALSE, ' Select3 ', ' http://localhost:4167/GetDropDownData.ashx? Action=select1 '); $.ajaxdropdownbind.bindcallback (' Select3 ', NULL, FALSE, NULL, ' http://localhost:4167/GetDropDownData.ashx?action= Select2 '); $ (' #Select1 '). Change (); * Most importantly, cascading ID settings are not missing. Advanced usage See the contents of the $.ajaxdropdownbind.bindcallback method. */ Jquery.extend ({ This object is a linked list structure Ajaxdropdowndata:function () { Configuration Properties ******* This.sourceid = '; This dropdown box ID This.items = []; The data for this item, two-dimensional array, form: [[' Text ', ' value ', ' parent ID '],......]; Values and parent IDs can be omitted This.callback = null; callback function to get the next-level method, which receives 2 parameters: value, DropDownList the values selected by the parent Drop-down box, and an instance of itself This.childid = '; The associated child control ID This.removefirst = true; Do you want to remove the first option from this item This.selectvalue = '; This item initializes the selected value Configuration Properties ******* The following are system variables and methods **************************************************** This.childitem = []; A list of child objects for caching This.parentobj = null; Parent object This.canchange = true; This.clearitem = true; This.bind = function () { $.ajaxdropdownbind.binddata (this); }; This.binddata = function (setting) { $.extend (this, setting); $.ajaxdropdownbind.binddata (this); }; /* goes back to the root node, starting from the root node to cascade the object of the current correct drop-down box Because there is only one event in the dropdown box and the corresponding object has multiple, you need to go back to the root first, and start looking at the root */ This.getrightonchangeobj = function () { if (this.parentobj) Return THIS.PARENTOBJ.GETRIGHTONCHANGEOBJ (). childitem[$ (' # ' + This.parentObj.sourceID). Val ()]; Recursion Else return this; } }, Ajaxdropdownbind: { Currentdrop:null, _thisdata:null, Callbackpool: [], Clean Fill Items Clear:function () { if (_thisdata.removefirst) { Currentdrop.empty (); } else { for (var i = currentdrop[0].options.length-1 i > 0; i--) { Currentdrop[0].options[i] = null; } } }, Populating data Items Fillitem:function () { for (var i = 0; i < _thisdata.items.length; i++) { var val = _thisdata.items[i].length > 1? _THISDATA.ITEMS[I][1]: _thisdata.items[i][0]; If value is not specified, replace with text Currentdrop.append (' <option value= ' + val + ' "> ' + _thisdata.items[i][0] + ' </option> '); } Set the selection if (_thisdata.selectvalue) { Currentdrop.val (_thisdata.selectvalue); _thisdata.selectvalue = '; } }, Parameter data is the object of the currently changing Drop-down box Binddata:function (data) { _thisdata = data; Currentdrop = $ (' # ' + _thisdata.sourceid); The node itself, not the child if (_thisdata.clearitem) This.clear (); if (_thisdata.items) This.fillitem (); if (_thisdata.childid) { if (!currentdrop.data (' binded ')) {//To determine if the event has been bound, not bound Currentdrop.data (' binded ', true); var _firstchangeobj = _thisdata; Because there is only one event in the dropdown box, and the corresponding object has multiple, the object here is a bound object, not the correct object Currentdrop.bind (' Change ', function () { var rightchilditem = _firstchangeobj.getrightonchangeobj (). ChildItem; var thisvalue = $ (' # ' + _firstchangeobj.sourceid). Val (); Gets the value of the currently changing Drop-down box, note that you cannot replace it with Currentdrop because Currentdrop is also an old value if (Rightchilditem[thisvalue]) { Console.log (' cache '); Rightchilditem[thisvalue].bind (); Using cached data to bind } else { Console.log (' GetData '); A new instance, and establish a linked list relationship var dropdata = new $.ajaxdropdowndata (); Dropdata.sourceid = _firstchangeobj.childid; Dropdata.parentobj = _firstchangeobj; Rightchilditem[thisvalue] = Dropdata; Setting up caching if (_firstchangeobj.callback)//If there is a callback function called directly, otherwise call the system automatically generated functions _firstchangeobj.callback (Thisvalue, dropdata); callback function else { var callback = $.ajaxdropdownbind.callbackpool[dropdata.sourceid]; if (callback) Callback (Thisvalue, Dropdata); } } }); } if (_thisdata.canchange) Currentdrop.change (); } }, Bind First Level dropdown box Bindtopdrop:function (SourceID, items, Selectvalue, Removefirst, childID) { var mydrop = new $.ajaxdropdowndata (); Mydrop.sourceid = SourceID; Mydrop.items = items; if (!items | | items.length = = 0) Mydrop.clearitem = false; Mydrop.removefirst = Removefirst; Mydrop.selectvalue = Selectvalue; Mydrop.childid = childID; Mydrop.canchange = false; Mydrop.bind (); }, Bind Child Drop-down Box Bindcallback:function (SourceID, Selectvalue, Removefirst, childID, Parentposturl) { var callback = function (value, DropDownList) { var postdata = {}; var parentobj = dropdownlist.parentobj; while (Parentobj) { Postdata[parentobj.sourceid] = $ (' # ' + Parentobj.sourceid). Val (); Parentobj = Parentobj.parentobj; } $.getjson (Parentposturl + ' &jsoncallback=? ', PostData, function (data) { Dropdownlist.items = data; Dropdownlist.removefirst = Removefirst; Dropdownlist.selectvalue = Selectvalue; Dropdownlist.childid = childID; Dropdownlist.bind (); }); }; This.callbackpool[sourceid] = callback; } } }); |