7

オッケイただいま。問題を3つの単純なフィールドに完全に単純化しましたが、addJSONDataメソッドを使用しても同じ行にとどまります。私はこれに何日も立ち往生していて、ajax呼び出し、json文字列、何とか何とか何とかをどのようにやり直しても...これを機能させることはできません!1行のデータを手動で追加すると、関数として機能させることさえできません。誰かがASP.NETとJSONで動作するjqGridの動作サンプルを投稿できますか?2〜3個のフィールド(文字列、整数、日付が望ましい)を含めていただけませんか?jqGridの動作サンプルと、addJSONDataメソッドを使用したJSONオブジェクトの手動追加だけを見ていただければ幸いです。本当にありがとう!!これが機能するようになった場合は、ASP.NETからのヘルプを得るために、他のすべての投稿の完全なコードサンプルを投稿します。JSONユーザーもこれに固執しています。また。ありがとう!!

tbl.addJSONData(objGridData); //エラー:tbl.addJSONDataは関数ではありません!!

このメッセージを受け取ったときにFirebugが表示しているものは次のとおりです。

•objGridDataObjecttotal = 1 page = 1 records = 5 rows=[5]
○Page"1"
Records "5"
Total "1"
Rows [Object ID = 1 PartnerID = BCN、Object ID = 2 PartnerID = BCN、Object ID = 3 PartnerID = BCN、さらに2つ... 0=オブジェクト1=オブジェクト2=オブジェクト3=オブジェクト4=オブジェクト]
(インデックス)0
(プロパティ)ID(値)1(プロパティ)PartnerID(値) "BCN"( prop)DateTimeInserted(値)2008年5月29日木曜日12:08:45 GMT-0700(太平洋夏時間)
*さらに3つの行があります

変数tbl(値)の値は次のとおりです'Table.scroll'

<TABLE cellspacing="0" cellpadding="0" border="0" style="width: 245px;" class="scroll grid_htable"><THEAD><TR><TH class="grid_sort grid_resize" style="width: 55px;"><SPAN> </SPAN><DIV id="jqgh_ID" style="cursor: pointer;">ID <IMG src="http://localhost/DNN5/js/jQuery/jqGrid-3.4.3/themes/sand/images/sort_desc.gif"/></DIV></TH><TH class="grid_resize" style="width: 90px;"><SPAN> </SPAN><DIV id="jqgh_PartnerID" style="cursor: pointer;">PartnerID </DIV></TH><TH class="grid_resize" style="width: 100px;"><SPAN> </SPAN><DIV id="jqgh_DateTimeInserted" style="cursor: pointer;">DateTimeInserted </DIV></TH></TR></THEAD></TABLE>

