动态加载省市区

数据库表结构:
在这里插入图片描述
SQL语句:

CREATE TABLE [Cascade]
(
ID int,
Name nvarchar(50),
ParentID int,
[Type] int
)
//aspx代码
<script type="text/javascript">
    var Province =<%=GetProvince()%>;
</script>
省:
<select class="easyui-combobox" id="Province" name="Province" style="width: 90px;" data-options="editable: false">
</select>
市:
<select class="easyui-combobox" id="City" name="City" style="width: 90px;" data-options="editable: false">
</select>
区:
<select class="easyui-combobox" id="Area" name="Area" style="width: 90px;" data-options="editable: false">
</select>
//js代码
$(document).ready(function () {
    //省
    $('#Province').combobox({
        data: Province,
        valueField: 'id',
        textField: 'text',
        editable: true,
        onChange: function (n, o) {
            if (n > 0) {
                getCity(n);
            }
            else {
                $('#City').combobox({
                    data: [{ "id": -1, "text": "所有市" }],
                    valueField: 'id',
                    textField: 'text',
                    editable: true
                })
                $("#City").combobox("setValue", -1)
            }
        }
    })
    $("#Province").combobox("setValue", -1)

    //市
    $('#City').combobox({
        data: [{ "id": -1, "text": "所有市" }],
        valueField: 'id',
        textField: 'text',
        editable: true,
        onChange: function (n, o) {
            if (n > 0) {
                getArea(n);
            }
            else {
                $('#Area').combobox({
                    data: [{ "id": -1, "text": "所有区" }],
                    valueField: 'id',
                    textField: 'text',
                    editable: true
                })
                $("#Area").combobox("setValue", -1)
            }
        }
    })
    $("#City").combobox("setValue", -1)

    //区
    $('#Area').combobox({
        data: [{ "id": -1, "text": "所有区" }],
        valueField: 'id',
        textField: 'text',
        editable: true
    })
    $("#Area").combobox("setValue", -1);

});//end ready


//绑定市
function GetCity(ParentID) {
    $.ajax({
        url: "Cascade.aspx?Action=GetCity&ParentID=" + ParentID,
        async: false,
        dataType: "json",
        success: function (data) {
            $('#City').combobox({
                data: data,
                valueField: 'id',
                textField: 'text',
                editable: true
            });
            $("#City").combobox("setValue", -1)
            GetArea(ParentID);
        },
        error: function () { }
    })
}

//绑定区
function GetArea(ParentID) {
    $.ajax({
        url: "Cascade.aspx?Action=GetArea&ParentID=" + ParentID,
        async: false,
        dataType: "json",
        success: function (data) {
            $('#Area').combobox({
                data: data,
                valueField: 'id',
                textField: 'text',
                editable: true
            });
            $("#Area").combobox("setValue", -1);
        },
        error: function () { }
    })
}
//aspx.cs代码
protected void Page_Load(object sender, EventArgs e)
{
     string Action = Utils.ReqStrParams("Action", "");
     switch (Action)
     {
       //获取市
        case "GetCity":
        GetCity();
        break;
        //获取区
        case "GetArea":
        GetArea();
        break;
     }
     Response.End();
}

/// <summary>
/// 查省
/// </summary>
/// <returns></returns>
public string GetProvince()
{
    DataTable dt = CascadeDB.GetProvince(1);
    List<dynamic> lst = new List<dynamic>();
    lst.Add(new
    {
        id = -1,
        text = "所有省"
    });
    lst.AddRange(dt.AsEnumerable().Select(p => new
    {
        id = p.Field<int>("ID"),
        text = p.Field<string>("Name")
    }).ToList<dynamic>());
    return JsonConvert.SerializeObject(lst);
}

/// <summary>
/// 查市
/// </summary>
private void GetCity()
{
    int ParentID = Utils.ReqIntParams("ParentID", -1);
    DataTable dt = CascadeDB.GetCityArea(2, ParentID);
    List<dynamic> lst = new List<dynamic>();
    lst.Add(new
    {
        id = -1,
        text = "所有市"
    });
    lst.AddRange(dt.AsEnumerable().Select(p => new
    {
        id = p.Field<int>("ID"),
        text = p.Field<string>("Name")
    }).ToList<dynamic>());
    Response.Write(JsonConvert.SerializeObject(lst));
}

/// <summary>
/// 查区
/// </summary>
/// <returns></returns>
public void GetArea()
{
    int ParentID = Utils.ReqIntParams("ParentID", -1);
    DataTable dt = CascadeDB.GetCityArea(3, ParentID);
    List<dynamic> lst = new List<dynamic>();
    lst.Add(new
    {
        id = -1,
        text = "所有区"
    });
    lst.AddRange(dt.AsEnumerable().Select(p => new
    {
        id = p.Field<int>("ID"),
        text = p.Field<string>("Name")
    }).ToList<dynamic>());
    Response.Write(JsonConvert.SerializeObject(lst));
}
        
//CascadeDB.cs代码
//连接字符串
static string strConn = ConfigurationManager.ConnectionStrings["CnnhoRechargePlatformConnectionString"].ToString();

/// <summary>
/// 执行查询,返回DataTable对象
/// </summary>
/// <param name="strSQL">sql语句</param>
/// <returns>DataTable对象</returns>
public static DataTable GetTable(string strSQL)
{
    DataTable dt = new DataTable(); ;
    using (SqlConnection conn = new SqlConnection(strConn))
    {
        SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
        da.Fill(dt);
    }
    return dt;
}

/// <summary>
/// 查省
/// </summary>
/// <param name="Type">类型</param>
/// <returns></returns>
public static DataTable GetProvince(int Type)
{
    string sql = @"select * from [Cascade] where [Type]= " + Type;
    return GetTable(sql);
}

/// <summary>
/// 查市/区
/// </summary>
/// <param name="Type">类型</param>
/// <param name="ParentID">父级ID</param>
/// <returns></returns>
public static DataTable GetCityArea(int Type, int ParentID)
{
     string sql = String.Format(@"select * from [Cascade] where [Type]={0} and ParentID={1} ", Type, ParentID);
     return GetTable(sql);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
element cascader 是一个基于 Vue.js 的级联选择器组件,可以实现动态加载省市区的功能。 在使用 element cascader 动态加载省市区之前,我们需要先准备好相应的数据。一般来说,省市区数据是以树形结构组织的,每个地区都有一个唯一的标识符,以及父级地区的标识符来建立层级关系。我们可以将这些地区数据存储在一个数组中,每个元素包含地区的名称、标识符以及父级地区的标识符。 首先,我们需要在页面中引入 element cascader 组件,并将数据作为 props 传入: ``` <el-cascader :options="options" v-model="selectedAreas"></el-cascader> ``` 在 Vue 实例中,我们需要定义一个数组来保存选中的地区,并且需要定义一个方法来动态加载地区数据。当用户选择某个地区时,我们需要根据该地区的标识符来获取该地区的子级地区数据,并将数据添加到 props 的 options 中,实现动态加载省市区的效果。 ``` data() { return { selectedAreas: [], // 保存选中的地区 options: [], // 地区数据 }; }, methods: { loadAreas(selectedOptions) { const targetOption = selectedOptions[selectedOptions.length - 1]; targetOption.loading = true; // 设置加载状态为 true // TODO: 根据 targetOption 中的标识符获取子级地区数据,并将数据添加到 targetOption 的 children 中 targetOption.loading = false; // 数据加载完成后设置加载状态为 false }, }, ``` 在 `loadAreas` 方法中,我们可以根据 `targetOption` 中的标识符来向后端发送请求,获取子级地区数据。获取到数据后,我们将其添加到 `targetOption` 的 `children` 属性中,并设置 `loading` 状态为 false,表示数据加载完成。 通过以上的操作,我们就可以在使用 element cascader 组件时实现动态加载省市区的功能。用户选择某个地区时,组件会根据选择的地区标识符动态加载该地区的子级地区数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值