Twitter bootstrap 如何在剑道UI中使用双列表框

Twitter bootstrap 如何在剑道UI中使用双列表框,twitter-bootstrap,asp.net-mvc-4,c#-4.0,kendo-ui,Twitter Bootstrap,Asp.net Mvc 4,C# 4.0,Kendo Ui,如何在剑道UI中创建自定义小部件?演示如何在剑道UI框架中创建自定义小部件。它还演示了如何将数据绑定到自定义小部件 您可以动态创建小部件的各种组件(ListView、按钮等),并在自定义小部件的init方法中连接事件处理程序。也就是说,您不需要将HTML与所有组件混在一起(除非您愿意)。假设您希望与ASP.NET MVC 4和剑道框架结合使用 我们将使用Razor语法和C# 首先,我们在视图中为代码编写占位符。我们将连接剑道控制和 var urlgetcascadeMultipleSelect

如何在剑道UI中创建自定义小部件?

演示如何在剑道UI框架中创建自定义小部件。它还演示了如何将数据绑定到自定义小部件

您可以动态创建小部件的各种组件(ListView、按钮等),并在自定义小部件的init方法中连接事件处理程序。也就是说,您不需要将HTML与所有组件混在一起(除非您愿意)。

假设您希望与ASP.NET MVC 4和剑道框架结合使用

我们将使用Razor语法和C#

首先,我们在视图中为代码编写占位符。我们将连接剑道控制和


var urlgetcascadeMultipleSelectBrandTypeByBrand=“@(Url.Action(“getcascadeMultipleSelectBrandTypeByBrand”,“DropDownList”))”;
@Html.LabelFor(m=>m.BrandId)
@(Html.Kendo().DropDownListFor(m=>m.BrandId) .DataSource(source=> { source.Read(Read=> { read.Action(“GetCascadedBlandBySegment”、“DropDownList”) .数据(“过滤器段”); }) .ServerFiltering(true); }) .DataTextField(“品牌名称”) .DataValueField(“BrandId”) .Filter(FilterType.Contains) .CascadeFrom(“段ID”) .OptionLabel(“选择品牌”) .Events(evt=>evt.Change(“onBrandIdDdlChange”)) .HtmlAttributes(新的{@style=“width:100%;”})) @Html.ValidationMessageFor(m=>m.BrandId) @Html.LabelFor(m=>m.BrandTypeIdList)
@if(Model.IsEdit) { @Html.ListBoxFor(m=>m.BrandTypeIdList,Html.GetBrandTypeByBrandIdSelectListItemsList(Model.BrandId)) } 其他的 { @Html.ListBoxFor(m=>m.BrandTypeIdList,new List()) } @Html.ValidationMessageFor(m=>m.BrandTypeIdList)
然后,我们创建C#helper代码以与之配套

public static IEnumerable<SelectListItem> GetBrandTypeByBrandIdSelectListItemsList(this HtmlHelper htmlHelper, int brandId)
{
    using (var dbContext = new Entities())
    {
        return dbContext.BrandType.Where(x => x.Active == true && x.BrandId == brandId).Select(BrandTypeToDdlSelector).ToList();
    }
}

public static Func<BrandType, SelectListItem> BrandTypeToDdlSelector
{
    get
    {
        return (x => new SelectListItem()
        {
            Value = x.BrandTypeId.ToString(),
            Text = x.Name
        });
    }
}

