Telerik Kendo网格从模板中获取行详细信息

时间:2018-06-12 14:06:46

标签: javascript jquery kendo-ui kendo-grid kendo-asp.net-mvc

我在.NET项目中使用带有MVC的kendo网格。我的网格的两列是模板,并且在模板内部有一个输入文本。这样,每次网格加载输入文本时,总是可见并且可以进行更改。

当用户点击save时,我需要检查网格的所有行,在客户端并获取这两列的值(只有两列有模板)并获取输入框的值。所以我期望的结果是一个包含两列的列表,用户在输入框中插入了最后一个值,而不是原始值。

这是我的代码:

//Grid Data source
var dataSource = new kendo.data.DataSource({
    transport: {
        read: { url: "/CsmForecastRegistration/GetForecast", cache: false }
    },
    error: function (e) {
        openDialog(e.errorThrown);
    },
    batch: true,
    pageSize: 20,
    schema: {
        data: "Data",
        total: "Total",
        errors: "Errors",
        model: {
            id: "ForecastItemId",
            fields: {
                ForecastItemId: { editable: false },
                RecordId: { editable: false },
                SaleId: { editable: false },
                IsUpSell: { editable: false },
                BusinessName: { editable: false },
                HealthScore: { editable: false },
                CurrencySymbol: { editable: false },
                ForecastWeekTotalContractValue: { editable: true },
                ForecastWeekMonths: { editable: true },
                ForecastWeek12Months: { editable: false }
            }
        }
    }
});


$("#grdCsmForecast").kendoGrid({
    dataSource: dataSource,
    scrollable: true,
    dataBound: onDataBound,
    toolbar: ["excel"],
    excel: {
        allPages: true,
        fileName: "CSMForecast.xlsx"
    },
    pageable: true,
    columns: [
        { title: "", width: "80px", template: $("#comments-template").html(), attributes: { style: "text-align:center; background-color: white;" } },
        {
            title: "Contract Details",
            columns: [
                { field: "RecordId", title: "RecordId", width: "90px", attributes: { "class": "contractDetailGridStyle" } },
                { field: "SaleId", title: "SaleId", width: "90px", attributes: { "class": "contractDetailGridStyle" } },
                { field: "IsUpSell", title: "Upsell?", width: "75px", attributes: { "class": "contractDetailGridStyle" } },
                { field: "BusinessName", title: "Business Name", width: "250px", attributes: { "class": "contractDetailGridStyle"} },
                { field: "HealthScore", title: "Health Score", width: "95px", attributes: { "class": "contractDetailGridStyle"} },
                { field: "CurrencySymbol", title: "CCY", width: "50px", attributes: { "class": "contractDetailGridStyle" }  }
            ]
        },
        {
            title: "Forecast Week", 
            columns: [
                { field: "ForecastWeekTotalContractValue", title: "TCV", width: "75px", template: $("#forecast-week-tcv").html(), attributes: { "class": "forecastWeekGridStyle" }, footerTemplate: "#: sum # "  },
                { field: "ForecastWeekMonths", title: "Months", width: "70px", template: $("#forecast-weekMonths").html(), attributes: { "class": "forecastWeekGridStyle" }  },
                { field: "ForecastWeek12Months", title: "12Month", width: "75px", attributes: { "class": "forecastWeekGridStyle" }, footerTemplate: "#: sum # " }
            ]
        }
    ]
});

模板:

<script id="forecast-week-tcv" type="text/x-kendo-template">

    # if(IsNewContract === true ){ #
        <span>#=ForecastWeekTotalContractValue#</span>
    #}
    else{#
        <input type="text" value="#=ForecastWeekTotalContractValue#" />
    #}#

</script>

<script id="forecast-weekMonths" type="text/x-kendo-template">

    # if(IsNewContract === true ){ #
        <span>#=ForecastWeekMonths#</span>
    #}
    else{#
        <input type="text" value="#=ForecastWeekMonths#" />
    #}#

</script>

所以我想有一个列表并向我的MVC发送控制器这两个输入的所有值:

<input type="text" value="#=ForecastWeekTotalContractValue#" />
<input type="text" value="#=ForecastWeekMonths#" />

由于

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

function getInputValues() {
    let result = [];

    $('#grid tbody tr').each((i, tr) => {
        let row = {};
        $(tr).find('input[type="text"]').each((index, input) => {
            row[(index ? "ForecastWeekTotalContractValue" : "ForecastWeekMonths")] = $(input).val();
        });
        result.push(row);
    });

    return result;
}

Demo

它只是迭代元素并添加到一个对象数组。