在DataTable中渲染HTML内容
我正在 asp.net core razor 页面中使用 js 在 datatable
中加载服务器响应。由于数据/UI 很复杂,我需要根据每个表格单元格中的当前值呈现不同的布局。
Datatable 支持每个单元格的 renderer
函数,所以我可以有类似的东西:
...
"data":"somefield",
"renderer":function(data,type,row,meta){
if (data.someId)=="someValue"{
return "<div... some label with somValue</div>"
}else{
return "<div... some label without value</div>"
}
}
}
这工作得很好,但是当 div
的样式和许多标签变得复杂时,它变得更难维护或改变。
我确实研究了一点 Razor 的 PartialViews
,因为它看起来是一个不错的选择。将 UI 放在 cshtml
文件中,能够从父 @Model
传递参数,并在其中使用 C# 根据收到的参数进行渲染。
虽然我可以使用
或 @Html.Partial(...)
在父页面中加载部分视图无法使用 $.get
在 js 中获取其内容并将其返回到数据表的渲染函数中。在这种情况下异步可能不起作用?还是会太慢?
我的问题是:处理这种情况更好的方法是什么?也许片面的观点不是正确的方法。我正在寻找一种轻松维护/更改单元格内容的方法。谢谢您的宝贵时间。
I am loading server response in a datatable
using js in a asp.net core razor page. Because the data/UI is complex, I need to render different layouts based on current value in each table cell.
Datatable supports a renderer
function for each cell, so I could have something like:
...
"data":"somefield",
"renderer":function(data,type,row,meta){
if (data.someId)=="someValue"{
return "<div... some label with somValue</div>"
}else{
return "<div... some label without value</div>"
}
}
}
This works perfectly fine, however when divs
get complex with style and many labels it becomes harder to maintain or change.
I did look a bit into Razor's PartialViews
as it may seem like a good alternative. Having the UI in a cshtml
file, being able to pass parameters from parent @Model
and using c# in it to render it based on the parameter received.
While I am able to load the partial view in the parent page, using <partial name=''/>
or @Html.Partial(...)
I didn't manage to get it's content in js using $.get
and return it in the datatable's render function. Probably async wouldn't work in this case? Or it would be too slow?
My question is: what would be a better way to handle this situation? Maybe partial views are not the way. I am looking for a way of easily maintaining/changing the cell content. Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将在此处留下此答案,因为也可以通过RowCallback函数渲染HTML元素。我发现RowCallback功能比列渲染函数更容易使用。但是,如果您不正确地执行操作,它将仅将HTML元素显示为字符串。这实际上将在表列中渲染HTML元素:
注意:上面代码中的“ 3”是基于零的列号。
I'm going to leave this answer here because it's also possible to render an HTML element through the rowcallback function. I found the rowcallback function to be a little easier to work with than the column render function. However, if you don't do it correctly, it will just display the html element as a string. This will actually render an html element in a table column:
Note: The "3" in the above code is the zero-based column number.
我不太熟悉
asp.net
但我会尽力回答你的问题。我认为
PartialViews
不会按照您建议的方式工作,因为它们似乎是服务器端代码,并尝试对中的每一行执行GET
请求您的表可能会生成大量服务器请求。我认为你有几个潜在的解决方案。首先是循环访问服务器上的数据,并为每个符合条件的属性生成部分视图并将其分配给该属性。然后返回数据数组,其中每行中的一个属性是 HTML 块。正如我所说,我没有使用这种语言的经验,因此我很难提供代码示例,但我希望您能理解我想说的内容。然后在 DataTables 中,您只需要输出值即可。
第二个选项是使用 JavaScript 在客户端上生成 HTML。既然你说它可能很复杂,那么最好有一个返回 HTML 字符串的函数。如果它是大量 HTML,那么您甚至可以将此函数放入单独的文件中并将其导出,以使其更易于管理。您的示例中有几个拼写错误,因此我将在这里修复它们。
Renderer
是一个表属性,您想要的是 columns.render< /a>.同样,在render
函数中,data
参数引用上面一行中定义的data
属性。如果您想引用不同的属性,请使用row
参数。I'm not really familiar with
asp.net
but I will try to answer your question.I don't think
PartialViews
are going to work in the way you suggest because they appear to be server-side code, and trying to do aGET
request for every line in your table could potentially generate a large number of server requests.I think you have a couple of potential solutions. First is to loop through your data on the server, and for each property that matches the condition generate the partial view and assign it to the property. Then return your data array with one of the properties in each row being a chunk of HTML. As I said, I don't have experience with this language so it's hard for me to provide a code example, but I hope you understand what I'm trying to say. Then in DataTables you just need to output the value
THe second option is to generate the HTML on the client using JavaScript. Since you say that it could be complex it's best if you have a function that returns a HTML string. If it's a large amount of HTML then you could even put this function in a separate file and export it, to make it more manageable. There are a couple of typos in your example, so I'm going to fix them here.
Renderer
is a table property, the one you want is columns.render. Also in therender
function thedata
argument references thedata
property that is defined in the line above. If you want to reference a different property, use therow
argument.