<div id="div1"></div>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
/*
showDivID 显示类别信息的Div的ID,
selid 生成sel 的ID
inputTxtID 用于显示的ID 如 330000_330001_33000102
hidid 隐藏的ID和name
type 1是存code 2是存name ,3 用于无级产品分类
*/
function ClassType(showDivID, selid, inputTxtID,hidid,type) {
this.IndexLevel = 0;
this.MaxLevel = 99;
var showDivID = showDivID;
var selid = selid;
var thisObj = this;
var selectvalue = inputTxtID == undefined ? "" : inputTxtID; //330000_330001_33000102
var array = selectvalue.split("_");
var thehidid = hidid == undefined ? "hidsel" : hidid;
var theType = type == undefined ? "1" : type;
this.Init = function () {
this.AddNodeForSel("0", "", "");
}
this.AddNodeForSel = function (strParentID, strID, strName) {
if (this.MaxLevel <= this.IndexLevel) return;
this.IndexLevel++;
var id = selid + this.IndexLevel;
var tmpsel = $("<select id=\"" + id + "\"></select>");
$("#" + showDivID).append(tmpsel);
//临时存放当前的索引 因为获取数据是异步方式
var tmpIndex = this.IndexLevel;
$(tmpsel).append($("<option value=\"" + strParentID + "_false_" + tmpIndex + "\">请选择</option>"));
//取得该类的数据并填充select
$.getJSON("/handle/Handler1.ashx", { code: strParentID, time: (new Date).getTime() }, function (data) {
var aa = 0;
$.each(data, function (i, item) {
$(tmpsel).append($("<option value=\"" + item.code + "_" + item.sub + "_" + tmpIndex + "\">" + item.name + "</option>"));
aa++;
if (item.code == array[tmpIndex - 1]) {
$(tmpsel).get(0).options[aa].selected = true;
thisObj.SetValue(tmpIndex, [item.code, item.name]);
}
});
});
$("#" + id).live("change", this.SelectChange);
if (selectvalue.length > 0) {
if (tmpIndex < array.length) {
thisObj.AddNodeForSel(array[tmpIndex - 1]);
} else {
selectvalue = "";
}
}
}
this.SelectChange = function () {
this.value = $(this).val();
var strID = parseInt(this.value.split("_")[0]);
var isShow = "true" == this.value.split("_")[1];
thisObj.IndexLevel = parseInt(this.value.split("_")[2]);
thisObj.SetValue(thisObj.IndexLevel, [strID, $(this).text()]);
$("#" + selid + thisObj.IndexLevel).nextAll().remove();
if (isShow) {
thisObj.AddNodeForSel(strID);
}
}
this.SetValue = function (tmpIndex, arr) {
if (theType == "3") {
var va_ = arr[0];
if ($("#" + thehidid).length > 0) {
$("#" + thehidid).val(va_);
} else {
$("#" + showDivID).prepend("<input type=\"hidden\" name=\"" + thehidid + "\" id=\"" + thehidid + "\" value=\"" + va_ + "\" />");
}
} else {
var va_ = theType == "1" ? arr[0] : arr[1];
if ($("#" + thehidid + tmpIndex).length > 0) {
$("#" + thehidid + tmpIndex).val(va_);
} else {
$("#" + showDivID).prepend("<input type=\"hidden\" name=\"" + thehidid + tmpIndex + "\" id=\"" + thehidid + tmpIndex + "\" value=\"" + va_ + "\" />");
}
}
}
}
$(document).ready(function () {
//var dd = new ClassType("div1", "sel", "330000_330001_33000102","hidsel","3");
var dd = new ClassType("div1", "sel","","hidsel","3");
dd.Init();
});
</script>
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string parentid = context.Request["code"].ToString();
DataTable dt = new DataTable();
dt.Columns.Add("code");
dt.Columns.Add("name");
dt.Columns.Add("parentid");
DataRow dr = dt.NewRow();
dr["code"] = 330000;
dr["name"] = "浙江省";
dr["parentid"] = "0";
dt.Rows.Add(dr);
DataRow dr2 = dt.NewRow();
dr2["code"] = 330001;
dr2["name"] = "杭州市";
dr2["parentid"] = "330000";
dt.Rows.Add(dr2);
DataRow dr4 = dt.NewRow();
dr4["code"] = 33000101;
dr4["name"] = "拱墅区";
dr4["parentid"] = "330001";
dt.Rows.Add(dr4);
DataRow dr5 = dt.NewRow();
dr5["code"] = 33000102;
dr5["name"] = "江干区";
dr5["parentid"] = "330001";
dt.Rows.Add(dr5);
DataRow dr3 = dt.NewRow();
dr3["code"] = 330002;
dr3["name"] = "宁波市";
dr3["parentid"] = "330000";
dt.Rows.Add(dr3);
DataRow dr1 = dt.NewRow();
dr1["code"] = 310000;
dr1["name"] = "江西省";
dr1["parentid"] = "0";
dt.Rows.Add(dr1);
//StringBuilder sb = new StringBuilder();
//sb.Append("[");
//sb.Append("{\"code\":\"330000\",\"name\":\"浙江省\"}");
//sb.Append(",{\"code\":\"310000\",\"name\":\"江西省\"}");
//sb.Append("]");
context.Response.Write(Getjoson(dt,parentid));
}
private string Getjoson(DataTable dt, string parentid)
{
StringBuilder sb = new StringBuilder();
sb.Append("[");
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["parentid"].ToString() == parentid)
{
sb.Append("{\"code\":\"" + dt.Rows[i]["code"] + "\",\"name\":\"" + dt.Rows[i]["name"] + "\",\"sub\":\"" + hasSub(dt, dt.Rows[i]["code"].ToString()) + "\"},");
}
}
sb.Remove(sb.Length - 1, 1);
sb.Append("]");
return sb.ToString();
}
private string hasSub(DataTable dt, string parentid)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["parentid"].ToString() == parentid)
{
return "true";
}
}
return "false";
}