ホームページ バックエンド開発 C#.Net チュートリアル Lauiui コンポーネントをカプセル化する asp.net コアの詳細な例

Lauiui コンポーネントをカプセル化する asp.net コアの詳細な例

Oct 11, 2017 am 10:23 AM
asp.net core layui

この記事では、asp.net core のカプセル化されたlayui コンポーネントの共有例の詳細な説明を主に紹介します。編集者はそれが非常に優れていると考えたので、参考として共有します。エディターに従って、どのパッケージを使用すればよいか見てみましょう。ここでは TagHelper が使用されていますが、これは何ですか?自分でドキュメントを読んでください

TagHelper の使い方を学ぶとき、私が最も望むのは、リファレンスとして使用できるデモがあることです

    コンポーネントをカプセル化するにはどうすればよいですか?
  • さまざまな状況を実装するにはどうすればよいですか?
  • もっと良い、より効率的な方法はありますか?
  • 検索して検索して、最終的には mvc の TagHelpers を調べてから、TagHelper のドキュメントをよく調べました


もともと記事を書こうと思っていたいくつかのコンポーネントを仕方なくいじってみました。しかし、建国記念日は終わったことがわかりました~

デモのダウンロード

エフェクトのプレビュー

このコードは参考用です。異なる意見がある場合は、お気軽に教えてください

チェックボックスチェックボックスコンポーネント。 package


タグ名: cl -checkbox


タグ属性: asp-for: バインドされたフィールド、指定する必要があります

    asp-items: バインディング単一オプションのタイプ: IEnumerable<SelectListItem>
  1. asp -skin:layui スキン スタイル、デフォルトまたはオリジナル
  2. asp-title: チェック ボックスが使用されたときに表示される単なるテキストであり、項目が指定されていない場合、デフォルトのチェック ボックスの値は true です

、これはカプセル化するときにソース コードを見るときに見つかります。 2 つの非常に便利なコード部分


1. 複数選択が可能かどうかを確認します:


コードをコピーします

コードは次のとおりです:

1

2

var realModelType = For.ModelExplorer.ModelType; //通过类型判断是否为多选

var _allowMultiple = typeof(string) != realModelType &amp;&amp; typeof(IEnumerable).IsAssignableFrom(realModelType);

ログイン後にコピー
2.モデル バインディングのリスト値を取得します (複数選択)


コードをコピーします

コードは次のとおりです:

1

var currentValues = Generator.GetCurrentValues(ViewContext,For.ModelExplorer,expression: For.Name,allowMultiple: true);

ログイン後にコピー
これらの 3 行のコードは、mvc に付属の SelectTagHelper に含まれています


なぜなら、コアは実際に一般的に使用される select など、多くの TagHelper が提供されています。これは優れた参照オブジェクトであり、問​​題が発生したときに検索すると、予期しない結果が得られる可能性があります。 example


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

using System.Collections.Generic;

using Microsoft.AspNetCore.Mvc.Rendering;

using Microsoft.AspNetCore.Mvc.ViewFeatures;

using Microsoft.AspNetCore.Razor.TagHelpers;

 

namespace LayuiTagHelper.TagHelpers

{

 /// &lt;summary&gt;

 /// 复选框

 /// &lt;/summary&gt;

 /// &lt;remarks&gt;

 /// 当Items为空时显示单个,且选择后值为true

 /// &lt;/remarks&gt;

 [HtmlTargetElement(CheckboxTagName)]

 public class CheckboxTagHelper : TagHelper

 {

  private const string CheckboxTagName = &quot;cl-checkbox&quot;;

  private const string ForAttributeName = &quot;asp-for&quot;;

  private const string ItemsAttributeName = &quot;asp-items&quot;;

  private const string SkinAttributeName = &quot;asp-skin&quot;;

  private const string SignleTitleAttributeName = &quot;asp-title&quot;;

  protected IHtmlGenerator Generator { get; }

  public CheckboxTagHelper(IHtmlGenerator generator)

  {

   Generator = generator;

  }

 

  [ViewContext]

  public ViewContext ViewContext { get; set; }

 

  [HtmlAttributeName(ForAttributeName)]

  public ModelExpression For { get; set; }

 

  [HtmlAttributeName(ItemsAttributeName)]

  public IEnumerable&lt;SelectListItem&gt; Items { get; set; }

 

  [HtmlAttributeName(SkinAttributeName)]

  public CheckboxSkin Skin { get; set; } = CheckboxSkin.默认;

 

  [HtmlAttributeName(SignleTitleAttributeName)]

  public string SignleTitle { get; set; }

 

  public override void Process(TagHelperContext context, TagHelperOutput output)

