2017-10-10 1 views
-3

저는 datatables를 사용하고 있으며 테이블의 여러 필드의 표시 가능한 길이를 제한해야합니다. 편집보기에서는 여전히 긴 문자열을 처리 할 수 ​​있어야합니다. 너무 많은 노력을하지 않아도 가능할 것으로 기대합니다.Grails, 테이블의 필드 길이를 어떻게 제한합니까?

   var table = $('#gridProducts').DataTable({ 
       "scrollY": "600px", 
       "scrollX": "1000px", 
       "scrollCollapse": true, 
       "paging": false, 
       "searching": true, 
       "dom": '<"top"i>rt<"bottom"lp><"clear">', 
       "select": true 

그리고 테이블의 HTML 부분 :이 컬럼의 많은 내가 그렇게 두 개 이상의 라인을 필요로

<table id="gridProducts" class="display" width="100%"> 
<colgroup> 
    <col width="1%"/> <!-- Del --> 
    <col width="3%"/> <!-- ID --> 
    <col width="3%"/> <!-- Mill --> 
    <col width="3%"/> <!-- Species --> 
    <col width="8%"/> <!-- Dimension --> 
    <col width="10%"/> <!-- Length --> 
    <col width="5%"/> <!-- Grade --> 
    <col width="3%"/> <!-- KD --> 
    <col width="3%"/> <!-- Currency --> 
    <col width="3%"/> <!-- FSC --> 
    <col width="3%"/> <!-- PEFC --> 
    <col width="3%"/> <!-- CW --> 
    <col width="3%"/> <!-- UC --> 
    <col width="3%"/> <!-- InStock --> 
    <col width="3%"/> <!-- Sold --> 
    <col width="3%"/> <!-- Offered --> 
    <col width="3%"/> <!-- Available --> 
    <col width="3%"/> <!-- W01 --> 
    <col width="3%"/> <!-- W02 --> 
    <col width="3%"/> <!-- W03 --> 
    <col width="3%"/> <!-- W04 --> 
    <col width="3%"/> <!-- W05 --> 
    <col width="3%"/> <!-- W06 --> 
    <col width="3%"/> <!-- W07 --> 
    <col width="3%"/> <!-- W08 --> 
    <col width="3%"/> <!-- W09 --> 
    <col width="3%"/> <!-- W10 --> 
    <col width="3%"/> <!-- W11 --> 
    <col width="3%"/> <!-- W12 --> 
</colgroup> 
<thead> 
    <tr> 
     <sec:ifAnyGranted roles="ROLE_ADMIN,ROLE_SALES,ROLE_SUPPLIER"> 
      <th>Del</th> 
     </sec:ifAnyGranted>  
     <th>Id</th> 
     <th>Mill</th> 
     <th>Species</th> 
     <th>Dimension</th> 
     <th>Length</th> 
     <th>Grade</th> 
     <th>KD(%)</th> 
     <th>Cur</th> 
     <th>FSC</th> 
     <th>PEFC</th> 
     <th>CW</th> 
     <th>UC</th> 
     <th>InStock</th> 
     <th>Sold</th> 
     <th>Offer</th> 
     <th>Avail(m3)</th> 
     <th>${myTag.weekNo(offset: "1")}</th> 
     <th>${myTag.weekNo(offset: "2")}</th> 
     <th>${myTag.weekNo(offset: "3")}</th> 
     <th>${myTag.weekNo(offset: "4")}</th> 
     <th>${myTag.weekNo(offset: "5")}</th> 
     <th>${myTag.weekNo(offset: "6")}</th> 
     <th>${myTag.weekNo(offset: "7")}</th> 
     <th>${myTag.weekNo(offset: "8")}</th> 
     <th>${myTag.weekNo(offset: "9")}</th> 
     <th>${myTag.weekNo(offset: "10")}</th> 
     <th>${myTag.weekNo(offset: "11")}</th> 
     <th>${myTag.weekNo(offset: "12")}</th> 
    </tr> 
</thead> 
<tbody> 

    <g:each in="${prodBuffer}" status="i" var="pb"> 
     <tr class="${ (i % 2) == 0 ? 'even': 'odd'}"> 
      <sec:ifAnyGranted roles="ROLE_ADMIN,ROLE_SALES, ROLE_SUPPLIER"> 
       <td> 
        <g:link action="deleteProduct" controller="ordersAndStore" params="[prodID:pb.id]" 
        onclick="return confirm('Are you sure?')"> 
        X 
        </g:link> 
       </td> 
      </sec:ifAnyGranted>  
      <td> <g:link action="edit" controller="prodBuffer" params="[id:pb.id]"> ${pb.id}</g:link></td> 
      <td>${pb.sawMill?:'UnDefined'}</td> 
      <td>${pb.species}</td> 
      <td>${pb.dimension}</td> 
      <td>${pb.length}</td> 
      <td>${pb.grade}</td> 
      <td>${pb.kd}</td> 
      <td>${pb.currency}</td> 
      <td>${pb.priceFSC}</td> 
      <td>${pb.pricePEFC}</td> 
      <td>${pb.priceCW}</td> 
      <td>${pb.priceUC}</td> 
      <td>${pb.volumeInStock}</td> 
      <td>${pb.volumeOnOrder}</td> 
      <sec:ifAnyGranted roles="ROLE_ADMIN,ROLE_SALES"> 
       <td><div id="${pb.id}" class="offers" >${pb.volumeOffered}</div></td> 
      </sec:ifAnyGranted>  
      <sec:ifNotGranted roles="ROLE_ADMIN,ROLE_SALES">  
       <td>${pb.volumeOffered}</td> 
      </sec:ifNotGranted>  
      <td>${pb.volumeAvailable}</td> 
      <g:each in="${pb.plannedVolumes}" status="j" var="pv"> 
       <td>${pv.volume}</td> 
      </g:each> 
     </tr> 
    </g:each> 
</tbody> 

좋아, 테이블은 다음과 같이 정의된다 그들이 포장하기를 원하지 마라. 텍스트가 50 자이지만 필드가 10 자 너비이면 처음 10 자만 표시됩니다. 그게 가능하지 않아야합니까? 당신의 <td> 요소에 다음 클래스를 추가

답변

0

도메인에 임시 필드를 추가하고 getter에서 방금 복사 한 첫 번째 10자를 복사 한 다음이 새로운 "별칭"필드를 테이블에서 참조했습니다. ...

class ProdBuffer { 
int id 
String dimension 
String length 

static transients = ['shortLength','shortDimension'] 

public String getShortLength() { length.substring(0, Math.min(length.length(), 10)); } 
public String getShortDim() { dimension.substring(0, Math.min(dimension.length(), 10)); } 

를 그리고 표에 : 그것은 테이블의 어떠한 방해와 함께 완벽하게 작동

<table id="gridProducts" class="display"> 
    <colgroup> 
     <col width="1em"/> <!-- Del --> 
... 
    </colgroup> 
    <thead> 
     <tr> 
    ... 

     <th>Dimension</th> 
      <th>Length</th> 
      <th>Grade</th> 
     </tr> 
    </thead> 
    <tbody> 
    ... 

     <td class="shrink">${pb.sawMill?:'UnDefined'}</td> <!-- This (shrink) didn't work--> 
     <td>${pb.species}</td> 
     <td>${pb.shortDim}</td>  <!-- New alias field --> 
     <td>${pb.shortLength}</td> <!-- New alias field --> 
     <td>${pb.grade}</td> 
1

시도 :

$(document).on('click', '.shrink', function() { 
    bootbox.alert(this.innerHTML); 
}); 
+0

그것은 어쩌면 때문이다, 내 테이블에 작동하지 않았다 나는 지금 datatables를 사용합니까? 나는 도메인에 일시적인 필드를 추가하여 내 자신을 해결했고, getter에서 나는 처음으로 10 개의 첫 번째 문자를 복사 한 다음이 새로운 "별칭"- 테이블의 필드를 참조했습니다. 그것은 테이블의 방해없이 완벽하게 작동했습니다. – larand

1

이는 다음과 같습니다

.shrink{ 
    width: 100px; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
    cursor: pointer; 
} 

또한 모달 대화 상자에서 셀의 내용을 표시 할 수있는 기회를 제공 bootbox & 같은 것을 사용할 수 있습니다 귀하의 질문에 대한 답변이 아니지만, 이미 Datatable을 사용하고 있다면 컬럼을 숨기는 기능을 활용할 수 있습니다. localStorage의 마지막 열 선택 항목을 저장하고 사용자가 다시 뷰를 방문 할 때 상태를 다시 설정하여 동적으로 만들 수 있습니다. 사용자가 이러한 기능을 잘 알고 있다고 생각합니다.

당신은 기본적으로 당신이 상태에게 영향을 전환 한 후 일부 변경 이벤트를 수행 할 열 관련 몇 가지 체크 박스를 생성이 link

숨어 열 문제의 예를 찾을 수 있습니다

<div> 
    <label> 
    <input type="checkbox" id="col1"> Hide First Column 
    </label> 
</div> 


$('#col1').change(function() { 
    dt.columns(0).visible(!$(this).is(':checked')); 
}); 

유용한 정보가 유용합니다.

관련 문제