我使用 C# 函數 (File.aspx.cs) 從外部源獲取一些值,并將此值用作數據表上的自定義屬性。然而,我只得到 0,1,2 等作為 ID 值,而不是來自 C# dataID 變量的實際值,即使我已經var ID = '<%=dataID%>';在我的 JS 文件中使用(沒有引號,我得到了錯誤(JS)表達式)。如何在我的 dt.file.js 上獲取此 dataID 值?文件.aspx.cspublic 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; }}文件.js$(document).ready(function () { var ID = '<%=dataID%>'; var table = $('#list').DataTable({ "createdRow": function (row, data, ID ) { $(row).attr('data-id', ID); } //other codes });更新解決方案: 感謝Paul 的回答,我能夠通過修改我的 JS 代碼來獲取 data_id 值:$(document).ready(function () { var table = $('#list').DataTable({ "createdRow": function (row, data, dataindex) { $(row).attr('data-id', data.data_id); } //other codes });
1 回答

斯蒂芬大帝
TA貢獻1827條經驗 獲得超8個贊
您需要使用 data 屬性來獲取 createdRow 方法https://datatables.net/reference/option/createdRow中行的 json 。您應該能夠執行 data.data_id (假設在序列化期間未更改屬性名稱)。
您也可以在那里執行 console.log(data) 并在瀏覽器開發工具 (f12) 中檢查它以查看哪些屬性可用。
- 1 回答
- 0 關注
- 260 瀏覽
添加回答
舉報
0/150
提交
取消