【发布时间】:2019-01-11 01:50:02
【问题描述】:
我使用 C# 函数 (File.aspx.cs) 从外部源获取一些值,并将该值用作数据表上的 custom attributes。然而,我只得到 0,1,2 等作为 ID 值,而不是 C# dataID 变量的实际值,尽管我已经在我的 JS 文件上使用了var ID = '<%=dataID%>';(没有引号我得到错误(JS)表达式预期)。
如何在我的 dt.file.js 上获取这个 dataID 值?
文件.aspx.cs
public partial class Example : System.Web.UI.Page
{
public static int dataID;
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static DataTableResponse GetList(string parameters)
{
DataTableAjaxPostModel model = JsonConvert.DeserializeObject<DataTableAjaxPostModel>(parameters);
List<FieldAgentRow> list = new List<FieldAgentRow>();
XmlDocument doc = Utilities.FileToXmlDocument("data");
foreach (XmlNode person in doc.SelectNodes("//Personal"))
{
FieldAgentRow tmp = new FieldAgentRow();
//other codes
tmp.data_id = Int32.Parse(person.SelectSingleNode("ID").InnerText);
dataID = tmp.data_id;
list.Add(tmp);
}
DataTableResponse res = new DataTableResponse();
res.draw = model.draw;
res.data = list;
return res;
}
}
dt.file.js
$(document).ready(function () {
var ID = '<%=dataID%>';
var table = $('#list').DataTable({
"createdRow": function (row, data, ID ) {
$(row).attr('data-id', ID);
}
//other codes
});
更新解决方案: 感谢Paul's answer,我可以通过修改我的JS代码来获取data_id值:
$(document).ready(function () {
var table = $('#list').DataTable({
"createdRow": function (row, data, dataindex) {
$(row).attr('data-id', data.data_id);
}
//other codes
});
【问题讨论】:
-
是我还是你的
GetList没有return什么? -
@Rafalon 我添加了缺失的部分。但这不是有趣的部分,因为我只想关注变量
-
您需要使用 data 属性来获取 createdRow 方法 datatables.net/reference/option/createdRow 中行的 json。您应该能够执行 data.data_id (假设在序列化期间未更改属性名称)。您也可以在那里执行 console.log(data) 并在浏览器开发工具 (f12) 中检查它以查看可用的属性。
-
@PaulZepernick 它有效!这实际上比我想象的要简单得多。您可以将此作为答案发布,以便我接受吗?
-
顺便说一句,如果您只是想为该行分配一个唯一的 id,您可以在您的行中发送 json 属性 DT_RowId。如果您使用的是 DT 1.10.8 > 似乎有一个选项可以更改默认名称 datatables.net/reference/option/rowId。
标签: javascript c# datatables