我一直在寻找最近几个小时,不幸的是我似乎找不到一个如何用动作编辑填充数据表并使用.net和MVC删除链接列的示例.
- <script type="text/javascript">
- $(document).ready(function () {
- $('#myDataTable').dataTable({
- bProcessing: true,sAjaxSource: '@Url.Action("Index1","Default1")'
- });
- });
- </script>
- <div id="container">
- <div id="demo">
- <table id="myDataTable">
- <thead>
- <tr>
- <th>
- RoleId
- </th>
- <th>
- RoleName
- </th>
- <th>
- UserId
- </th>
- <th>
- UserName
- </th>
- </tr>
- </thead>
- <tbody>
- </tbody>
- </table>
- </div>
- </div>
我想在最后一栏中添加这个;
- <td>
- @Html.ActionLink("Edit","Edit",new {id=item.PrimaryKey}) |
- @Html.ActionLink("Details","Details",new { id=item.PrimaryKey }) |
- @Html.ActionLink("Delete","Delete",new { id=item.PrimaryKey })
- </td>
但无法弄清楚该怎么做.
解决方法
您可以使用带有fnRender功能的aoColumns属性来添加自定义列.
您不能使用Html.ActionLink助手,因为您必须从javascript动态生成链接. aoColumns属性可以帮助您配置每个列,如果您不想配置特定列,只需传递null,否则您必须传递一个对象({}).
您不能使用Html.ActionLink助手,因为您必须从javascript动态生成链接. aoColumns属性可以帮助您配置每个列,如果您不想配置特定列,只需传递null,否则您必须传递一个对象({}).
fnRender函数可帮助您使用其他列的值创建链接.您可以使用oObj.aData获取另一列的值(如id)以生成链接.
- <script type="text/javascript">
- $(document).ready(function () {
- $('#myDataTable').dataTable({
- bProcessing: true,"Default1")',aoColumns: [
- null,// first column (RoleId)
- null,// second column (RoleName)
- null,// third (UserId)
- null,// fourth (UserName)
- { // fifth column (Edit link)
- "sName": "RoleId","bSearchable": false,"bSortable": false,"fnRender": function (oObj)
- {
- // oObj.aData[0] returns the RoleId
- return "<a href='/Edit?id="
- + oObj.aData[0] + "'>Edit</a>";
- }
- },{ },// repeat the samething for the details link
- { } // repeat the samething for the delete link as well
- ]
- });
- });
- </script>
从服务器返回的JSON输出中的另一个重要事项,对于编辑列,您还必须返回1,2,3或其他任何内容.
参考:http://jquery-datatables-editable.googlecode.com/svn/trunk/ajax-inlinebuttons.html