2013-07-12 90 views
1

請幫助解決這個非常煩人的問題。我正在使用for循環遍歷數據數組並創建多個網格。它運作良好,但過濾器不能正常發揮約束力(它只綁定到最後創建的網格)下面是代碼:將功能分配給多個slickgrids

// this function iterates over the data to build the grids 
function buildTables() { 

// "domain" contains the dataset array 
for (i = 0; i < domains.length; i++) { 
    var dataView; 
    dataView = new Slick.Data.DataView(); 
    var d = domains[i]; 
    grid = new Slick.Grid('#' + d.name, dataView, d.columns, grids.options); 
    var data = d.data; 


    // create a column filter collection for each grid - this works fine 
    var columnFilters = columnFilters[d.name]; 

    // this seems to be working just fine 
    // Chrome console confirms it is is processed when rendering the filters 
    grid.onHeaderRowCellRendered.subscribe(function (e, args) { 
     $(args.node).empty(); 
     $("<input type='text'>") 
      .data("columnId", args.column.id) 
      .val(columnFilters[args.column.id]) 
      .appendTo(args.node); 
    }); 

    // respond to changes in filter inputs 
    $(grid.getHeaderRow()).delegate(":input", "change keyup", function (e) { 
     var columnID = $(this).data("columnId"); 
     if (columnID != null) { 

      // this works fine - when the user enters text into the input - it 
      // adds the filter term to the filter obj appropriately 
      // I have tested this extensively and it works appropriately on 
      // all grids (ie each grid has a distinct columnFilters object 
      var gridID = $(this).parents('.grid').attr('id'); 
      columnFilters[gridID][columnID] = $.trim($(this).val()); 
      dataView.refresh(); 
     } 
    }); 


    //##### FAIL ##### 
    // this is where things seem to go wrong 
    // The row item always provides data from the LAST grid populated!! 
    // For example, if I have three grids, and I enter a filter term for 
    // grids 1 or 2 or 3 the row item below always belongs to grid 3!! 
    function filter(row) { 
     var gridID = $(this).parents('.grid').attr('id'); 
     for (var columnId in grids.columnFilters[gridID]) { 
      if (columnId !== undefined && columnFilters[columnId] !== "") { 
       var header = grid.getColumns()[grid.getColumnIndex(columnId)]; 
       //console.log(header.name); 
      } 
     } 
     return true; 
    } 


    grid.init(); 
    dataView.beginUpdate(); 
    dataView.setItems(data); 
    dataView.setFilter(filter); // does it matter than I only have one dataView instance? 
    dataView.endUpdate(); 
    grid.invalidate(); 
    grid.render(); 

總之,每個功能似乎是適當地結合到每個網格除過濾功能。當我在ANY網格中輸入一個過濾條件時,它只返回最後一個網格中的行。

我花了好幾個小時試圖找到錯誤,但不得不承認失敗。非常感激任何的幫助。

+0

你能創造一個小提琴哪裏這個問題可以轉載嗎? – user700284

回答

0

是的,重要的是你只有一個dataView實例。也遲早你會拿出一個事實,即所有網格的一個變量也是一個壞主意

所以加一個var dataView到你的循環,它應該解決的問題