简体   繁体   English

如何在 jQuery Bootgrid 和 C# 之间使用 Ajax json 格式构建 CRUD(而不是 JqGrid 来处理此问题)

[英]How to build the CRUD with Ajax json format between jQuery Bootgrid and C# (instead of JqGrid to deal with this issue)

Hi all I have been looking for a lot of relative resource to deal with my problem, however it isn't useful to me, even if I have checked the "jQuery Bootgrid" and many websites , I still didn't find any great example out, hopefully someone who really know it can help me to give me any slim clues or any good simple.大家好,我一直在寻找很多相关资源来解决我的问题,但是它对我没有用,即使我已经检查了“jQuery Bootgrid”和许多网站,我仍然没有找到任何很好的例子出来,希望真正了解它的人可以帮助我给我任何微不足道的线索或任何好的简单。
By the way,顺便一提,
1. I wish I can use Ajax to update and see any modifies data between Browser and DB 1. 我希望我可以使用 Ajax 来更新和查看浏览器和数据库之间的任何修改数据
2. Plesae give me example code 2.请给我示例代码
Thanks谢谢

Some problems issue一些问题问题
1.Ajax Connection 1.Ajax连接
Front end前端

   <table id="grid-data" class="table table-condensed table-hover table-striped">
    <thead>
        <tr>
            <th data-column-id="id" data-type="numeric">ID</th>
            <th data-column-id="sender" data-css-class="sender">Sender</th>
            <th data-column-id="received" data-order="desc">Received</th>
        </tr>
    </thead>
</table>

    <script>
    var grid = $("#grid-data").bootgrid({
        ajax: true,
        url: "WebForm3.aspx/x" // link to your web api
    });
</script>

Back End后端

  public partial class WebForm3 : System.Web.UI.Page
    {
        [WebMethod]

        public static string x()
        {
            return "";
        }
    }

Messages留言

Uncaught SyntaxError: Unexpected token < in JSON at position 6
    at JSON.parse (<anonymous>)
    at Function.n.parseJSON (jquery-2.1.1.min.js:4)
    at Object.success (jquery.bootgrid.min.js:6)
    at j (jquery-2.1.1.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-2.1.1.min.js:2)
    at x (jquery-2.1.1.min.js:4)
    at XMLHttpRequest.<anonymous> (jquery-2.1.1.min.js:4)

At the moment, bootgrid doesn't provide a way to achieve this out of the box.目前, bootgrid没有提供一种开箱即用的方法来实现这一点。 I'd recommend using X-editable (I prefer bootstrap version) together with bootgrid.我建议将X-editable (我更喜欢引导程序版本)与 bootgrid 一起使用。 Below are the steps to achieve what you want.以下是实现您想要的目标的步骤。

Include references包括参考

Include all the necessary script/css references in your page.在您的页面中包含所有必要的脚本/css 引用。 In this example, I needed to include:在这个例子中,我需要包括:

  • jQuery jQuery
  • Bootstrap引导程序
  • Bootgrid引导网格
  • xEditable (bootstrap-editable version) xEditable(引导程序可编辑版本)

Setup your bootgrid HTML设置您的引导网格 HTML

I got this from their examples :我从他们的例子中得到了这个:

<table id="grid-data" class="table table-condensed table-hover table-striped">
    <thead>
        <tr>
            <th data-column-id="id" data-type="numeric">ID</th>
            <th data-column-id="sender" data-css-class="sender">Sender</th>
            <th data-column-id="received" data-order="desc">Received</th>
        </tr>
    </thead>
</table>

Take note I added a data-css-class="sender" to the sender's column ( th tag).请注意,我在发件人的列( th标记)中添加了一个data-css-class="sender" This will make bootgrid insert a "sender" class to every td in that column.这将使 bootgrid 向该列中的每个td插入一个“发送者”类。

Setup your bootgrid javascript设置你的引导网格 javascript

Just create your bootgrid as you would normally.只需像往常一样创建您的引导网格。 Again, I got this sample from their page:同样,我从他们的页面上得到了这个样本:

var grid = $("#grid-data").bootgrid({
    ajax: true,
    url: "/api/data/basic" // link to your web api
});

I keep a reference to the grid element here, because I need to bind an event to it, when its data is loaded , that is, when all rows ( tr ) with their respective td are created inside tbody html tag.我在这里保留对grid元素的引用,因为我需要在加载数据时将事件绑定到它,也就是说,当在tbody html 标记内创建具有各自td所有行 ( tr ) 时。

Bind X-editable to your td将 X-editable 绑定到您的td

Just bind the x-editable to your cells.只需将 x-editable 绑定到您的单元格即可。 We need to do this inside bootgrid's loaded event , because inside it we are sure all td elements were already created in the DOM .我们需要在 bootgrid 的加载事件中执行此操作,因为在其中我们确定所有td元素都已经在DOM 中创建。

grid.on("loaded.rs.jquery.bootgrid", function()
{
    /* Executes after data is loaded and rendered */
    /* looks for all td's with "sender" class...and make them editable... */
    grid.find("td.sender").editable({
        url: '/post', // web api url to your post
        title: 'Enter the sender...'
    });
});

Look X-editable example page for more examples.查看X 可编辑示例页面以获取更多示例。 Read their docs to know how to use them.阅读他们的文档以了解如何使用它们。


Also take note X-editable is just one possibility, if you don't like it, or you are more familiar with other plugin, use it instead.另请注意 X-editable 只是一种可能性,如果您不喜欢它,或者您更熟悉其他插件,请改用它。 Just make sure you configure it inside the bootgrid's loaded event, as I did.只要确保你像我一样在 bootgrid 的loaded事件中配置它。

When working with ajax, everytime you type in something in search box, order by a column, change the page, bootgrid will send a request and recreate everything inside tbody .当使用Ajax的工作,每次你搜索框中,为了通过一些列类型,更改页面,bootgrid会发送一个请求,并重新创建里面一切tbody Which means all tr and td will be removed from DOM and recreated (thus firing loaded again).这意味着所有trtd将从 DOM 中删除并重新创建(因此再次触发loaded )。


See JSFiddle .请参阅JSFiddle

New idea for my problem我的问题的新想法
I have used the jqGrid to deal with my problem and it is very useful to handle UI(DataGrid) with CRUD for me and it also has a lot of specific instructions in Google if you need to more specific steps or operations, so that it should be very good idea to instead of BootGrid.我已经使用jqGrid来处理我的问题,它对我来说使用 CRUD 处理 UI(DataGrid) 非常有用,如果您需要更具体的步骤或操作,它在 Google 中也有很多具体说明,因此它应该代替 BootGrid 是个好主意。 it is my personal suggestion.这是我个人的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM