title: mvc.jquery.datatables how to set up export to csv/pdf
body: <p>I have looked through datatables.net documentation as well as the docs on the nuget package (mvc.jquery.datatables) and I cannot seem to find how to get the exporting button group set up like in <a href="http://mvcjquerydatatables.apphb.com/" rel="nofollow">this example</a>.</p>
  
  <p>I have my code set up like that example, and it is working fine. but when I try to add the examples from datatables.net, I get errors saying "cannot reinitialize datatable".</p>
  
  <p>Does anyone have a source for documentation on the package and how I can set up exporting? Or do I need to go about a manual route and set up the export myself?</p>
  
  <p>Here is the code:</p>
  
  <p>Controller:</p>
  
  <pre><code> public DataTablesResult&lt;AdminUserListViewModel&gt; GetUsers(DataTablesParam dataTableParam)
      {
          return DataTablesResult.Create(Ctx.ApplicationUser.Select(user =&gt; new AdminUserListViewModel()
          {
              Id = user.UserId,
              LastName = user.Applicant.PersonalInformation.LastName,
              FirstName = user.Applicant.PersonalInformation.FirstName,
              Email = user.Applicant.ContactInformation.Email,
              ProgCode = user.Applicant.AcceptedProgressInformation.ProgramCode,
              RepName = user.Applicant.AcceptedProgressInformation.RepName
          }), dataTableParam,
          uv =&gt; new
          {
              //Name = "&lt;b&gt;" + uv.Name + "&lt;/b&gt;",
              //Hired = uv.Hired == null ? "&amp;lt;pending&amp;gt;" : uv.Hired.Value.ToShortDateString() + " (" + FriendlyDateHelper.GetPrettyDate(uv.Hired.Value) + ") "
          });
      }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@using Accepted.Web.Areas.Admin.Controllers
  @using Mvc.JQuery.Datatables
  @using Mvc.JQuery.Datatables.Serialization
  @{
  ViewBag.Title = "USERS";
  Layout = "~/Views/Shared/_Layout.cshtml";
  
  var vm = Html.DataTableVm("table-id", (HomeController h) =&gt; h.GetUsers(null));
  
  vm.JsOptions.Add("fnCreatedRow", new Raw(@"function( nRow, aData, iDataIndex ) {
      $(nRow).attr('data-id', aData[0]);
    }
  }"));
  vm.FilterOn("Id").NumberRange();
  vm.StateSave = true;
  }
  &lt;div class="page-container"&gt;
  &lt;div class="page-container-nav"&gt;
      &lt;ol class="breadcrumb"&gt;
          &lt;li class="active"&gt;@ViewBag.Title&lt;/li&gt;
      &lt;/ol&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-sm-4"&gt;
              &lt;h1&gt;@ViewBag.Title&lt;/h1&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="page-container-body container-gradient"&gt;
      &lt;div class="row"&gt;
          &lt;h2 class="thickThinHeader"&gt;
              &lt;small&gt;All&lt;/small&gt;
              &lt;br /&gt;
              Users
          &lt;/h2&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
  
      &lt;/div&gt;
      &lt;div class="row"&gt;
          &lt;hr /&gt;
          @Html.Partial("DataTable", vm)
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  
  @section scripts{
  &lt;link type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css" rel="stylesheet" /&gt;
  &lt;script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      //$(document).ready(function () {
      //    $('#table-id').dataTable({
      //        "sDom": 'T&lt;"clear"&gt;lfrtip',
      //        "oTableTools": {
      //            "sSwfPath": "copy_csv_xls_pdf.swf"
      //        }
      //    });
      //});
  
      $('#table-id').delegate('tbody &gt; tr', 'click', function() {
          var id = this.firstElementChild.innerHTML;
          var url = '@Url.Action("User", "Home", new {area = "Admin", id = "__id__"})';
          window.location.href = url.replace('__id__', id);
      });
  &lt;/script&gt;
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Simpler code:</p>
  
  <pre><code>@model IEnumerable&lt;Accepted.Web.ViewModels.AdminUserListViewModel&gt;
  @{
  ViewBag.Title = "USERS";
  Layout = "~/Views/Shared/_Layout.cshtml";
  }
  &lt;div class="page-container"&gt;
  &lt;div class="page-container-body container-gradient"&gt;
      &lt;div class="row"&gt;
          &lt;table class="display table table-condensed table-hover" id="table-id"&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;First Name&lt;/th&gt;
                      &lt;th&gt;Last Name&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                  @foreach (var obj in Model)
                  {
                      &lt;tr&gt;
                          &lt;td&gt;
                              @obj.FirstName
                          &lt;/td&gt;
                          &lt;td&gt;
                              @obj.LastName
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  }
              &lt;/tbody&gt;
          &lt;/table&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  
  @section scripts{
  &lt;link type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css" rel="stylesheet" /&gt;
  &lt;script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      $(function () {
          $('#table-id').dataTable({
              "bPaginate": true,
              "sPaginationType": "full_numbers",
              "bLengthChange": true,
              "aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
              "iDisplayLength": 25,
              "aSorting": [1, 'asc'],
              "oLanguage": {
                  "sLengthMenu": "&lt;span class='lenghtMenu'&gt; _MENU_&lt;/span&gt;&lt;span class='lengthLabel'&gt;Page Size:&lt;/span&gt;",
              },
              "sDom": '&lt;"table_top"fl&lt;"clear"&gt;&gt;,&lt;"tbl_tools"C&lt;"clear"&gt;&gt;,&lt;"table_content"t&gt;,&lt;"table_bottom"iTp&lt;"clear"&gt;&gt;',
              "oTableTools": {
                  "sSwfPath": "Areas/Admin/media/swf/copy_csv_xls_pdf.swf",
                  "aButtons": ["copy", "csv", "pdf", "print"]
              }
          });
      });
      $('#table-id').delegate('tbody &gt; tr', 'click', function () {
          var id = this.firstElementChild.innerHTML;
          var url = '@Url.Action("User", "Home", new { area = "Admin", id = "__id__" })';
          window.location.href = url.replace('__id__', id);
      });
  &lt;/script&gt;
  }
  </code></pre>