2013-12-24 3 views
0

Slick Grid 및 DataView를 사용하여이 예제에서와 같이 열 합계를 계산하려고합니다 : http://mleibman.github.io/SlickGrid/examples/example-grouping. 그러나 필자는 행을 그룹화하지 않으므로 getter 및 formatter를 dataView.setGrouping (..) 메소드에 전달하지 않으려 고했지만 테이블에 'undefined'라는 텍스트가있는 그룹화 행이 표시됩니다. 그것은 내 합계를 정확하게 계산합니다. 불필요한 그룹화 행을 제거하려면 어떻게합니까? SlickGrid 및 DataView를 사용하여 그룹화하지 않고 총계를 계산하는 방법은 무엇입니까?

내가 노력하고있어입니다 :

dataView.setGrouping({ 
    aggregators: [ 
     new Slick.Data.Aggregators.Sum('someField1'), 
     new Slick.Data.Aggregators.Sum('someField2') 
    ] 
}); 
+0

열 옵션의 코드를 다음과 같이 넣을 수 있습니까? 글쎄, 내가 본 후에 너를 도울 수있을거야 ... 그리고 또한 그룹화를 유발하는 코드 – ghiscoding

+0

전체 데이터 세트의 합계를 얻으려고합니까? – ghiscoding

답변

2

그래서 질문은 "어떻게 그룹화하지 않고 합계를 계산하기 위해"나는 질문은 오래 알고 있지만 나 자신을해야했다 아무것도 찾을 수 없기 때문에 일하다, 나는 그것에 약간의 시간을 보내기로 결정했다. 그리고 지금 나는 나의 솔루션을 공유하고있다. SlickGrid의 코드는 그룹화 후에 합계를 계산합니다. 즉, 내가 생각하기에 Aggregators가 무엇인지는 모르겠지만 DataView 코드를 파고 솔루션을 생각해 냈습니다. 몇 줄의 코드이지만 훌륭하게 작동하고 Slick Aggregators를 사용합니다. 이미 알고 있고 이미 정의되어 있습니다. DataView에서 2 개의 함수를 가져 왔고 일부는 비공개이므로 코드에서 다시 복사해야하는 이유가 있습니다. 따라서 마지막 코드는 내부적으로 2 개의 다른 함수를 호출하는 1 함수 이름 CalculateTotalByAggregator()입니다.

참고 : dataview.getItems()에주의하십시오. dataview은 코드에서 오는 것이어야하며 다르게 입력해야합니다.

/** Calculate the total of an existing field from the datagrid 
* @param object aggregator 
* @return mixed total 
*/ 
function CalculateTotalByAggregator(agg) { 
    var constructorName = agg.constructor.name; // get constructor name, ex.: SumAggregator 
    var type = constructorName.replace(/aggregator/gi, '').toLowerCase(); // remove the word Aggregator and make it lower case, ex.: SumAggregator -> sum 

    var totals = new Slick.GroupTotals(); 
    var fn = compileAccumulatorLoop(agg); 
    fn.call(agg, dataview.getItems()); 
    agg.storeResult(totals); 

    return totals[type][agg.field_]; 
} 

/** This function comes from SlickGrid DataView but was slightly adapted for our usage */ 
function compileAccumulatorLoop(aggregator) { 
    aggregator.init(); 
    var accumulatorInfo = getFunctionInfo(aggregator.accumulate); 
    var fn = new Function(
     "_items", 
     " for (var " + accumulatorInfo.params[0] + ", _i=0, _il=_items.length; _i<_il; _i++) {" + 
      accumulatorInfo.params[0] + " = _items[_i]; " + 
      accumulatorInfo.body + 
     "}" 
); 
    fn.displayName = "compiledAccumulatorLoop"; 
    return fn; 
} 

/** This function comes from Slick DataView, but since it's a private function, you will need to copy it in your own code */ 
function getFunctionInfo(fn) { 
    var fnRegex = /^function[^(]*\(([^)]*)\)\s*{([\s\S]*)}$/; 
    var matches = fn.toString().match(fnRegex); 
    return { 
    params: matches[1].split(","), 
    body: matches[2] 
    }; 
} 

그런 다음 클라이언트 측에서, 당신은 단지 CalculateTotalByAggregator(agg) 기능, 예를 들어 같은 유효한 Slick.Aggregators 객체 호출해야합니다 : 그것은 몇 줄의 코드입니다

// use any valid Slick Aggregator object and pass it to the function 
var agg = new Slick.Data.Aggregators.Sum("Hours"); 
var totalHours = gridObject.CalculateTotalByAggregator(agg); 

// or on a 1 liner 
var totalHours = gridObject.CalculateTotalByAggregator(new Slick.Data.Aggregators.Sum("Hours")); 

을하지만,이 방법이 합계를 수행하는 함수를 다시 작성할 필요가 없으며, 평균을 수행하는 등 ... 당신은 이미 사용하고있는 Slick Aggregators를 사용합니다. 간단합니다 :)