English 中文(简体)
in slickgrid how I can delete a row from a javascript function
原标题:

how I can delete a row from a javascript function from a button for example

问题回答

If you re using a DataView, use the following:

DataView.deleteItem(RowID);//RowID is the actual ID of the row and not the row number
Grid.invalidate();
Grid.render();

If you only know the row number, you can get theRowID using:

var item = DataView.getItem(RowNum);//RowNum is the number of the row
var RowID = item.id

Suppose you are using jQuery

var grid;
$(function () {
   // init options, load data  
   ...

   var columns = [];
   columns[0] = { 
     id:  id ,
     name:  # , 
     field:  id , // suppose you have an id column in your data model
     formatter: function (r, c, id, def, datactx) { 
        return  <a href="#" onclick="RemoveClick(  + id +  ,  + r +  )">X</a> ; }
   }
   // init other columns
   ...

   grid = new Slick.Grid($( #gridDiv ), data, columns, options);
}
function RemoveClick(databaseId, gridRow) {
   // remove from serverside using databaseId
   ...
   // if removed from serverside, remove from grid using
   grid.removeRow(gridRow);
}

This is how i do it (not using any data provider though):

//assume that "grid" is your SlickGrid object and "row" is the row to be removed
var data = grid.getData(); 
data.splice(row, 1);
grid.setData(data);
grid.render();

I use this in a live project and it runs well. Of course, if you want to remove multiple rows then a few tweaks should be made, or if you use a data provider then you d maybe want to remove the row only from the data provider and then have SlickGrid just refresh the rows.

Hope it helps :)

function deleteRows() {
    var selectedIndexes = grid.getSelectedRows().sort().reverse();
    var result = confirm("Are you sure you want to delete " + grid.getSelectedRows().length + " row(s)?");
    if (result) {

      $.each(selectedIndexes, function (index, value) {
        var item = dataView.getItem(value); //RowNum is the number of the row
        if (item)
          dataView.deleteItem(item.id); //RowID is the actual ID of the row and not the row number
      });

      grid.invalidate();
      grid.render();
    }
  }
var rowsToDelete = grid.getSelectedRows().sort().reverse();
for (var i = 0; i < rowsToDelete.length; i++) {
    data.splice(rowsToDelete[i], 1);
}
grid.invalidate();
grid.setSelectedRows([]);

yes of course, I use it this way

var selrow = grid.getSelectedRows (); 
data.splice(selrow, 1); 
grid.invalidateAllRows();
grid.render ();

Greetings





相关问题
(PHP & mySQL) Treat rows as columns

I am working on PHP & mySQL based website. I am trying to setup the Settings page in administration panel where I can enter different settings to manage the site. The settings can range upto 100 ...

jQuery Delete From Table

This code does following: Click on "Delete" in item row -> deletes item row Click on "Delete" in category row -> deletes category row and all item rows (in all table) Need it to do following: Click ...

Best Way to Ajax-Control Table Rows

I want to be able to print out a table with x rows and about 10 columns. However, I want to be able to select rows in the table as if it was a multiple select box. Selecting the row of the table will ...

Removing a row from an Excel sheet with Apache POI HSSF

I m using the Apache POi HSSF library to import info into my application. The problem is that the files have some extra/empty rows that need to be removed first before parsing. There s not a ...

jqgrid custom row colors

How can I set the background-color of an entire row (not just cell) using the custom formatter?

How to programmatically select top row of JQGrid?

How does one programmatically select the top row of a JQGrid. I want to have the top row already selected when it is opened on the page. My grid is sorted by a descriptive column so the first row s id ...

热门标签