2012-11-28 18 views

risposta

46

Come si definisce la riga che si desidera aggiornare? Presumo che sia la riga che hai selezionato e il nome della colonna in fase di aggiornamento è symbol.

// Get a reference to the grid 
var grid = $("#my_grid").data("kendoGrid"); 

// Access the row that is selected 
var select = grid.select(); 
// and now the data 
var data = grid.dataItem(select); 
// update the column `symbol` and set its value to `HPQ` 
data.set("symbol", "HPQ"); 

Ricordate che il contenuto del DataSource è un oggetto observable, il che significa che è possibile aggiornare utilizzando set e il cambiamento dovrebbe riflettersi magicamente nel grid.

+4

Cosa dovremmo fare se il loro è un modello personalizzato su una cella, che non è modificabile e che il modello chiama una funzione. Chiamare impostato sull'elemento di dati non sembra ridisegnare il modello. – jonperl

+0

@jonperl Ti suggerisco di fare una domanda diversa per assicurarmi che gli altri di me vedano e abbiano l'opportunità di rispondere. Ad ogni modo l'ho provato con i modelli e ha funzionato. Quindi, per favore pubblica un codice che mostra ciò che hai provato e mostra ciò che fallisce. – OnaBai

+0

NOTA: 'kendoDataGrid' è ora chiamato 'kendoGrid' (o forse lo è sempre stato) –

26

data.set effettivamente aggiornerà l'intera griglia e invierà un evento databound in alcuni casi. Questo è molto lento e non necessario. Inoltre, collassa qualsiasi modello di dettaglio espanso che non è l'ideale.

Si consiglia di utilizzare questa funzione che ho scritto per aggiornare una singola riga in una griglia di kendo.

// Updates a single row in a kendo grid without firing a databound event. 
// This is needed since otherwise the entire grid will be redrawn. 
function kendoFastRedrawRow(grid, row) { 
    var dataItem = grid.dataItem(row); 

    var rowChildren = $(row).children('td[role="gridcell"]'); 

    for (var i = 0; i < grid.columns.length; i++) { 

     var column = grid.columns[i]; 
     var template = column.template; 
     var cell = rowChildren.eq(i); 

     if (template !== undefined) { 
      var kendoTemplate = kendo.template(template); 

      // Render using template 
      cell.html(kendoTemplate(dataItem)); 
     } else { 
      var fieldValue = dataItem[column.field]; 

      var format = column.format; 
      var values = column.values; 

      if (values !== undefined && values != null) { 
       // use the text value mappings (for enums) 
       for (var j = 0; j < values.length; j++) { 
        var value = values[j]; 
        if (value.value == fieldValue) { 
         cell.html(value.text); 
         break; 
        } 
       } 
      } else if (format !== undefined) { 
       // use the format 
       cell.html(kendo.format(format, fieldValue)); 
      } else { 
       // Just dump the plain old value 
       cell.html(fieldValue); 
      } 
     } 
    } 
} 

Esempio:

// Get a reference to the grid 
var grid = $("#my_grid").data("kendoGrid"); 

// Access the row that is selected 
var select = grid.select(); 
// and now the data 
var data = grid.dataItem(select); 

// Update any values that you want to 
data.symbol = newValue; 
data.symbol2 = newValue2; 
... 

// Redraw only the single row in question which needs updating 
kendoFastRedrawRow(grid, select); 

// Then if you want to call your own databound event to do any funky post processing: 
myDataBoundEvent.apply(grid); 
+2

Cosa succede se non si sta modificando sulla griglia stessa, ma in un popup. Come si determina la riga che si sta modificando? – Scott

+0

Grazie! Questo mi ha salvato le spalle !!! –

+0

I get get myDataBoundEvent è una variabile di funzione nello snippet di codice sopra. Ma quale tipo di codice entra effettivamente in quella funzione? C'è un esempio vivo di ciò che posso guardare? Attualmente sto avendo il codice sopra senza la chiamata apply alla fine dopo aver richiamato il richiamo del metodo kendoFastRedrawRow e vedo una ruota che gira, sembrando suggerire che il browser sta ancora aspettando qualcosa. Qualche indicazione? – Sudhir

3

ho trovato un modo per aggiornare il dataSource griglia e mostrano nella griglia senza aggiornare tutta la griglia. Ad esempio si dispone di una riga selezionata e si desidera modificare il valore "nome" della colonna.

//the grid 
var grid = $('#myGrid').data('kendoGrid');  
// Access the row that is selected 
var row = grid.select(); 
//gets the dataItem 
var dataItem = grid.dataItem(row); 
//sets the dataItem 
dataItem.name = 'Joe'; 
//generate a new row html 
var rowHtml = grid.rowTemplate(dataItem); 
//replace your old row html with the updated one 
row.replaceWith(rowHtml); 
Problemi correlati