  {

   //获取绑定的生成的Name属性

   string inputName = ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(For?.Name);

   string skin = string.Empty;

   #region 风格

   switch (Skin)

   {

    case CheckboxSkin.默认:

     skin = &quot;&quot;;

     break;

    case CheckboxSkin.原始:

     skin = &quot;primary&quot;;

     break;

   }

   #endregion

   #region 单个复选框

   if (Items == null)

   {

    output.TagName = &quot;input&quot;;

    output.TagMode = TagMode.SelfClosing;

    output.Attributes.Add(&quot;type&quot;, &quot;checkbox&quot;);

    output.Attributes.Add(&quot;id&quot;, inputName);

    output.Attributes.Add(&quot;name&quot;, inputName);

    output.Attributes.Add(&quot;lay-skin&quot;, skin);

    output.Attributes.Add(&quot;title&quot;, SignleTitle);

    output.Attributes.Add(&quot;value&quot;, &quot;true&quot;);

    if (For?.Model?.ToString().ToLower() == &quot;true&quot;)

    {

     output.Attributes.Add(&quot;checked&quot;, &quot;checked&quot;);

    }

    return;

   }

   #endregion

   #region 复选框组

   var currentValues = Generator.GetCurrentValues(ViewContext,For.ModelExplorer,expression: For.Name,allowMultiple: true);

   foreach (var item in Items)

   {

    var checkbox = new TagBuilder(&quot;input&quot;);

    checkbox.TagRenderMode = TagRenderMode.SelfClosing;

    checkbox.Attributes[&quot;type&quot;] = &quot;checkbox&quot;;

    checkbox.Attributes[&quot;id&quot;] = inputName;

    checkbox.Attributes[&quot;name&quot;] = inputName;

    checkbox.Attributes[&quot;lay-skin&quot;] = skin;

    checkbox.Attributes[&quot;title&quot;] = item.Text;

    checkbox.Attributes[&quot;value&quot;] = item.Value;

    if (item.Disabled)

    {

     checkbox.Attributes.Add(&quot;disabled&quot;, &quot;disabled&quot;);

    }

    if (item.Selected || (currentValues != null &amp;&amp; currentValues.Contains(item.Value)))

    {

     checkbox.Attributes.Add(&quot;checked&quot;, &quot;checked&quot;);

    }

 

    output.Content.AppendHtml(checkbox);

   }

   output.TagName = &quot;&quot;;

   #endregion

  }

 }

 public enum CheckboxSkin

 {

  默认,

  原始

 }

}

ログイン後にコピー

ラジオラジオボタンコンポーネントパッケージ


タグ名: cl-radio


タグ属性: asp-for: バインドされたフィールドを指定する必要があります

asp-items:バインディングの単一オプションのタイプは次のとおりです: IEnumerable<SelectListItem>


    直接的には単純すぎます
  1. RadioTagHelperコード

  2. 1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    @{

    string sex=&quot;男&quot;;

    var Items=new List&lt;SelectListItem&gt;()

       {

        new SelectListItem() { Text = &quot;男&quot;, Value = &quot;男&quot; },

        new SelectListItem() { Text = &quot;女&quot;, Value = &quot;女&quot;},

        new SelectListItem() { Text = &quot;不详&quot;, Value = &quot;不详&quot;,Disabled=true }

       };

    }

    &lt;cl-checkbox asp-items=&quot;Model.Items&quot; asp-for=&quot;Hobby1&quot; asp-skin=&quot;默认&quot;&gt;&lt;/cl-checkbox&gt;

    &lt;cl-checkbox asp-for=&quot;Hobby3&quot; asp-title=&quot;单个复选框&quot;&gt;&lt;/cl-checkbox&gt;

    ログイン後にコピー
  3. 使用例


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

using System;

using System.Collections.Generic;

using Microsoft.AspNetCore.Mvc.Rendering;

using Microsoft.AspNetCore.Mvc.ViewFeatures;

using Microsoft.AspNetCore.Razor.TagHelpers;

 

namespace LayuiTagHelper.TagHelpers

{

 /// &lt;summary&gt;

 /// 单选框

 /// &lt;/summary&gt;

 [HtmlTargetElement(RadioTagName)]

 public class RadioTagHelper : TagHelper

 {

  private const string RadioTagName = &quot;cl-radio&quot;;

  private const string ForAttributeName = &quot;asp-for&quot;;

  private const string ItemsAttributeName = &quot;asp-items&quot;;

 

  [ViewContext]

  public ViewContext ViewContext { get; set; }

 

  [HtmlAttributeName(ForAttributeName)]

  public ModelExpression For { get; set; }

 

