我为我的列表视图定义了一个div:
<div id="carrierList"></div>
这是我的脚本,它定义了listview及其数据源;
<script type="text/javascript">
$(document).ready(function () {
var t94StragglerCarriers = new kendo.data.DataSource({
transport: {
read: {
url: "/MenuTrain/T94StragglerCarriers",
dataType: "json"
},
schema: {
fields: {
id: { type: "string" },
val: { type: "string" }
}
},
pageSize: 5,
serverPaging : true
}
});
var carriers = $("#carrierList").kendoListView({
dataSource: t94StragglerCarriers,
selectable: true,
pageable : true ,
change: onChange,
template : kendo.template($("#carrierTemplate").html())
}).data("kendoListView");
function onChange() {
var index = carriers.select().index();
var item = carriers.dataSource.view()[index];
console.log("Item " + index + " selected. Guid = " + item.id);
}
});
以下是我的listview的模板定义:
<script type="text/x-kendo-tmpl" id="carrierTemplate">
<div id='carrierListVal_#=val#_#=id#' class='k-textbox'>#=val#</div>
<br/>
以下是我的控制器上的操作方法:
public JsonResult T94StragglerCarriers( [DataSourceRequest] DataSourceRequest request)
{
return Json(t94RailcarRepository.CarriersWithStragglers(), JsonRequestBehavior.AllowGet);
}
这是服务器返回的json字符串:
[{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"CHTT"},{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"CMO "},{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"CTCX"},{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"DBUX"},{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"GATX"},{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"MWCX"},{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"NDYX"},{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"PLMX"},{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"TAEX"},{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"TCIX"},{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"TEIX"},{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"TILX"},{"id":"10eac72a-d62d-434f-8505-4869cdb27b04","val":"UP "},{"id":"ffbcdb6c-4d3a-45f6-8ef6-ada5f28ba44b","val":"MDXx"}]
我的问题是,从onChange事件处理程序,当我尝试获取所选listview项的索引时,如下所示:
var index = carriers.select().index();
返回一个值,好像json字符串中的每个字段都是一个元素。例如,第一个对象的id字段将是索引0,它的val fiedl将是索引1.依此类推。任何人都可以看到我做错了什么来获取所选列表视图项的索引?或者我看不到json字符串有什么问题?