public JsonResult GetCascadeMultiSelectBrandTypeByBrand(int? brandId)
{
    var brandTypesList = DbContext.BrandType.Where(p => p.Active == true);

    if (brandId != null)
    {
        brandTypesList = brandTypesList.Where(p => p.BrandId == brandId);
    }

    return Json(brandTypesList.Select(x => new { BrandTypeId = x.BrandTypeId, BrandTypeName = x.Name }), JsonRequestBehavior.AllowGet);
}
public static IEnumerable GetBrandTypeByBrandIdSelectListItemsList(此HtmlHelper HtmlHelper,int brandId)
{
使用(var dbContext=new Entities())
{
返回dbContext.BrandType.Where(x=>x.Active==true&&x.BrandId==BrandId);
}
}
公共静态函数BrandTypeToDdlSelector
{
得到
{
返回(x=>newselectListItem()
{
Value=x.BrandTypeId.ToString(),
Text=x.名称
});
}
}
public JsonResult GetCascade MultiselectBrandTypeByBrand(int?brandId)
{
var brandTypesList=DbContext.BrandType.Where(p=>p.Active==true);
if(brandId!=null)
{
brandTypesList=brandTypesList.Where(p=>p.BrandId==BrandId);
}
返回Json(brandTypesList.Select(x=>new{BrandTypeId=x.BrandTypeId,BrandTypeName=x.Name}),JsonRequestBehavior.AllowGet;
}
然后,我们创建JS代码,在运行时操作HTML,并将选定的值绑定到MVC模型

var brandTypeIdDualListbox = new Object();

$(document).ready(function ()
{   
    //we create the dual list control
    brandTypeIdDualListbox = $('select[name="BrandTypeIdList"]').bootstrapDualListbox({
    nonSelectedListLabel: 'Non-selected',
    selectedListLabel: 'Selected',
    preserveSelectionOnMove: 'moved',
    moveOnSelect: false,    
    });

    //we setup the change event for the control
    $('select[name="BrandTypeIdList').on('change', function (args)
    {
        //we traverse every option
        $("#BrandTypeIdList option").each(function (index,element)
        {
            //we check if the element has the `data-sortindex` attribute
            if (!!$(element).attr('data-sortindex'))
                $(element).attr('selected', 'selected');
            else
                $(element).removeAttr('selected');
        });
    })
});


function filterBrands()
{
    var brandId = $("#BrandId").val();
    if (brandId == "")
        brandId = "-1";
    return {
        BrandId: brandId
    };
}

function populateBrandTypeIdDualListbox()
{
    $.getJSON(urlGetCascadeMultiSelectBrandTypeByBrand, filterBrands(), function (data)
    {
        var items;
        $.each(data, function (i, item)
        {
            brandTypeIdDualListbox.append("<option value=" + item.BrandTypeId/*Value*/ + ">" + item.BrandTypeName/*Key or Text*/ + "</option>");
        });
        brandTypeIdDualListbox.trigger('bootstrapDualListbox.refresh', true); // we refresh the control
    });
}

function onBrandIdDdlChange(evt)
{
    var brandIdDropDownList = $("#BrandId").data("kendoDropDownList");

    $('#BrandTypeIdList').empty();
    brandTypeIdDualListbox.trigger('bootstrapDualListbox.refresh', true);
    if ($("#BrandId").val() == "" || $("#BrandId").val() == "-1")
    {
        //if no value is selected we disable the control
        $(".bootstrap-duallistbox-container").find("*").prop("disabled", true);
    }
    else
    {
        populateBrandTypeIdDualListbox();
        $(".bootstrap-duallistbox-container").find("*").prop("disabled", false); // we enable the control

    }
}
var brandTypeIdDualListbox=新对象();
$(文档).ready(函数()
{   
//我们创建双列表控件
brandTypeIdDualListbox=$('select[name=“BrandTypeIdList”]”)。bootstrapDualListbox({
nonSelectedListLabel:“未选定”,
selectedListLabel:“已选”,
preserveSelectionOnMove:“已移动”,
moveOnSelect:false,
});
//我们为控件设置更改事件
$('select[name=“BrandTypeIdList')。在('change',函数(args)上
{
//我们遍历每个选项
$(“#BrandTypeIdList选项”)。每个(函数(索引,元素)
{
//我们检查元素是否具有'data sortindex'属性
if(!!$(element).attr('data-sortindex'))
$(element.attr('selected','selected');
其他的
$(元素).removeAttr('selected');
});
})
});
函数filterBrands()
{
var brandId=$(“#brandId”).val();
如果(brandId==“”)
brandId=“-1”;
返回{
布兰迪:布兰迪
};
}
函数populateBrandTypeIdDualListbox()
{
$.getJSON(urlGetCascadeMultiSelectBrandTypeByBrand,filterBrands(),函数(数据)
{
风险值项目;
$。每个(数据、功能(i、项)
{
brandTypeIdDualListbox.append(“+item.BrandTypeName/*键或Text*/+”);
});
brandTypeIdDualListbox.trigger('bootstrapDualListbox.refresh',true);//我们刷新控件
});
}
函数onBrandIDLDLChange(evt)
{
var brandIdDropDownList=$(“#BrandId”).data(“kendoDropDownList”);
$(“#BrandTypeIdList”).empty();
brandTypeIdDualListbox.trigger('bootstrapDualListbox.refresh',true);
如果($(“#BrandId”).val()=”|$(“#BrandId”).val()=“-1”)
{
//如果未选择任何值,则禁用该控件
$(“.bootstrap duallistbox容器”).find(“*”).prop(“已禁用”,true);
}
其他的
{
populateBrandTypeIdDualListbox();
$(“.bootstrap duallistbox container”).find(“*”).prop(“disabled”,false);//我们启用该控件
}
}

双列表框现在是jquery默认剑道Ui的一部分

 $(document).ready(function () {
            $("#optional").kendoListBox({
                connectWith: "selected",
                toolbar: {
                    tools: ["moveUp", "moveDown", "transferTo", "transferFrom", "transferAllTo", "transferAllFrom", "remove"]
                }
            });

            $("#selected").kendoListBox();
        });
或.NETMVC版本

@(Html.Kendo().ListBox()
            .Name("optional")
            .Toolbar(toolbar =>
            {
                toolbar.Position(Kendo.Mvc.UI.Fluent.ListBoxToolbarPosition.Right);
                toolbar.Tools(tools => tools
                    .MoveUp()
                    .MoveDown()
                    .TransferTo()
                    .TransferFrom()
                    .TransferAllTo()
                    .TransferAllFrom()
                    .Remove()
                );
            })
            .ConnectWith("selected")
            .BindTo(ViewBag.Attendees)
        )

        @(Html.Kendo().ListBox()
            .Name("selected")
            .BindTo(new List<string>())
            .Selectable(ListBoxSelectable.Multiple)
        )
@(Html.Kendo().ListBox())
.名称(“可选”)
.Toolbar(Toolbar=>
{
位置(Kendo.Mvc.UI.Fluent.ListBoxToolbarPosition.Right);
toolbar.Tools(Tools=>Tools
.MoveUp()
.MoveDown()
.TransferTo()
.转让自
.TransferAllTo()
.TransferAllFrom()
.删除()
);
})
.ConnectWith(“选定”)
.BindTo(ViewBag.Attendes)
)
@(Html.Kendo().ListBox())
.姓名(“选定”)
.BindTo(新列表())
.可选(ListBoxSelective.Multiple)
)
@(Html.Kendo().ListBox()
            .Name("optional")
            .Toolbar(toolbar =>
            {
                toolbar.Position(Kendo.Mvc.UI.Fluent.ListBoxToolbarPosition.Right);
                toolbar.Tools(tools => tools
                    .MoveUp()
                    .MoveDown()
                    .TransferTo()
                    .TransferFrom()
                    .TransferAllTo()
                    .TransferAllFrom()
                    .Remove()
                );
            })
            .ConnectWith("selected")
            .BindTo(ViewBag.Attendees)
        )

        @(Html.Kendo().ListBox()
            .Name("selected")
            .BindTo(new List<string>())
            .Selectable(ListBoxSelectable.Multiple)
        )