【问题标题】:I can't read data from ajax with jstree我无法使用 jstree 从 ajax 读取数据
【发布时间】:2021-11-13 00:11:05
【问题描述】:

我正在使用 Ajax 将数据从数据库发送到页面。我想用 jsTree 显示这些数据。文件夹的数量与我发送的数据数量一样多。但它没有显示他们的名字。如何使用 jsTree 正确显示数据?

我的 JavaScript 代码:

$(function () {
        $.ajax({
            type: "GET",
            url: "/Home/anaKategori",
            success: function (data) {
                
                console.log(data);
                createJSTrees(data);
               
            },

            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });
    });

    function createJSTrees(json) {
        $('#jstree').jstree({
            'core': {
                'data': json
            },
            "plugins": ["themes", "data", "ui"]
        });
    }

我的 C# 代码(.net core 5.0):

public List<tumKategoriler> anaKategori()
        //public string anaKategori()
        //public IActionResult anaKategori()

        {
            foreach (var item in context.urunKategorileri.ToList())
            {
                if (context.urunKategorileri.Any(x=>x.urunKategorileriUstId == item.urunKategorileriId))
                {
                    List<altKategoriler> p = new List<altKategoriler>();


                    foreach (var altItem in context.urunKategorileri.Where(x => x.urunKategorileriUstId == item.urunKategorileriId))
                    {
                        altKategoriler e = new altKategoriler
                        {
                            ad = altItem.urunKategorileriKategoriAdi
                        };
                        p.Add(e);
                    }

                    tumKategoriler i = new tumKategoriler
                    {
                        ustKategoriAdi = item.urunKategorileriKategoriAdi,
                        AltKategoriAdlari = p
                    };
                    json.Add(i);
                   
                }
                else
                {
                    tumKategoriler o = new tumKategoriler
                    {
                        ustKategoriAdi = item.urunKategorileriKategoriAdi
                    };
                    json.Add(o);

                }
               
            }

            return json;
            //return JsonConvert.SerializeObject(json);
            //return Json(new { jsonvar = JsonConvert.SerializeObject(json) });
        }

【问题讨论】:

    标签: javascript c# json asp.net-core jstree


    【解决方案1】:

    你的数据结构可能有问题。你可以尝试在anaKategori中返回List&lt;JsTreeModel&gt;

    public class JsTreeModel
        {
            public string Id { get; set; }
            public string Text { get; set; }
            public string Parent { get; set; } = "#";
    
        }
    

    如果没有父母,Parent="#"。如果有父母,设置父母的ID。到父属性。

    这里是示例数据:

    [{
                            id: "padre1",
                            parent: "#",
                            text: "Padre 1",
                        }, {
                            id: "padre2",
                            parent: "#",
                            text: "Padre 2"
                        }, {
                            id: "id3",
                            parent: "padre1",
                            text: "Figlio 1 di padre 1"
                        }, {
                            id: "id4",
                            parent: "padre1",
                            text: "Figlio 2 di padre 1"
                        }, {
                            id: "id5",
                            parent: "padre2",
                            text: "Figlio 1 di padre 2"
                        }, {
                            id: "id6",
                            parent: "id5",
                            text: "Figlio 1 di figlio 1 di padre 2"
                        }, {
                            id: "id7",
                            parent: "id5",
                            text: "Figlio 2 di figlio 1 di padre 2"
                        }, {
                            id: "id8",
                            parent: "id5",
                            text: "Figlio 3 di figlio 1 di padre 2"
                        }, {
                            id: "id9",
                            parent: "#",
                            text: "Figlio 3 di figlio 1 di padre 2"
                        }]
    

    结果:

    【讨论】:

      【解决方案2】:

      我是你错过了文件类型的 ajax 代码的 url。 ************************************你的ajax

      $.ajax({
                  type: "GET",
                  url: "/Home/anaKategori",
                  success: function (data) {
                  .
                  .
                  . 

      **********************************尝试在url中给出文件的扩展名

      $.ajax({
                      url : 'test.php', //like this
                      type : 'post',
                      data : {'data':'1'},
                      .
                      .
                      .

      希望您通过此修复得到结果,谢谢

      【讨论】:

      • 嗨。我从方法中提取数据,而不是从页面中提取数据。我在地址处提供的页面不存在。我正在使用.net 核心。我还在问题部分分享了我的后端代码。你可以看看。
      猜你喜欢
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多