简体   繁体   English

JavaScript-使用xml数据生成Kendo Ui网格

[英]JavaScript - Generate Kendo Ui Grid using xml Data

I've Been using XSLT to display my xml page. 我一直在使用XSLT显示我的xml页面。 I make use of the following to get the data from the xml file: 我利用以下内容从xml文件获取数据:

< xsl:value-of select="ClinicalDocument/component/structuredBody/component[3]/section/text/table/tbody"/ > <xsl:value-of select =“ ClinicalDocument / component / structuredBody / component [3] / section / text / table / tbody” />

After this, I have the following javascript to clean up the data and do the conversion: 在此之后,我将使用以下javascript清理数据并进行转换:

 -----------Get Content for Grids---------- //Split Content into array var purposeArray = document.getElementById('purposeOfVisit').innerHTML.split("\\n"); var activeProblemArray = document.getElementById('activeProblems').innerHTML.split("\\n"); //------------ Remove All Unwanted Values-----------\\\\*/ var newDataString =""; for( var k = 0; k < purposeArray.length; k++ ) { newDataString += purposeArray[k] + "__"; } newDataString = newDataString.replace(/ /g,""); newDataString = newDataString.replace(/__________/g,"__-__"); var newDataArray = newDataString.split("__"); //------------- Save Values in final Array -------------\\\\*/ var semiFinalArray = new Array(); for( var x=0; x < newDataArray.length; x++) { if(newDataArray[x].length != 0) { semiFinalArray.push(newDataArray[x]); } } var finalArray = new Array(); var counter = 0; //------------ Find Number of Columns in row ------------\\\\*/ var numberOfRows = document.getElementById('numberOfRows').innerHTML; var numberOfColumns = document.getElementById('numberOfColumns').innerHTML; var columnsPerRow = parseInt(numberOfColumns) / parseInt(numberOfRows); //------------------------------Testing ------------------------------// var dataNamePre = "dataValue"; var temporaryArray = new Array(); var dataName; //----------- Generate Grid Values -----------// for( var b=0 ; b < semiFinalArray.length ; b = b + columnsPerRow) { var problemComment = ""; counter = 0; var obj; for( var a=0 ; a < columnsPerRow ; a++) { dataName = dataNamePre + counter.toString() + ""; //-------Generate Grid Titles------// temporaryArray.push("Title " + (counter+1)); var key = "key"+a; obj = { values : semiFinalArray[b+a] }; var problemComment = ""; finalArray.push(obj); counter++; } } //---------------------Generate GridArray---------------------------// var gridArray = []; var gridArrayHead = new Array(); counter = 0; var objectValue = new Array(); for( var x = 0; x < finalArray.length; x++ ) { objectValue = { head:temporaryArray[x], values: finalArray[x].values } gridArray.push(objectValue); } var provFacilities = []; for( var x = 0; x < finalArray.length; x++ ) { provFacilities[x] = { head:temporaryArray[x], values: finalArray[x].values } } //alert(gridArray); $("#grid").kendoGrid( { columns: [{ title:gridArray.head, template:'#= values #' }], dataSource: { data:finalArray, pageSize:10 }, scrollable:false, pageable:true }); 

This may be a roundabout method, but I'm still prettry new to this method of coding. 这可能是一种回旋方法,但是对于这种编码方法,我仍然很陌生。 Currently, all the data is being presented in one column, with the last value in my temporaryArray as the title for the column. 当前,所有数据都显示在一个列中,而我的临时数组中的最后一个值作为该列的标题。

Everything works until I try to set the DataSource for the Kendo Grid. 一切正常,直到我尝试为Kendo Grid设置数据源。 When working in the columns property in the grid, I made the following change: 在网格中的columns属性中工作时,进行了以下更改:

title:gridArray[0].head 标题:gridArray [0] .head

When this is done, the title is changed to the first value in the array. 完成此操作后,标题将更改为数组中的第一个值。

What I want to know is how can I generate columns in the Kendo Grid According to the title? 我想知道的是如何根据标题在Kendo Grid中生成列? Is there a way to loop through all the values and create the objects from there, seeing that the date that is being sent to the grid are objects in an Array? 有没有一种方法可以遍历所有值并从那里创建对象,以确保发送到网格的日期是数组中的对象?

What I basically want is something to make this work, without the repitition: 我基本上想要的是使这项工作有效的方法,而无需重新定义:

var myGrid = $("#grid").kendoGrid( { columns: [ { title: temporaryArray[0], var myGrid = $(“#grid”)。kendoGrid({列:[{标题:临时数组[0],
field: finalArray[0].values }, { title: temporaryArray[1], 字段:finalArray [0] .values},{标题:临时数组[1],
field: finalArray[1].values }, { title: temporaryArray[2], 字段:finalArray [1] .values},{标题:temporaryArray [2],
field: finalArray[2].values }, { title: temporaryArray[3], 字段:finalArray [2] .values},{标题:temporaryArray [3],
field: finalArray[3].values }, { title: temporaryArray[4], 字段:finalArray [3] .values},{标题:temporaryArray [4],
field: finalArray[4].values } ] )}; field:finalArray [4] .values}])};

Any help appreciated, thanks! 任何帮助表示赞赏,谢谢!

This issue has been fixed using the following coding: 使用以下编码已解决此问题:

var arrayData = [];

for( var x = 0; x < semiFinalArray.length; x=x+5 )
{
    var tempArr = new Array();

    for( var y = 0; y < 5; y++ )
    {
        var num = x + y;
        tempArr.push(semiFinalArray[num]);
    }
    arrayData.push(tempArr);
}

var dataTitles = [];
for( var x = 0; x < titleArray.length; x++ )
{
    var head = "";
    head = titleArray[x];
    head = head.replace(/ /g,"");
    dataTitles.push(head);
}


var counter = 0;
var columnDefs = [];
for (var i = 0; i < columnsPerRow.length; i++) 
{
    if (counter == (columnsPerRow - 1)) 
    {
        counter = 0;
    }
    columnDefs.push({ field: dataTitles[counter], template: arrayData[i].values });
    counter++;
}
// Create final version of grid array
var gridArray = [];
for (var i = 0; i < arrayData.length; i++) 
{
    var data = {};
    for (var j = 0; j < dataTitles.length; j++) 
    {
        data[dataTitles[j]] = arrayData[i][j];
    }
    gridArray.push(data);
}
// Now, create the grid using columnDefs as argument
$("#grid").kendoGrid(
{
    dataSource: 
    {
        data: gridArray,
        pageSize: 10
    },
    columns: columnDefs,
    scrollable: false,
    pageable: true
}).data("kendoGrid");

With this, the data is displayed in the DataGrid. 这样,数据将显示在DataGrid中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM