2009-12-07 33 views
0
var store = new FMP.AspNetJsonStore({ 
    fields: [ 
       { name: 'AssetID' }, 
       { name: 'AssociationID' }, 
       { name: 'Image' }, 
       { name: 'StatusName' }, 
       { name: 'ModelName' }, 
       { name: 'IPAddress' }, 
       { name: 'InScope', type: 'boolean' }, 
       { name: 'ServicePlanName' }, 
       { name: 'PricePlanName' }, 
       { name: 'PricePlanDescription' }, 
       { name: 'Program' }, 
       { name: 'ServicePlanID' }, 
       { name: 'Customer' }, 
       { name: 'Black', type: 'float' }, 
       { name: 'Cyan', type: 'float' }, 
       { name: 'Magenta', type: 'float' }, 
       { name: 'Yellow', type: 'float' }, 
       { name: 'BlackPct' }, 
       { name: 'CyanPct' }, 
       { name: 'MagentaPct' }, 
       { name: 'YellowPct' }, 
       { name: 'PrinterMarkerSupplies' }, 
       { name: 'PageCount' }, 
       { name: 'BlackImpressions' }, 
       { name: 'ColorImpressions' }, 
       { name: 'PricePlanID' }, 
       { name: 'ResponsibilityForAction' }, 
       { name: 'PrinterSerialNumber' } 
      ], 

    totalProperty: "TotalCount", 
    autoLoad: { params: { start: 0, limit: myPageSize} }, 
    //autoLoad: true, 
    proxy: new Ext.data.HttpProxy({ 
     // Call web service method using GET syntax 
     url: 'GetPrintersGrid.asmx/buildGrid', 
     // Ask for Json response 
     headers: { 'Content-type': 'application/json' }, 
     method: "GET" 
    }), 
    remoteSort: true, 
    //sortInfo: { field: 'PageCount', direction: "DESC" }, 
    groupField: 'Customer', 
    root: 'Records' 
}); 

store.setDefaultSort('PageCount', 'DESC'); 

這裏是Web服務,我用排序在asp.net

public PagedResult<FMPAsset> buildGrid(int start, int limit, string sortfield, string dir) 
{ 
    var a=5; 

    Guid AccountID = (Guid)Session["AccountID"]; 
    //string sortdir; 
    //if(dir == "DESC") 
    //{ 
    // sortdir = dir.Substring(0, 4).Trim().ToUpper(); 
    //} 
    //else 
    //{ 
    // sortdir = dir.Substring(0, 3).Trim().ToUpper(); 
    //} 

    string SortExpression = sortfield + " " + (!String.IsNullOrEmpty(dir) ? dir : String.Empty); 

    //string whereClause = "SELECT value a FROM XSP_AssetList_V AS a WHERE a.AccountID = GUID'" + AccountID + "' order by a.PageCount = '" + + "'"; 
    string whereClause = "SELECT value a FROM XSP_AssetList_V AS a WHERE a.AccountID = GUID'" + AccountID + "' Order By a."+SortExpression; 
    //string whereClause = "SELECT value a , ROW_NUMBER() OVER(ORDER BY" 
    //  + " " + SortExpression 
    //  + ") As RowNumber FROM XSP_AssetList_V AS a WHERE a.AccountID = GUID'" 
    //  + AccountID + "'"; 
    //string whereClause = "SELECT value a FROM XSP_AssetList_V AS a WHERE a.AccountID = GUID'" + AccountID + "'"; 
    List<FMPAsset> fmpAssets = new List<FMPAsset>(); 

    using (XSPAssetModel.XSPAssetEntities assetEntities = new XSPAssetEntities(b.BuildEntityConnectionString1("XSMDSN"))) 
    { 
     ObjectQuery<XSP_AssetList_V> assets = new ObjectQuery<XSP_AssetList_V>(whereClause, assetEntities); 
     //var assetOrder = assets.OrderBy(x => x.StatusName).ToList(); 
     var assetPage = assets.Skip(start).Take(limit); 

     //var totalAssetCount = assets.Count(); 

     currentAssets = assetPage.ToList(); 
     int currentAssetsCount = currentAssets.Count; 
     string imgprefix = System.Configuration.ConfigurationManager.AppSettings["ImgPrefix"]; 
     char[] separators = { '/' }; 
     string appname = ""; 
     int lastloc = imgprefix.Substring(0, imgprefix.Length - 1).LastIndexOfAny(separators); 
     if (lastloc > 6) 
     { 
      appname = imgprefix.Substring(lastloc + 1); 
     } 

     FMPAsset asset = new FMPAsset(); 

     //StreamWriter sw = new StreamWriter("C:\\test.txt"); 

     XSPPrinterMarkerSupplyModel.XSPPrinterMarkerSupplyEntities markerCtx = 
      new XSPPrinterMarkerSupplyModel.XSPPrinterMarkerSupplyEntities(b.BuildEntityConnectionString1("XSMDSN")); 

     for (int x = 0; x < currentAssetsCount; x++) 
     { 
      asset = new FMPAsset(); 

      asset.AssetID = currentAssets[x].AssetID.ToString(); 
      asset.PricePlanID = currentAssets[x].PricePlanID.ToString(); 
      asset.AssociationID = currentAssets[x].AssociationID; 
      asset.ModelName = currentAssets[x].ModelName; 
      asset.ResponsibilityForAction = currentAssets[x].ResponsibilityForAction; 
      asset.IPAddress = (String.IsNullOrEmpty(currentAssets[x].PrinterIPAddress)) ? "No IP" : currentAssets[x].PrinterIPAddress; ; 

      if (currentAssets[x].InScope) 
      { 
       asset.InScope = b.GetString("SDE_YES"); 
      } 
      else 
      { 
       asset.InScope = b.GetString("SDE_NO"); 
      } 

      asset = SetStatus(appname, asset, x); 

      asset.PricePlanName = currentAssets[x].Program; 
      asset.PricePlanDescription = currentAssets[x].PricePlanDescription; 
      asset.ServicePlanName = currentAssets[x].ServicePlanName; 

      if (currentAssets[x].PrinterSerialNumber != null) 
      { 
       asset.PrinterSerialNumber = currentAssets[x].PrinterSerialNumber; 
      } 
      else 
      { 
       asset.PrinterSerialNumber = "-"; 
      } 

      //sw.WriteLine("ChargebackDescription: " + DateTime.Now.Millisecond); 
      if (this.b.UseChargebackDescription 
       && !String.IsNullOrEmpty(currentAssets[x].CustomerChargebackDescription) 
       && currentAssets[x].CustomerChargebackDescription != "Generated by OUT Integration") 
      { 
       asset.Customer = currentAssets[x].CustomerChargebackDescription; 

       if (asset.Customer.IndexOf(Environment.NewLine) > -1) 
       { 
        asset.Customer = 
         asset.Customer.Substring(0, asset.Customer.IndexOf(Environment.NewLine)); 
       } 
      } 
      else 
      { 
       asset.Customer = currentAssets[x].CustomerChargeBackEntryName; 
      } 

      if (this.b.UsePricePlanDescription && !String.IsNullOrEmpty(currentAssets[x].PricePlanDescription)) 
      { 
       asset.Program = currentAssets[x].PricePlanDescription; 
       if (asset.Program.IndexOf(Environment.NewLine) > -1) 
       { 
        asset.Program = 
         asset.Program.Substring(0, asset.Program.IndexOf(Environment.NewLine)); 
       } 
      } 
      else 
      { 
       asset.Program = currentAssets[x].Program; 
      } 

      asset.BlackPct = -3; 
      asset.CyanPct = -3; 
      asset.MagentaPct = -3; 
      asset.YellowPct = -3; 

      Guid id = currentAssets[x].AssetID; 

      asset = SetCMYKvalues(asset, x); 

      BuilldImpressionsValues(currentAssets[x], ref asset); 
      fmpAssets.Add(asset); 
     } 
     //CommonGrid1.ApplyUserPreferences(); 

     //JavaScriptSerializer json = new JavaScriptSerializer(); 
     //string JsonArray = json.Serialize(fmpAssets); 
     //string ArrayDeclaration = string.Format("var arr = {0};", JsonArray); 
     //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "fmpAssets", ArrayDeclaration, true); 

     //assetEntities.Dispose(); 
     var totalAssetCount = assets.Count(); 
     var y = new PagedResult<FMPAsset>(); 
     y.Records = fmpAssets; 
     y.TotalCount = totalAssetCount; 
     return y; 

     // CommonGrid1.BindDataSource(SortByStatusName(fmpAssets)); 
    } 
} 

我收到一個錯誤說:

{ 「消息」:「無效的JSON原始: DESC 。「,」StackTrace「:」at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\ r \ n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.Deseri alizeInternal(的Int32 深度個)\ r \ n在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(字符串 輸入,的Int32 depthLimit, JavaScriptSerializer串行個)\ r \ n 在 System.Web.Script.Serialization。 JavaScriptSerializer.Deserialize(JavaScriptSerializer 串行器,字符串輸入,類型類型, 的Int32 depthLimit個)\ r \ n在 System.Web.Script.Services.RestHandler.GetRawParamsFromGetRequest(HttpContext的 上下文,JavaScriptSerializer 串行器,WebServiceMethodData methodData個)\ r \ n在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext的上下文)\ r \ n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext的 上下文,WebServiceMethodData methodData)」, 「ExceptionType」: 「System.ArgumentException」}

任何人都可以幫我解決這個問題嗎?

+0

你可以發佈JSON嗎?您可能還想通過JSLint運行它,以確保其有效(消息似乎表明這是問題)。 – wgpubs 2009-12-07 19:10:57

+0

我已經在JSLint中測試了這條消息。 這是說Missing semicolon.iam不知道在哪裏添加這個缺少的分號,因爲據我瞭解的語法是正確的 – xrx215 2009-12-07 19:20:19

回答

0

當然這裏是JSON商店

Ext.ns( 「FMP」);

FMP.AspNetJsonReader = Ext.extend(Ext.data.JsonReader, { 

     read: function(response) { 
      // Assuming ASP.NET encoding - Data is stored as 
      var json = response.responseText; 
      var o = Ext.decode(json); 
      if (!o) { 
       throw { message: "AspNetJsonReader.read: Json object not found" }; 
      } 
      if (!o.d) { 
       throw { message: "AspNetJsonReader.read: Root element d not found" }; 
      } 
      return this.readRecords(o.d); 

     } 

    }); 

    FMP.AspNetJsonStore = Ext.extend(Ext.data.GroupingStore, { 
     /** 
     * @cfg {Ext.data.DataReader} reader @hide 
     */ 
     constructor: function(config) { 
      FMP.AspNetJsonStore.superclass.constructor.call(this, Ext.apply(config, { 
       reader: new FMP.AspNetJsonReader(config) 
      })); 
     } 
    });