2017-01-31 3 views
0

How to update value of data in jqgrid에서 설명한 솔루션을 사용하여 데이터를 로컬로 업데이트했습니다. 그것은 첫 번째 바이올린에 나와있는 것처럼 나를 위해 일했습니다. "작업"열에는 "IsActive"열의 데이터를 기반으로 조건부로 추가 된 단추가 있습니다. "true"이면 "Retire"버튼이 액션으로 추가됩니다. false이면 "Activate"버튼이 추가됩니다. 자바 스크립트 함수가 호출되면 버튼이 "활성화"로 바뀝니다.두 열이 동일한 필드를 사용할 때 jqGrid의 값이 업데이트되지 않습니다.

Fiddle 1

이제, 텍스트 등의 상태 값을 표시하는 다른 항목을 추가했다. 이제 "Status"열과 "Action"열은 동일한 데이터 열 (IsActive)을 사용합니다. 이 열을 추가 한 후 javascript 함수는 버튼을 "Retire"에서 "Activate"로 변경하지 않습니다.

Fiddle 2

이 문제를 해결하는 가장 좋은 방법은 무엇입니까?

코드는

$(document).ready(function() { 

function updateActiveStatus(rowid, isToActive) { 

alert('function'); 

         // first change the cell in the visible part of grid 
         $("#list").jqGrid('setCell', rowid, 'firstname', 'A'); 

         // now change the internal local data 
         $("#list").jqGrid('getLocalRow', rowid).firstname = 'A'; 


         $("#list").jqGrid('setCell', rowid, 'IsActive', false); 
         $("#list").jqGrid('getLocalRow', rowid).IsActive = false; 



      } 


      var myData = [ 

       { "id": "35", "firstname": null, "codeval": "G", "note": "xx7866", "amount": "23", "IsActive": true }, 
       { "id": "73", "firstname": null, "codeval": "W", "note": "dd1047", "amount": "34", "IsActive": true }, 
       { "id": "75", "firstname": "LORA", "codeval": "H", "note": "rr7323", "amount": "56", "IsActive": true }, 
       { "id": "95", "firstname": "EST", "codeval": "M", "note": "gg574", "amount": "55", "IsActive": true } 
       ], 

       myGrid = $("#list"); 

      myGrid.jqGrid({ 
       datatype:'local', 
       data: myData, 
       colNames: ['ID', 'FirstName', 'Code', 'Amount', 'Note', 'Action'], 
       colModel:[ 
        {name:'id',width:70,align:'center',sorttype: 'int'}, 
        {name:'firstname',width:80, align:'center'}, 
        { name: 'codeval', width: 70 }, 
        {name:'amount',width:100, formatter:'number'}, 
        {name:'note',index:'note',width:100,sortable:false}, 
        { 
         name: 'IsActive', 
         width: 100, 
         formatter: function (cellvalue, options, rowObject)             { 
          if (cellvalue == true) { 
           return '<div style="padding-left:5px;"><button class="ui-button ui-widget ui-state-default app-custom-button-retire" >' + 
             '<span title="" class="ui-button-icon-primary ui-icon ui-icon-scissors"></span>Retire' + 
             '</button></div>'; 
          } 
          else { 
           return '<div style="padding-left:5px;"><button class="ui-button ui-widget ui-state-default app-custom-button-activate" >' + 
             '<span title="" class="ui-button-icon-primary ui-icon ui-icon-check"></span>Activate' + 
             '</button></div>'; 
          } 
         } 
        } 
       ], 
       rowNum:10, 
       pager: '#pager', 
       gridview:true, 
       ignoreCase:true, 
       rownumbers:true, 
       viewrecords: true, 
       sortorder: 'desc', 
       height: '100%', 
       beforeSelectRow: function (rowid, e) { 
        var $self = $(this), 
         $td = $(e.target).closest("tr.jqgrow>td"), 
         rowid = $td.parent().attr("id"), 
         //rowData = $self.jqGrid("getLocalRow", rowid), 
         rowData = $self.jqGrid("getRowData", rowid) 
        iCol = $td.length > 0 ? $td[0].cellIndex : -1, 
        colModel = $self.jqGrid("getGridParam", "colModel"); 

        celValue = $self.jqGrid('getCell', rowid, 'FirstName'); 

        if (iCol >= 0 && colModel[iCol].name === "IsActive") { 

         if ($(e.target).hasClass("app-custom-button-retire")) { 
          updateActiveStatus(rowid,false); 
          return false; 
         } 

         if ($(e.target).hasClass("app-custom-button-activate")) { 

          updateActiveStatus(rowid, true); 
          return false; 
         } 
        } 


        //Avoid selection of row 
        return false; 
       } 
      }); 
      myGrid.jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false, defaultSearch: "cn" }); 

     }); 

