将层次结构数据从XML文件映射到Kendo UI数据源

伊德里斯·汗

我有一个带有不同子节点的xml文件(这里是假的,但真正的是相同的结构),我想获取它并将其绑定到listview。我试了一下,但是我无法访问数据,我找到了一个合适的例子。

<cars type="metadata">
  <categories>
    <category name="BMW" />
    <category name="Honda" />
    .....
  </categories>
  <manufacturers>
    <names>
      <name alias="BMW" />
    </names>
  </manufacturers>
</cars>
........
//another different noeds

这是我的数据源代码

    var dataSource = new kendo.data.DataSource({
        transport: {
            // specify the XML file to read. The same as read: { url: "books.xml" }
            //read: "http://demos.telerik.com/kendo-ui/content/web/datasource/books.xml"
            read: "content/quran-data.xml",
            dataType: "xml"
        },
        schema: {
            type: "xml",
            data: "/cars",
            model: {
                fields: {
                    children: "categories"
                    //children: {
                    //    schema: {
                    //        data : "category",
                    //        model: {
                    //            fields : {
                    //                name: "@name",
                    //            },
                    //            hasChildren: "category"
                    //        }
                    //    }
                    //}
                },
                hasChildren: "true"
            }
        },
        change: onChage
    });

我尝试了不同的方法,但未能成功。我也知道Kendo中的HierarchicalDataSource,但是我不确定哪个示例适合它。我只想访问子节点,即类别,制造商及其嵌套节点

朱利安

我了解您的困境。正如评论中所讨论的,我的建议是将每种实体类型(例如“汽车”,“制造商”,“类别”)的关注点分离为单独的数据源。我将解释进一步的方式和原因。

至于剑道的HierarchicalDataSource ; 这就要求数据本质上是同质的。意思是,它总是期望相同类型的对象。这意味着出于您的目的,考虑到您正在组合不同的对象(或者在架构/模型中具有奇怪层次结构的地狱),它可能无法正常工作。

但是,如果您考虑在注释中提到的替代方案针对每个对象/域类型使用数据源,则实际上可以很好地解决问题。

考虑到这一点,我在考虑以下内容:

// Define the 'Categories' datasource
var categories = new kendo.data.DataSource({
    transport: {
        read: "content/quran-data.xml",
        dataType: "xml"
    },
    schema: {
        // specify the the schema is XML
        type: "xml",
        // the XML element which represents a single data record
        data: "/cars/categories/category",
        // define the model - the object which will represent a single data record
        model: {
            // configure the fields of the object
            fields: {
                // the "name" field is mapped to the text of the "name" XML element
                name: "name/text()"
            }
        }
    }
});

// Define the 'Manufacturers' datasource
var manufacturers = new kendo.data.DataSource({
    transport: {
        read: "content/quran-data.xml",
        dataType: "xml"
    },
    schema: {
        // specify the the schema is XML
        type: "xml",
        // the XML element which represents a single data record
        data: "/cars/manufacturers/names/name",
        // define the model - the object which will represent a single data record
        model: {
            // configure the fields of the object
            fields: {
                // the "alias" field is mapped to the text of the "alias" XML element
                alias: "alias/text()"
            }
        }
    }
});

这些定义可以在全局某处进行设置(我喜欢将它们分别放置在诸如datasources.js之类的约定中)的方法,同时您可以在应用程序中的任何位置轻松地对其进行初始化,如下所示:

// Fetch both, by default always async
categories.read();
manufacturers.read();

注意:当然,这也可以通过HierarchicalDataSource来完成。schema如果可以一对一地映射和重用子代的属性,这将使您更加清晰。

除了分离数据源既更干净又易于维护这一事实之外,它与加载一个大数据文件相比,不会对性能造成更大的影响。特别是如果您考虑以下主要优点(在性能和可重用性方面),则不会:

  • 单个数据源可以异步加载,从而减少了负载
  • 单个数据源可以“按需”加载;如果您只需要一个小节点,则无需加载整个文档
  • 各个数据源可以分别使用分页和缓存。如果您的列表很大,或者只想加载一次(例如,仅在deviceReady之后读取xml,然后从中获取数据源),则是理想的选择
  • 将多个数据源(例如,汽车类型和制造商)组合成一个新的特定数据源很容易。考虑到它已经在内存中了,这将在性能上花很少的钱

只是我的想法,但希望能有所帮助;)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章