2012-09-24 180 views
4

我有两个数据表,一个列出折叠,另一个列表文件在其父文件夹中。这里是我的脚本如何查找文件夹表:如何在两个jQuery数据表之间传递参数

var oTable = $('#folderTable').dataTable({ 
      "bServerSide": true, 
      "sAjaxSource": "AJAXViewFolders", 
      "bProcessing": true, 
      "bFilter": false, 
      "aoColumns": [ 
        { "sName": "folder_id", 
         "bSearchable": false, 
         "bSortable": false, 
         "fnRender": function (oObj) { 
          return '<a href=\"ViewFiles?parentid=' + oObj.aData[0] + '\">View</a>'; 
         } 
        }, 
        { "sName": "folder_name" }, 
        { "sName": "create_date" } 
       ] 
     }); 
    }); 

现在,当用户点击我需要能够到的parentId传递到文件数据表的链接。到目前为止我没有运气。下面是JSON结果的样子在我的控制器,用于数据表文件:

public JsonResult AJAXViewFiles(DataTableParamModel dtParams, int parentid) 
    { 
     var repo = new TrusteeDocumentRepository(); 
     var allDocuments = repo.FindAllFiles().Where(c=>c.folder_id == parentid); 
     IEnumerable<Files> filteredFiles; 
     filteredFiles = allDocuments; 


     var displayedFiles = filteredFiles.Skip(dtParams.iDisplayStart).Take(dtParams.iDisplayLength); 
     var result = from c in displayedFiles select new[] { Convert.ToString(c.folder_id),c.file_name, c.upload_date.ToString() }; 

     return Json(new 
     { 
      sEcho = dtParams.sEcho, 
      iTotalRecords = allDocuments.Count(), 
      iTotalDisplayRecords = filteredFiles.Count(), 
      aaData = result 
     }, 
        JsonRequestBehavior.AllowGet); 
    } 

我将如何去获得的文件夹表的链接顺利通过的parentId到jsonresult为文件的数据表?

回答

1

我假设的DataTable是在同一页上,所以我想切换到一个按钮...

"fnRender": function (oObj) { 
    return '<button type="button" class="folder-view" data-id="' + oObj.aData[0] + '">View</button>'; 
} 

添加在线点击处理程序,以便您可以设置当前的parentId并刷新文件数据表。处理程序可能是这样的......

$('.folder-view').on('click', function() { 
    var $filesTable = $('#filesTable'); 
    $filesTable.attr('data-parentid', $(this).attr('data-id')); 
    //refresh the files table 
    $filesTable.dataTable().fnDraw(false); 
}); 

最后,文件的dataTable将需要重写fnServerData功能合并多余的parentid数据...

"fnServerData": function (sSource, aoData, fnCallback) { 

    var extraData = [ { parentid: $('#filesTable').attr('data-parentid') } ]; 

    $.ajax({ 
     "dataType": "json", 
     "type": "POST", 
     "url": sSource, 
     "data": $.merge(extraData, aoData), 
     "success": fnCallback 
    }); 
} 
+0

对不起,我应该已经指定。这些数据表在两个不同的页面上,但我认为这个解决方案可以很好地通过简单地切换AJAX url,正确吗? –

+0

正确,甚至更容易...你可以把parentid放到ajax url中。 – dotjoe

+0

真棒的幌子,谢谢你。 –

相关问题