답변

1

난 당신의 코드에서 여러 오해를 보인다. 먼저, 항목의 배열 인 일부 원본 데이터가 있습니다.

{ "id": "75", "firstname": "LORA", "codeval": "H", "note": "rr7323", 
    "amount": "56", "IsActive": true } 

또는, 동일

{ id: "75", firstname: "LORA", codeval: "H", note: "rr7323", 
    amount: "56", IsActive: true } 

같은 항목이 같은 이름와 여러 속성을 가질 수 있음을 분명히해야한다 : 모든 항목이 같은 여러 속성을 가진 객체입니다 . 이 객체는 어떤 웹 브라우저가 오류를 무시할지라도 잘못된 것입니다. 객체는

{ "id": "75", "firstname": "LORA", "codeval": "H", "note": "rr7323", 
    "amount": "56", "IsActive": true, "IsActive": true } 

입니다. 을 지정한 경우에도 모두 "IsActive"속성에 대해true이 지정됩니다.

동일한 방법으로 동일한 name 속성을 가진 colModel의 여러 열을 사용할 수 없습니다. 두 번째 데모 https://jsfiddle.net/LijoCheeran/rqab1veh/11/두 개의 열과 동일한 속성 name: 'IsActive'입니다. 틀렸어. 코드를 사용법에 따라 수정할 수 있습니다 (예 : name: 'IsActive1'). IsActive1의 포맷터는 cellvalue 대신 rowObject.IsActive을 사용하여 필요한 데이터에 액세스 할 수 있습니다. 해당 코드 retireButtonactiveButton는 버튼 HTML 단편을 포함 follwing을

{ 
    name: 'IsActive1', 
    width: 75, 
    formatter: function (cellvalue, options, rowObject) { 
     return rowObject.IsActive == true ? 'Active' : 'Retired'; 
    } 
}, 
{ 
    name: 'IsActive', 
    width: 100, 
    formatter: function (cellvalue, options, rowObject) { 
     return cellvalue == true ? retireButton : activeButton; 
    } 
} 

것이다.

이제 jqGrid가 data 어레이를 보유한다는 것을 이해하는 것이 중요합니다. 방법 $("#list").jqGrid('getLocalRow', rowid)은 행의 데이터에 해당하는 데이터 항목참조를 가져옵니다. 메소드 getRowData은 셀의 HTML 표현 (<td> 요소)에서 데이터를 가져 와서 포맷을 해제합니다. 반환 된 객체의 필드 유형은 문자열입니다.

당신은뿐만 아니라 데이터를 업데이트해야하지만 firstname, IsActive1IsActive 열의 셀 내용은 다음 필드 또는 더 나은 통화마다에 setCell 전화를 가지고 있기 때문에 하나 setRowData :

function updateActiveStatus (rowid, isToActive) { 
    alert('calling the function'); 
    $("#list").jqGrid('setRowData', rowid, { 
     firstname: 'A', 
     IsActive1: false, 
     IsActive: false 
    }); 
    var item = $("#list").jqGrid('getLocalRow', rowid); 
    // delete unneeded IsActive1 property created in the item 
    delete item.IsActive1; 
} 

유일한에게 setRowData 호출의 작은 단점은 데이터 항목에 새 속성 IsActive1을 작성하는 중입니다. Old jqGrid 4.6은 item.IsActive1과 같은 다른 장소에 데이터를 "사실상"저장할 가능성이 없습니다. 무료 jqGrid는 saveLocally 콜백을 지정하여 로컬 항목의 열 데이터를 사용자 정의 "저장할"수 있습니다. 그것은 귀하의 경우 실제 큰 문제가되지 않습니다 그리고 당신은 당신은 수정 된 데모에 결과를 볼 수 있습니다 delete item.IsActive1

에 의해 불필요한 속성을 삭제해야합니다 https://jsfiddle.net/OlegKi/rqab1veh/12/