=============================combobox绑定和获取数据====================================
1、获取选中的值
$('#comboboxlist').combobox('getValue'); //单选时
$('#comboboxlist').combobox('getValues'); //多选时
2、赋值
$('#comboboxlist').combobox('setValue', '北京');
$('#cbbStatus').combobox({
url: '@Url.Action("GetHouseStatue", "HouseInfo")',
valueField: 'Value',
textField: 'Text',
onSelect: function (res) {
console.log(res);
}
});
<input id="cbbStatus" class="easyui-combobox" data-options="prompt:' - 选择状态 - '" editable="false" />
#region 获取房源信息状态
public ActionResult GetHouseStatue()
{
//房源状态0待审核1审核通过2审核不通过3上架4下架
List<ComboboxData> models = new List<ComboboxData>();
models.Add(new ComboboxData() { Text = "待审核", Value = "0" });
models.Add(new ComboboxData() { Text = "审核通过", Value = "1" });
models.Add(new ComboboxData() { Text = "审核不通过", Value = "2" });
models.Add(new ComboboxData() { Text = "上架", Value = "3" });
models.Add(new ComboboxData() { Text = "下架", Value = "4" });
return Json(models, JsonRequestBehavior.AllowGet);
}
#endregion
==========================================combotree绑定和获取数据=================================
$(function () {
$("#cbt").combotree({
width: 175,
url: '@Url.Action("GetComboTreeJson", "Test")',
valueField: 'id',
textField: 'text',
editable: false
});
$("#btnSubmit").click(function () {
//获取当前combotree的tree对象
var tree = $('#cbt').combotree('tree');
//获取当前选中的节点
var data = tree.tree('getSelected');
//查看当前选中节点的id
console.log("选择的数据", data.id, data.text);
});
});
/// <summary>
/// 异步加载无限Tree
/// </summary>
/// <returns></returns>
[HttpPost]
public string GetComboTreeJson()
{
//easyui 会每展开一个节点,往后端传一个id
string parentNodeId = RequestHelper.GetString("id") ?? null;
if (string.IsNullOrEmpty(parentNodeId))
{
parentNodeId = "0";
}
List<TreeModule> tree = GetSubNodes(parentNodeId, "module_fatherid","module_id", "module_name");
string JsonStr = JsonConvert.SerializeObject(tree);
return JsonStr;
}
/// <summary>
/// 获取ComboTree方法
/// </summary>
/// <param name="parentNodeId">父节点数据</param>
/// <param name="fatherColumn">父节点字段名</param>
/// <param name="idColumn">ID字段名</param>
/// <param name="textColumn">TEXT字段名</param>
/// <returns></returns>
private List<TreeModule> GetSubNodes(string parentNodeId, string fatherColumn,string idColumn,string textColumn)
{
DataTable dt = CreateDT();
List<TreeModule> Tree = new List<TreeModule>();
TreeModule TM = null;
if (dt != null && dt.Rows.Count > 0)
{
DataRow[] rows = dt.Select(string.Format("{0} = '{1}'", fatherColumn, parentNodeId));
foreach (DataRow item in rows)
{
string id = item[idColumn].ToString();
string text = item[textColumn].ToString();
TM = new TreeModule();
DataRow[] IsNulRows = dt.Select(string.Format("{0} = '{1}'", fatherColumn, id));
if (IsNulRows.Length > 0)
{
//这个很关键,此节点为closed状态,才可以展开,才能往后台传你点击的id
//看到Combotree的异步加载Demo,发现treegrid_data.json中 state="closed" 属性能把点击展开的节点Id传到后台中
TM.state = "closed";
}
TM.id = id;
TM.text = text;
Tree.Add(TM);
}
}
return Tree;
}
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>HouseInfo信息列表</title>
<link type="text/css" rel="stylesheet" href="/css/default.css" target="_blank" rel="external nofollow" />
<!--easyui-->
<link rel="stylesheet" type="text/css" href="/JS/jquery-easyui-1.5.5.5/themes/default/easyui.css" target="_blank" rel="external nofollow" />
<link rel="stylesheet" type="text/css" href="/JS/jquery-easyui-1.5.5.5/themes/default/tree.css" target="_blank" rel="external nofollow" />
<link rel="stylesheet" type="text/css" href="/JS/jquery-easyui-1.5.5.5/themes/icon.css" target="_blank" rel="external nofollow" />
<script type="text/javascript" src="/JS/jquery-easyui-1.5.5.5/jquery.min.js"></script>
<script type="text/javascript" src="/JS/jquery-easyui-1.5.5.5/jquery.easyui.min.js"></script>
<script src="/Js/jquery-easyui-1.5.5.5/locale/easyui-lang-zh_CN.js"></script>
<script src="~/Js/layer-v3.1.1/layer.js"></script>
<script src="~/Js/LayerExt.js"></script>
<script src="~/Js/JqueryExt.js"></script>
<script type="text/javascript">
$(function () {
$("#datagrid1").datagrid({
//title: "HouseInfo信息列表",
url: "@Url.Action("GetList", "HouseInfo")",
striped: true, //交替行换色
rownumbers: true, //行号
pagination: true, //显示底部分页
fitColumns: true,//自动适应。先给列随便加个宽度
toolbar: "#tb",
pagePosition: "bottom", // top|bottom|both
pageSize: 15,
pageList: [15, 30, 50, 100],
singleSelect: true, //如果为true,则只允许选择一行。
checkOnSelect: false, //true,当用户点击行的时候该复选框就会被选中或取消选中。
selectOnCheck: true, //true,单击复选框将永远选择行。
onClickRow: function (index, row) {
//var d_id = row["UserID"];
//alert(d_id);
},
columns: [[
//{ field: 'HouseID', title: '主键ID', width: 120, align: 'center', sortable: true },
{ field: 'HouseName', title: '房源名称', width: 120, align: 'center', sortable: true },
{ field: 'TypeID', title: '房源类型ID', width: 120, align: 'center', sortable: true },
{ field: 'PostUid', title: '发布人ID', width: 120, align: 'center', sortable: true },
{ field: 'HouseRent', title: '房源租金', width: 120, align: 'center', sortable: true },
{ field: 'HouseUnit', title: '租金单位默认月', width: 120, align: 'center', sortable: true },
{ field: 'HouseArea', title: '房源面积默认平方米', width: 120, align: 'center', sortable: true },
{ field: 'ProvinceID', title: '省份ID', width: 120, align: 'center', sortable: true },
{ field: 'CityID', title: '城市ID', width: 120, align: 'center', sortable: true },
{ field: 'AreaID', title: '县区ID', width: 120, align: 'center', sortable: true },
{ field: 'CircleID', title: '商圈ID', width: 120, align: 'center', sortable: true },
{ field: 'HouseStatue', title: '房源状态0待审核1审核通过2审核不通过3上架4下架', width: 120, align: 'center', sortable: true },
{ field: 'IsDeleted', title: '是否已删除0否1是', width: 120, align: 'center', sortable: true },
{ field: 'AddDate', title: '新增时间', width: 120, align: 'center', sortable: true },
{ field: 'OnsaleDate', title: '上架时间', width: 120, align: 'center', sortable: true },
{ field: 'IsTop', title: '是否已置顶', width: 120, align: 'center', sortable: true },
{ field: 'Dx', title: '操作', width: 120, align: 'center', formatter: formatOper }
]],
});
var p = $('#datagrid1').datagrid('getPager');
$(p).pagination({
beforePageText: '第',
afterPageText: '页 共 {pages} 页',
displayMsg: '当前显示 {from}-{to} 条记录,共 {total} 条记录'
});
//获取参数
function GetParas() {
var hstatus = $("#cbbStatus").combobox("getValue");
var cbbHouseType = $("#cbbHouseType").combobox("getValue");
var cbbProvince = $("#cbbProvince").combobox("getValue");
var cbbCity = $("#cbbCity").combobox("getValue");
var cbbArea = $("#cbbArea").combobox("getValue");
var txtPostMan = $("#txtPostMan").textbox("getValue");
var txtKeyword = $("#txtKeyword").textbox("getValue");
var para = {
hstatus: hstatus,
cbbHouseType: cbbHouseType,
cbbProvince: cbbProvince,
cbbCity: cbbCity,
cbbArea: cbbArea,
txtPostMan: txtPostMan,
txtKeyword: txtKeyword
};
console.log(para);
return para;
}
//查询
$("#btnQuery").click(function () {
$('#datagrid1').datagrid('load', GetParas());
});
//清空
$("#btnClear").click(function () {
ClearForm();
});
//绑定房源信息状态
$('#cbbStatus').combobox({
url: '@Url.Action("GetHouseStatue", "HouseInfo")',
valueField: 'Value',
textField: 'Text',
onSelect: function (res) {
console.log(res);
}
});
//绑定房源类型
$('#cbbHouseType').combobox({
url: '@Url.Action("GetHouseType", "HouseInfo")',
valueField: 'Value',
textField: 'Text',
onSelect: function (res) {
console.log(res);
}
});
//加载省份
$('#cbbProvince').combobox({
url: "@Url.Action("GetPca", "HouseInfo")",
queryParams: { pId: 0 },
valueField: 'Value',
textField: 'Text',
onChange: function () {
var value = $(this).combobox('getValue');
console.log("==选择省份==", value)
GetCity(value);
}
});
//加载城市
function GetCity(pId) {
$('#cbbCity').combobox({
url: "@Url.Action("GetPca", "HouseInfo")",
queryParams: { pId: pId },
valueField: 'Value',
textField: 'Text',
onChange: function () {
var value = $(this).combobox('getValue');
GetArea(value);
console.log("==选择城市==", value)
}
});
}
//加载区域
function GetArea(pId) {
$('#cbbArea').combobox({
url: "@Url.Action("GetPca", "HouseInfo")",
queryParams: { pId: pId },
valueField: 'Value',
textField: 'Text',
onChange: function () {
var value = $(this).combobox('getValue');
console.log("==选择区域==", value)
}
});
}
});
//自定义操作列
function formatOper(val, row, index) {
var str = "";
str += '<a href="javascript:void(0);" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" onclick="doShow(' + row.HouseID + ')">查看</a>';
str += ' ';
str += '<a href="javascript:void(0);" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" onclick="doEdit(' + row.HouseID + ')">修改</a>';
str += ' ';
str += '<a href="javascript:void(0);" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" onclick="doDelete(' + row.HouseID + ')">删除</a>';
return str;
}
function doAdd() {
var url = "@Url.Action("Add", "HouseInfo")";
LayerOpen(url, "新增信息", "800px", "600px")
}
function doShow(a) {
var url = "@Url.Action("Show", "HouseInfo")" + "?id=" + a;
LayerOpen(url, "查看信息", "800px", "600px")
}
function doEdit(a) {
var url = "@Url.Action("Edit", "HouseInfo")" + "?id=" + a;
LayerOpen(url, "修改信息", "800px", "600px")
}
function doDelete(a) {
layer.confirm("确认删除?", { btn: ['确定', '取消'] }, function (index) {
$.ajax({
type: "post",
timeout: 3000,
url: "@Url.Action("Delete", "HouseInfo")",
data: { id: a },
dataType: "json", //返回数据形式为json
success: function (result) {
if (result.success) {
$('#datagrid1').datagrid('reload')
layer.alert("删除成功!", { icon: 1 }, function () {
layer.closeAll();
});
}
},
error: function (errorMsg) {
layer.alert("数据操作失败,请稍后重试!", { icon: 2 });
}
});
});
};
</script>
</head>
<body>
<div>
<table id="datagrid1"></table>
<div id="tb" class="datagrid_tb">
<a href="#" target="_blank" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="doAdd()">新增房源</a>
<input id="cbbStatus" class="easyui-combobox" data-options="prompt:' - 选择状态 - '" editable="false" />
<input id="cbbHouseType" class="easyui-combobox" data-options="prompt:' - 房源类型 - '" editable="false" />
<input id="cbbProvince" class="easyui-combobox" data-options="prompt:' - 选择省份 - '" editable="false" />
<input id="cbbCity" class="easyui-combobox" data-options="prompt:' - 选择城市 - '" editable="false" />
<input id="cbbArea" class="easyui-combobox" data-options="prompt:' - 选择县区 - '" editable="false" />
<input id="txtPostMan" class="easyui-textbox" data-options="prompt:'发布人名称'" />
<input id="txtKeyword" class="easyui-textbox" data-options="prompt:'房源名称'" />
<a href="javascript:void(0)" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" id="btnQuery" class="easyui-linkbutton" iconcls="icon-search">查询</a>
<a href="javascript:void(0)" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" id="btnClear" class="easyui-linkbutton" iconcls="icon-clear">清空</a>
</div>
</div>
</body>
</html>
using CRM.Core.Dal;
using CRM.Core.Model;
using Magic.Tool;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using CRM.Other;
namespace SiteProject.Areas.Admin.Controllers
{
public class HouseInfoController : Controller
{
#region 获取数据(一条)
/// <summary>
/// 获取数据(一条)
/// </summary>
/// <returns></returns>
public ActionResult Show()
{
int id = Magic.Tool.RequestHelper.GetInt("id", 0);
var mode = DataRootBase.Context.From<HouseInfo>().Where(p => p.HouseID == id).ToFirstDefault();
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
ViewBag.Data = JsonConvert.SerializeObject(mode, timeFormat);
return View();
}
#endregion
#region 修改数据(一条)
[HttpGet]
public ActionResult Edit()
{
int id = Magic.Tool.RequestHelper.GetInt("id", 0);
var mode = DataRootBase.Context.From<HouseInfo>().Where(p => p.HouseID == id).ToFirstDefault();
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
ViewBag.Data = JsonConvert.SerializeObject(mode, timeFormat);
return View();
}
[HttpPost]
/// <summary>
/// 修改数据(一条)
/// </summary>
/// <returns></returns>
public ActionResult Edit(HouseInfo data)
{
var result = new Result();
try
{
int i = DataRootBase.Context.Update<HouseInfo>(data);
result.success = (i > 0) ? true : false;
}
catch (Exception ex)
{
result.success = false;
result.message = " 请检查数据是否填写正确";
}
return Json(result);
}
#endregion
#region 新增数据(一条)
[HttpGet]
public ActionResult Add()
{
var mode = new HouseInfo();
ViewBag.Data = mode;
return View();
}
[HttpPost]
/// <summary>
/// 新增数据(一条)
/// </summary>
/// <returns></returns>
public ActionResult Add(HouseInfo data)
{
var result = new Result();
try
{
data.HouseUnit = 1;
data.IsDeleted = false;
data.AddDate = DateTime.Now;
data.IsTop = false;
int i = DataRootBase.Context.Insert<HouseInfo>(data);
result.success = (i > 0) ? true : false;
}
catch (Exception ex)
{
result.success = false;
result.message = " 请检查数据是否填写正确";
}
return Json(result);
}
#endregion
#region 删除数据(一条)
[HttpPost]
/// <summary>
/// 删除数据(一条)
/// </summary>
/// <returns></returns>
public ActionResult Delete()
{
var result = new Result();
int id = Magic.Tool.RequestHelper.GetInt("id", 0);
try
{
int i = DataRootBase.Context.Delete<HouseInfo>(id);
result.success = (i > 0) ? true : false;
}
catch (Exception)
{
result.success = false;
}
return Json(result);
}
#endregion
#region 获取数据(列表)
/// <summary>
/// 获取数据(列表)
/// </summary>
/// <returns></returns>
public ActionResult List()
{
return View();
}
public ActionResult GetList()
{
string status = RequestHelper.GetString("hstatus");
string cbbHouseType = RequestHelper.GetString("cbbHouseType");
string cbbProvince = RequestHelper.GetString("cbbProvince");
string cbbCity = RequestHelper.GetString("cbbCity");
string cbbArea = RequestHelper.GetString("cbbArea");
string txtPostMan = RequestHelper.GetString("txtPostMan");
string txtKeyword = RequestHelper.GetString("txtKeyword");
int pageIndex = Magic.Tool.RequestHelper.GetInt("page", 0); //当前页码
int pageSize = Magic.Tool.RequestHelper.GetInt("rows", 0); //每页显示记录数
string sort = Magic.Tool.RequestHelper.GetString("sort"); //排序字段名。
string order = Magic.Tool.RequestHelper.GetString("order"); //排序方式
//查询参数
//string keyword = Magic.Tool.RequestHelper.GetString("keyword");
//string beginDate = Magic.Tool.RequestHelper.GetString("beginTime");
//string endDate = Magic.Tool.RequestHelper.GetString("endTime");
int TotalRecord = 0;
int TotalPage = 0;
string orderBy = string.Format("{0} {1}", sort, order); //排序
orderBy = string.IsNullOrWhiteSpace(orderBy) ? "HouseID" : orderBy;
//执行存储过程
var result = DataRootBase.QueryPagingMssql<HouseInfo>("HouseInfo", "*", orderBy, pageIndex, pageSize, "", out TotalRecord, out TotalPage);
ResultEasyUI data = new ResultEasyUI();
data.total = TotalRecord;
List<JObject> list = new List<JObject>();
foreach (var item in result)
{
list.Add(JObject.FromObject(item));
}
data.rows = list;
//格式化时间
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
var JsonStr = JsonConvert.SerializeObject(data, timeFormat);
return Content(JsonStr);
}
#endregion
#region 返回通知类
/// <summary>
/// 返回通知类
/// </summary>
public class Result
{
public bool success { set; get; }
public string message { set; get; }
}
#endregion
#region 获取房源信息状态
public ActionResult GetHouseStatue()
{
//房源状态0待审核1审核通过2审核不通过3上架4下架
var models = SiteCommonData.GetHouseInfoStatus();
return Json(models, JsonRequestBehavior.AllowGet);
}
#endregion
#region 获取房源类型
public ActionResult GetHouseType()
{
var models = DataRootBase.Context.From<HouseType>().ToList();
List<ComboboxData> lists = new List<ComboboxData>();
foreach (var item in models)
{
lists.Add(new ComboboxData() { Text = item.TypeName, Value = item.TypeID.ToString() });
}
return Json(lists, JsonRequestBehavior.AllowGet);
}
#endregion
#region 获取省市县
/// <summary>
/// 获取省市县
/// </summary>
/// <param name="pId"></param>
/// <returns></returns>
public ActionResult GetPca(int pId)
{
var models = DataRootBase.Context.From<CRM.Core.Model.Areas>()
.Where(p => p.ParentId == pId).ToList();
var list = new List<ComboboxData>();
foreach (var item in models)
{
list.Add(new ComboboxData() { Value = item.Id.ToString(), Text = item.Name });
}
list.Insert(0, new ComboboxData { Value = "0", Text = "请选择" });
return Json(list, JsonRequestBehavior.AllowGet);
}
#endregion
}
}