簡體   English   中英

為asp.net mvc剃須刀編寫自定義插件/擴展程序,例如kendoui和telerik

[英]Write custom plugins/extensions like kendoui and telerik for asp.net mvc razor

TelerikkendoUI提供了許多html控件,例如網格,圖表等,以增強asp.net mvc中的ui。 更有趣的是,它們被用作html擴展,因此我們可以將某些模型綁定到控件,這也使它具有某種強類型。 它還使用javascript庫來處理客戶端交互。 我需要了解如何編寫自定義插件(如kendo和telerik),如構建自己的網格組件。 我應該遵循的正確模式是什么?

這不是一個很好的例子,但我認為這是一個好的開始。

此方法生成dataTable

public static string GenerateTableHtml<TValue>(IEnumerable<TValue> model, string DeleteUrl, string EditUrl) 
        {
            StringBuilder Table = new StringBuilder();

            Table.AppendLine("<script type='text/javascript'> $(document).ready(function () {"
                            + "$('.btnDelete').click(function () {"
                            + "var url = $(this).attr('dropzone');"
                            + "$('#dialog-form').attr('action', url);})"
                            + "})</script>");

            Table.AppendLine(" <div class=\"dataTables_wrapper\"><div class=\"\">");

            string TableButtonsTemplate = "<td><div class=\"table_buttons\">"
                                        + "<a href=\"{0}\"><img src=\"../Images/icons/dark/pencil.png\")\" title=\"რედაქტირება\" /></a>"
                                        + "<a href=\"#\" id=\"opener\" class=\"btnDelete\" dropzone=\"{1}\">"
                                        +"<img class=\"\" src=\"../Images/icons/dark/close.png\")\" title=\"წაშლა\" /></a>"
                                        + "</div></td>";

            string TableButtons = String.Empty;
            bool HaveActionButtons = false;
            string rowID = string.Empty;

            if (String.IsNullOrEmpty(DeleteUrl) == false || string.IsNullOrEmpty(EditUrl) == false)
            {
                HaveActionButtons = true;
            }

            Table.AppendLine("<table class=\"display dTable\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">");
            Table.AppendLine("<thead>");
            Table.AppendLine("<tr>");

            int ColumnIndex = 0;
            int rowIndexer = 0;

            var fType = model.GetType().FullName;
            var startIndex = fType.IndexOf("[[") + 2;
            var type = fType.Substring(startIndex, fType.Length - startIndex - 2);

            foreach (var item in model)
            {

                if (ColumnIndex == 0)
                {
                    var properties = item.GetType().GetProperties();

                    foreach (var prop in properties)
                    {
                        var metaData = ModelMetadataProviders.Current.GetMetadataForProperty(null,
                                       Type.GetType(type), prop.Name);

                        if (metaData.DisplayName != null)
                        {
                            Table.AppendLine("<th class=\"ui-state-default\" rowspan=\"1\" colspan=\"1\">" + metaData.DisplayName + "</th>");
                        }
                        else
                        {
                            Table.AppendLine("<th class=\"ui-state-default\" rowspan=\"1\" colspan=\"1\">" + prop.Name + "</th>");
                        }
                    }

                    Table.AppendLine("<th></th>");
                    Table.AppendLine("</tr>");
                    Table.AppendLine("</thead>");
                    Table.AppendLine("<tbody>");
                }

                foreach (var value in item.GetType().GetProperties().Select(x => x.GetValue(item, null)))
                {
                    int rowCount = item.GetType().GetProperties().Select(x => x.GetValue(item, null)).Count();
                    rowIndexer++;

                    if (rowIndexer != rowCount)
                    {
                        if (rowIndexer == 1)
                        {
                            Table.AppendLine("<tr class=\"gradeA odd\">");
                        }

                        if (value != null)
                        {
                            string val = value.ToString();
                            Table.AppendLine("<td>" + val + "</td>");
                            rowID = item.GetType().GetProperty("ID").GetValue(item, null).ToString();
                        }
                        else
                        {
                            Table.AppendLine("<td></td>");
                        }
                    }
                    else
                    {
                        if (value != null)
                        {
                            Table.AppendLine("<td>" + value.ToString() + "</td>");
                        }
                        else
                        {
                            Table.AppendLine("<td></td>");
                        }

                        if (HaveActionButtons == true)
                        {
                            Table.AppendLine(String.Format(TableButtonsTemplate, EditUrl + rowID, DeleteUrl + rowID));
                        }

                        Table.AppendLine("</tr>");

                        if (rowIndexer != item.GetType().GetProperties().Count())
                        {
                            Table.AppendLine("<tr class=\"gradeA odd\">");
                        }
                    }
                }
                rowIndexer = 0;
                ColumnIndex++;
            }

            Table.AppendLine("</tbody></table></div></div>");

            Table.AppendLine(String.Format(Resources.MainResources.ModalDialogConfirm, "Confirmation",
                "<strong>Warning!</strong><br /> Are you sure you want to delete user?", ""));

            return Table.ToString();
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM