在 slickgrid 中如何从 javascript 函数中删除一行

发布于 2024-09-10 12:44:44 字数 36 浏览 6 评论 0原文

如何从按钮的 javascript 函数中删除一行 例如

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

对你的占有欲 2024-09-17 12:44:44

如果您使用的是 DataView,请使用以下命令:

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

如果您只知道行号,则可以使用以下方法获取 RowID:

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

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
染柒℉ 2024-09-17 12:44:44

假设您正在使用 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);
}

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);
}
酒废 2024-09-17 12:44:44

这就是我的做法(虽然不使用任何数据提供程序):

//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();

我在实时项目中使用它并且运行良好。当然,如果您想删除多行,那么应该进行一些调整,或者如果您使用数据提供程序,那么您可能只想从数据提供程序中删除该行,然后让 SlickGrid 刷新这些行。

希望它有帮助:)

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 :)

蹲在坟头点根烟 2024-09-17 12:44:44
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();
    }
  }
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();
    }
  }
旧时光的容颜 2024-09-17 12:44:44
var rowsToDelete = grid.getSelectedRows().sort().reverse();
for (var i = 0; i < rowsToDelete.length; i++) {
    data.splice(rowsToDelete[i], 1);
}
grid.invalidate();
grid.setSelectedRows([]);
var rowsToDelete = grid.getSelectedRows().sort().reverse();
for (var i = 0; i < rowsToDelete.length; i++) {
    data.splice(rowsToDelete[i], 1);
}
grid.invalidate();
grid.setSelectedRows([]);
会发光的星星闪亮亮i 2024-09-17 12:44:44

这样使用的

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

是的,当然,我就是

yes of course, I use it this way

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

Greetings

与君绝 2024-09-17 12:44:44

你好
我用这个脚本删除 SlickGrid

函数的行deletefila(numrow)
{

  alert("delete row"+numrow);

  data.splice(numrow,1); 

    grid.removeAllRows(); 

    grid.render(); 

        //grid.removeRow(5);

        //grid.updateRowCount();

        //and then invalidate and re-render the grid by calling grid.removeAllRows() followed by grid.render(). 

}

hi
i'm used this script for delete row of SlickGrid

function deletefila(numrow)
{

  alert("delete row"+numrow);

  data.splice(numrow,1); 

    grid.removeAllRows(); 

    grid.render(); 

        //grid.removeRow(5);

        //grid.updateRowCount();

        //and then invalidate and re-render the grid by calling grid.removeAllRows() followed by grid.render(). 

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文