  [HtmlAttributeName(ItemsAttributeName)]

  public IEnumerable&lt;SelectListItem&gt; Items { get; set; }

 

  public override void Process(TagHelperContext context, TagHelperOutput output)

  {

   if (For == null)

   {

    throw new ArgumentException(&quot;必须绑定模型&quot;);

   }

   foreach (var item in Items)

   {

    var radio = new TagBuilder(&quot;input&quot;);

    radio.TagRenderMode = TagRenderMode.SelfClosing;

    radio.Attributes.Add(&quot;id&quot;, ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(For.Name));

    radio.Attributes.Add(&quot;name&quot;, ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(For.Name));

    radio.Attributes.Add(&quot;value&quot;, item.Value);

    radio.Attributes.Add(&quot;title&quot;, item.Text);

    radio.Attributes.Add(&quot;type&quot;, &quot;radio&quot;);

    if (item.Disabled)

    {

     radio.Attributes.Add(&quot;disabled&quot;, &quot;disabled&quot;);

    }

    if (item.Selected || item.Value == For.Model?.ToString())

    {

     radio.Attributes.Add(&quot;checked&quot;, &quot;checked&quot;);

    }

    output.Content.AppendHtml(radio);

   }

   output.TagName = &quot;&quot;;

  }

 }

}

ログイン後にコピー

最後に、スイッチコンポーネント

実際、単一のチェックボックスをスイッチに直接置き換えることができます。スイッチはたまたまlayuiでも利用可能です。そのため、スイッチは個別にカプセル化されており、コードは似ています

これだけです


また、使い方は簡単: &lt;cl-switch asp-for="Hobby4" asp-switch-text="Open|Close"&gt;&lt;/cl-switch&gt;<p>

1

2

3

4

5

6

7

8

9

10

@{

string sex=&quot;男&quot;;

var Items=new List&lt;SelectListItem&gt;()

   {

    new SelectListItem() { Text = &quot;男&quot;, Value = &quot;男&quot; },

    new SelectListItem() { Text = &quot;女&quot;, Value = &quot;女&quot;},

    new SelectListItem() { Text = &quot;不详&quot;, Value = &quot;不详&quot;,Disabled=true }

   };

}

&lt;cl-radio asp-items=&quot;@Items&quot; asp-for=&quot;sex&quot;&gt;&lt;/cl-radio&gt;

ログイン後にコピー

概要

梱包はまだ非常に粗雑ですが、通常の使用には問題ありません、問題が見つかった場合は、ご指摘ください。 &lt;cl-switch asp-for=&quot;Hobby4&quot; asp-switch-text=&quot;开启|关闭&quot;&gt;&lt;/cl-switch&gt;<br>

layui はページが読み込まれた直後にレンダリングされるフォームタグであるため、layui に関連するスタイルはあまりありません。

いくつかのフォームコンポーネントに加えて、実際にはタブ、タイムライン、ページング、および後で紹介するコード表示コンポーネントもカプセル化されます。

もちろん、興味のある友人は、どのようなコンポーネントが実装されているかを簡単に確認することができます

以上がLauiui コンポーネントをカプセル化する asp.net コアの詳細な例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットな記事タグ

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Lauiuiでフォームデータを取得する方法 Lauiuiでフォームデータを取得する方法 Apr 04, 2024 am 03:39 AM

Lauiuiでフォームデータを取得する方法

Lauiuiログインページでジャンプを設定する方法 Lauiuiログインページでジャンプを設定する方法 Apr 04, 2024 am 03:12 AM

Lauiuiログインページでジャンプを設定する方法

ライウイとエレメントイはどっちがいいですか? ライウイとエレメントイはどっちがいいですか? Apr 04, 2024 am 04:15 AM

ライウイとエレメントイはどっちがいいですか?

Lauiui が自己適応を実現する方法 Lauiui が自己適応を実現する方法 Apr 26, 2024 am 03:00 AM

Lauiui が自己適応を実現する方法

レイウイの実行方法 レイウイの実行方法 Apr 04, 2024 am 03:42 AM

レイウイの実行方法

Lauiui フレームワークは何語ですか? Lauiui フレームワークは何語ですか? Apr 04, 2024 am 04:39 AM

Lauiui フレームワークは何語ですか?

Lauiui フレームワークと Vue フレームワークの違い Lauiui フレームワークと Vue フレームワークの違い Apr 26, 2024 am 01:27 AM

Lauiui フレームワークと Vue フレームワークの違い

lauiとvueの違いは何ですか? lauiとvueの違いは何ですか? Apr 04, 2024 am 03:54 AM

lauiとvueの違いは何ですか?

See all articles