Javascript - 如何动态地将对象添加到剑道 ui 网格中的列数组? [英] Javascript - How to add objects to column array in kendo ui grid dynamically?

查看:15
本文介绍了Javascript - 如何动态地将对象添加到剑道 ui 网格中的列数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照标题所暗示的那样在列数组中生成对象,尽管我还没有找到可行的方法.

I am trying to generate objects in the columns array as the heading implies, although I haven't found a working method.

alert( "Value 1: " + temporaryArray[1] + " - " + finalArray[1].values ); 
alert( "Value 2: " + temporaryArray[2] + " - " + finalArray[2].values ); 
var myGrid =  $("#grid").kendoGrid( 
{
    columns: 
    [
        {
            title: temporaryArray[0] + " ",
            field: gridArray[0].values + " "
        }
    ],
    dataSource: 
    {
        data:finalArray,
        pageSize:10
    },
    scrollable:false,
    pageable:true 
});

我尝试了以下方法来添加对象:

I've tried the following to add the object:

for( var x = 0; x < finalArray.length; x++ )
{
    myGrid[columns] = { temporaryArray[x]:finalArray[x] };
}

for( var x = 0; x < finalArray.length; x++ )
{
    myGrid.columns[values]= finalArray[x].values;
}

没有成功...

以下看起来像我想动态实现的对象内部的对象数组:

The following looks like an array of objects inside of the object which I want to achieve dynamically:

columns: 
[
    {
        title: temporaryArray[0] + " ",
        field: gridArray[0].values + " "
    },
    {
        title: temporaryArray[1] + " ",
        field: gridArray[1].values + " "
    },
    {
        title: temporaryArray[2] + " ",
        field: gridArray[2].values + " "
    }
],

例如:

for( var x = 0; x < finalArray.length; x++ )
{
    myGrid[columns] = { temporaryArray[x]:finalArray[x] };
}

我想使用 for 循环生成对象以生成列数组内的对象数组.

我想知道的是,这是否可以动态执行?或者根本没有硬编码就可能?

What I want to know is, whether this is possible to do dynamically? or just possible at all without hard coding it?

推荐答案

你可以做到.让标题存储在 titleDefs 中,field 名称存储在 fieldDef 中.那么你应该这样做:

You can do it. Lets have the Titles stored in titleDefs and the field name in fieldDef. Then you should do:

// Title Definitions
var titleDefs = [
    "First Name", "Last Name", "Title"
];
// Field Definition
var fieldDefs = [
    "FirstName", "LastName", "Title"
];
// Lets compute column definition
var columnDefs = [];
for (var i = 0; i < titleDefs.length; i++) {
    columnDefs.push({ title : titleDefs[i], field: fieldDefs[i] });
}
// Now, create the grid using columnDefs as argument
var grid = $("#grid").kendoGrid({
    dataSource: {
        data    : dataArray,
        pageSize: 10
    },
    columns   : columnDefs
}).data("kendoGrid");

注意:在本例中,我定义了一个数据源,它是内存中的 JavaScript array,但您可以从更改 DataSource 的服务器获取数据 定义.

NOTE: In this example I've defined a DataSource that is a JavaScript array in memory but you can get the data from a server changing the DataSource definition.

注意:在您的代码中,您在 title 定义中添加了额外的空格,这是不正确的:列定义是 JavaScript 代码而不是 字符串 这样您就不必像要显示的那样对其进行格式化.

NOTE: In your code, you were adding extra white space to the title definition and that is not correct: column definitions are JavaScript code and not strings so you don't have to format it as you were going to display it.

这篇关于Javascript - 如何动态地将对象添加到剑道 ui 网格中的列数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