完全な機能は次のとおりです。

 $('table.scroll').jqGrid({  
    datatype: function(postdata) {    
        mtype: "POST",    
    $.ajax({  
        url: 'EDI.asmx/GetTestJSONString',  
        type: "POST",  
        contentType: "application/json; charset=utf-8",  
        data: "{}",  
        dataType: "text", //not json . let me try to parse  
        success: function(msg, st) {  
            if (st == "success") {                    
                 var gridData;  

                //strip of "d:" notation  
                var result = JSON.parse(msg);   
                for (var property in result) {  
                    gridData = result[property];  
                    break;  
                }  

                var objGridData = eval("(" + gridData + ")"); //creates an object with visible data and structure  
                var tbl = jQuery('table.scroll')[0];  

                alert(objGridData.rows[0].PartnerID); //displays the correct data  

                //tbl.addJSONData(objGridData); //error received: addJSONData not a function  

                //error received: addJSONData not a function (This uses eval as shown in the documentation)  
                //tbl.addJSONData(eval("(" + objGridData + ")"));   

                //the line below evaluates fine, creating an object and visible data and structure  
                //var objGridData = eval("(" + gridData + ")");  
                //BUT, the same thing will not work here  
                //tbl.addJSONData(eval("(" + gridData + ")"));  
                //FIREBUG SHOWS THIS AS THE VALUE OF gridData:  
               // "{"total":"1","page":"1","records":"5","rows":[{"ID":1,"PartnerID":"BCN","DateTimeInserted":new Date(1214412777787)},{"ID":2,"PartnerID":"BCN","DateTimeInserted":new Date(1212088125000)},{"ID":3,"PartnerID":"BCN","DateTimeInserted":new Date(1212088125547)},{"ID":4,"PartnerID":"EHG","DateTimeInserted":new Date(1235603192033)},{"ID":5,"PartnerID":"EMDEON","DateTimeInserted":new Date(1235603192000)}]}"         

            }  
        }  
    });  
    },  
    jsonReader: {  
        root: "rows", //arry containing actual data  
        page: "page", //current page  
        total: "total", //total pages for the query  
        records: "records", //total number of records  
        repeatitems: false,  
        id: "ID" //index of the column with the PK in it   
    },  
    colNames: [  
        'ID', 'PartnerID', 'DateTimeInserted'  
          ],    
    colModel: [    
    { name: 'ID', index: 'ID', width: 55 },    
    { name: 'PartnerID', index: 'PartnerID', width: 90 },  
    { name: 'DateTimeInserted', index: 'DateTimeInserted', width: 100}],  
    rowNum: 10,  
    rowList: [10, 20, 30],  
    imgpath: 'http://localhost/DNN5/js/jQuery/jqGrid-3.4.3/themes/sand/images',  
    pager: jQuery('#pager'),  
    sortname: 'ID',  
    viewrecords: true,  
    sortorder: "desc",  
   caption: "TEST Example")};  
4

8 に答える 8

11

これが簡単な例です...

これを機能させるには、 https://github.com/douglascrockford/JSON-js/blob/master/json2.jsが必要 です...

そしてもちろん、通常のjqueryファイル。

これをWebサービスに貼り付けます

// The lower case properties here are required to be lower case
// I cant find a way to rename them when they are serialized to JSON
// XmlElement("yournamehere") does not work for JSON :(
public class JQGrid
{
    public class Row
    {
        public int id { get; set; }
        public List<string> cell { get; set; }

        public Row()
        {
            cell = new List<string>();
        }
    }

    public int page { get; set; }
    public int total { get; set; }
    public int records { get; set; }
    public List<Row> rows { get; set; }

    public JQGrid()
    {
        rows = new List<Row>();
    }
}


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyWebService : System.Web.Services.WebService
{

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]    
    public JQGrid GetJQGrid(int page, int pageSize, string sortIndex, string sortDirection)
    {
        DataSet ds = SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING, "udsp_GetMyData",pageIndex, pageSize);

        if (ds == null || ds.Tables.Count < 1)
            throw new Exception("Unable to retrieve data.");

        JQGrid jqGrid = new JQGrid();

        int i = 1;
        foreach (DataRow dataRow in ds.Tables[0].Rows)
        {
            JQGrid.Row row = new JQGrid.Row();

            row.id = Convert.ToInt32(dataRow["MyIdColumn"]);

            row.cell.Add(dataRow["MyIdColumn"].ToString());

            row.cell.Add(dataRow["MyColumn"].ToString());


            projectGrid.rows.Add(row);
        }

        jqGrid.page = 1; // Set this when you are actually doing paging... this is just a sample
        jqGrid.records = jqGrid.rows.Count;
        jqGrid.total = jqGrid.rows.Count;  // Set this to total pages in your result...

        return jqGrid;
    }
}

これをaspxページに貼り付けます

<script type="text/javascript">
function getData(pdata) {
    var params = new Object();
    params.page = pdata.page;
    params.pageSize = pdata.rows;
    params.sortIndex = pdata.sidx;
    params.sortDirection = pdata.sord;


    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/CLM/CLM.asmx/GetProjectGrid2",
        data: JSON.stringify(params),
        dataType: "json",
        success: function(data, textStatus) {
            if (textStatus == "success") {
                var thegrid = $("#testGrid")[0];
                thegrid.addJSONData(data.d);
            }
        },
        error: function(data, textStatus) {
            alert('An error has occured retrieving data!');
        }
    });
}

var gridimgpath = '/clm/css/ui-lightness/images';
$(document).ready(function() {
    $("#testGrid").jqGrid({
        datatype: function(pdata) {
            getData(pdata);
        },
        colNames: ['My Id Column', 'My Column'],
        colModel: [
            { name: 'MyIdColumn', index: 'MyIdColumn', width: 150 },
            { name: 'My Column', index: 'MyColumn', width: 250 }
        ],
        rowNum: 10,
        rowList: [10, 20, 30],
        imgpath: gridimgpath,
        pager: jQuery('#pagerdt'),
        sortname: 'id',
        viewrecords: false,
        sortorder: "desc",
        caption: "Projects",
        cellEdit: false
    });
});
</script>
于 2009-08-10T14:36:59.310 に答える
3

通常、jqGrid で 'blah not a function' エラーが発生するのは、正しいモジュールがロードされていないためです。addJSONData 関数は grid.base.js ファイルで定義されています。jquery.jqGrid.js ファイルの jqGridInclude() 関数をチェックして、モジュール変数の初期化の一部として grid.base.js が含まれていることを確認できますか?

于 2009-07-28T20:27:06.940 に答える
1

よく私はここで1つの間違っていることがわかります:

var tbl = jQuery('table.scroll')[0];  
 //tbl.addJSONData(objGridData); //error received: addJSONData not a function  

なぜこのエラーが発生するのか疑問に思っているのなら、それはtblにその機能がないためです。

編集:興味があり、jqGridがそれらのメソッドをDOM参照オブジェクトに追加したかどうかを確認しました。そして彼らはそうしました。(ここでfirebugを使用して確認しました:http://trirand.com/jqgrid/jqgrid.html)。

ここで発生している可能性のあることの1つは、クラス'scroll'のテーブルが複数あり、jqueryが間違ったテーブルを返していることです。

于 2009-04-07T20:52:23.853 に答える
1

スタックオーバーフローに投稿されたこのコードの助けになるでしょうか? jqgrid と asp.net webmethod および json は、並べ替え、ページング、検索、および LINQ で動作しますが、動的演算子が必要です

于 2010-03-09T22:36:03.080 に答える
0

ASP.NET WebForms と jqGrid について多くの質問が寄せられたため、「コンポーネント」の方法で asp:GridView に非常に似たものを実装することにしました。このようにして、使い慣れた ASP.NET ページのライフサイクル、イベント、データソースなどを使用して jqGrid を制御できます。

このオンラインのベータ版は、ここで見ることができます。現在、30 以上のサンプルが利用可能です。

http://www.trirand.net/demo.aspx

関心があれば、おそらく商用製品にも成長するでしょう (オープンソース ライセンスが利用可能になります)。その間、Reflector を使用して参照用のソースをチェックアウトできます (ソースをオンラインで利用できるようにする方法が見つかるまで)。jsコアのgitHubではなく、ASP.NETにSVNを使用しているため、その方向でいくつかの作業があります。

これがコミュニティに役立つことを願っています。

ルーメン・スタンコフ・トリランド

于 2009-10-01T05:18:13.507 に答える
0

これは非常に古い質問ですが、最近同じ問題が発生していました。これをどのように達成したかを、始めようとしている新しいブログに投稿しました。

これを行うためのよりクリーンな方法があるかもしれませんが、これは私にとってはうまくいきました。これまでのところ、この例からかなり簡単にスケールアップできました。次のハードルは、loadonce を機能させることです。

ここで私の例を見つけることができます:

http://programming.webdad3.com/?p=3

于 2010-07-03T16:38:31.023 に答える