2011-06-14 35 views
2

添加一行這裏是繼jqGrid的設置信息無法使用的jqGrid

jQuery(document).ready(function() { 
    jQuery("#list").jqGrid({ 
     url: '/TabMaster/GetGridData', 
     datatype: 'json', 
     mtype: 'GET', 
     colNames: ['col ID', 'First Name', 'Last Name'], 
     colModel: [ 
       { name: 'colID', index: 'colID', width: 100, align: 'left' }, 
       { name: 'FirstName', index: 'FirstName', width: 150, align: 'left', editable: true }, 
       { name: 'LastName', index: 'LastName', width: 300, align: 'left', editable: true }, 
      ], 
     pager: jQuery('#pager'), 
     rowNum: 4, 
     rowList: [1, 2, 4, 5, 10], 
     sortname: 'colID', 
     sortorder: "asc", 
     viewrecords: true, 
     multiselect: true, 
     imgpath: '/scripts/themes/steel/images', 
     caption: 'Tab Master Information' 
    }).navGrid('#pager', { edit: true, add: true, del: true }, 
    // Edit options 
      { 
      savekey: [true, 13], 
      reloadAfterSubmit: true, 
      jqModal: false, 
      closeOnEscape: true, 
      closeAfterEdit: true, 
      url: "/TabMaster/Edit/", 
      afterSubmit: function (response, postdata) { 
       if (response.responseText == "Success") { 
        jQuery("#success").show(); 
        jQuery("#success").html("Company successfully updated"); 
        jQuery("#success").fadeOut(6000); 
        return [true, response.responseText] 
       } 
       else { 
        return [false, response.responseText] 
       } 
      } 
     }, 
    // Add options 
      {}, 
    // Delete options 
      {url: '/TabMaster/Remove' } 
      ); 
}); 

是獲取數據並更新數據使用的jqGrid

#region "JQGrid Actions" 
      public JsonResult GetGridData(string sidx, string sord, int rows, int page) 
      { 
       int pageIndex = page; 
       int totalRecords = Convert.ToInt32(_tabmasterService.Count()); 
       int totalPages = (int)Math.Ceiling((float)totalRecords/(float)rows); 
       IQueryable<TabMasterViewModel> tabmasters = _tabmasterService.GetQueryTabMasterList(sidx, sord, rows, page); 
       var jsonData = new 
       { 
        total = totalPages, 
        page = page, 
        records = totalRecords, 
        rows = (from tm in tabmasters 
          select new 
          { 
           id = tm.colID, 
           cell = new string[] { tm.colID.ToString(), tm.FirstName, tm.LastName } 
          }).ToArray() 
       }; 
       return Json(jsonData, JsonRequestBehavior.AllowGet); 
      } 
      [AcceptVerbs(HttpVerbs.Post)] 
      public ActionResult Edit(int id, FormCollection updateExisting) 
      { 
       int _id = Convert.ToInt32(updateExisting["colID"]); 
       TabMasterViewModel editExisting = new TabMasterViewModel(); 
       editExisting = _tabmasterService.GetSingle(x => x.colID == id); 
       try 
       { 
        UpdateModel(editExisting); 
        _tabmasterService.Update(editExisting); 
        return Content("Success"); 
       } 
       catch (Exception e) 
       { 
        return Content(e.Message); 
       } 
      } 
[AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Remove(string id) 
     { 
      List<String> ids = new List<String>(id.Split(',')); 
      for (int i = 0; i < ids.Count; i++) 
      { 
       int deleteid = Convert.ToInt32(ids[i]); 
       TabMasterViewModel deleteExisting = new TabMasterViewModel(); 
       deleteExisting = _tabmasterService.GetSingle(x => x.colID == deleteid); 
       _tabmasterService.Delete(deleteExisting); 
      } 
      return RedirectToAction("Index"); 
     } 
#endregion 

注意的細節: -獲取和更新成功工作,但我在添加和刪除問題。 請有人幫我寫ADD和DELETE函數嗎?

+0

**無身體有任何解決方案... :))** – imdadhusen 2011-06-14 09:20:49

+0

**現在我可以刪除記錄!**我修改了刪除的代碼 – imdadhusen 2011-06-14 11:14:37

+0

爲什麼你不能添加行?你有錯誤嗎?你嘗試了什麼?你基本上就是這樣要求建立你的項目;-) – Rhapsody 2011-06-14 13:18:05

回答

1

這裏是添加,編輯完整的解決方案和DELETE * index.cshtml *

jQuery(document).ready(function() { 
     jQuery("#list").jqGrid({ 
      url: '/TabMaster/GetGridData', 
      datatype: 'json', 
      mtype: 'GET', 
      colNames: ['col ID', 'First Name', 'Last Name'], 
      colModel: [ 
        { name: 'colID', index: 'colID', width: 100, align: 'left', searchoptions: { sopt: ['eq', 'ne', 'cn']} }, 
        { name: 'FirstName', index: 'FirstName', width: 150, align: 'left', editable: true }, 
        { name: 'LastName', index: 'LastName', width: 300, align: 'left', editable: true }, 
       ], 
      pager: jQuery('#pager'), 
      rowNum: 4, 
      rowList: [1, 2, 4, 5, 10], 
      sortname: 'colID', 
      sortorder: "asc", 
      viewrecords: true, 
      multiselect: true, 
      imgpath: '/scripts/themes/steel/images', 
      caption: 'Tab Master Information' 
     }).navGrid('#pager', { edit: true, add: true, del: true }, 
     // Edit options 
      { 
      savekey: [true, 13], 
      reloadAfterSubmit: true, 
      jqModal: false, 
      closeOnEscape: true, 
      closeAfterEdit: true, 
      url: "/TabMaster/Edit/", 
      afterSubmit: function (response, postdata) { 
       if (response.responseText == "Success") { 
        jQuery("#success").show(); 
        jQuery("#success").html("Company successfully updated"); 
        jQuery("#success").fadeOut(6000); 
        return [true, response.responseText] 
       } 
       else { 
        return [false, response.responseText] 
       } 
      } 
     }, 
     // Add options 
      {url: '/TabMaster/Create', closeAfterAdd: true }, 
     // Delete options 
       {url: '/TabMaster/Remove' }, 
       { closeOnEscape: true, multipleSearch: true, 
        closeAfterSearch: true 
       } 
       ); 
    }); 

controller.cs

#region "JQGrid Actions" 
     public JsonResult GetGridData(string sidx, string sord, int rows, int page) 
     { 
      int pageIndex = page; 
      int totalRecords = Convert.ToInt32(_tabmasterService.Count()); 
      int totalPages = (int)Math.Ceiling((float)totalRecords/(float)rows); 
      IQueryable<TabMasterViewModel> tabmasters = _tabmasterService.GetQueryTabMasterList(sidx, sord, rows, page); 
      var jsonData = new 
      { 
       total = totalPages, 
       page = page, 
       records = totalRecords, 
       rows = (from tm in tabmasters 
         select new 
         { 
          id = tm.colID, 
          cell = new string[] { tm.colID.ToString(), tm.FirstName, tm.LastName } 
         }).ToArray() 
      }; 
      return Json(jsonData, JsonRequestBehavior.AllowGet); 
     } 
     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Edit(int id, FormCollection updateExisting) 
     { 
      int _id = Convert.ToInt32(updateExisting["colID"]); 
      TabMasterViewModel editExisting = new TabMasterViewModel(); 
      editExisting = _tabmasterService.GetSingle(x => x.colID == id); 
      try 
      { 
       UpdateModel(editExisting); 
       _tabmasterService.Update(editExisting); 
       return Content("Success"); 
      } 
      catch (Exception e) 
      { 
       return Content(e.Message); 
      } 
     } 
     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Remove(string id) 
     { 
      List<String> ids = new List<String>(id.Split(',')); 
      for (int i = 0; i < ids.Count; i++) 
      { 
       int deleteid = Convert.ToInt32(ids[i]); 
       TabMasterViewModel deleteExisting = new TabMasterViewModel(); 
       deleteExisting = _tabmasterService.GetSingle(x => x.colID == deleteid); 
       _tabmasterService.Delete(deleteExisting); 
      } 
      return RedirectToAction("Index"); 
     } 
     [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Create(FormCollection FormValue) 
     { 
      if (ModelState.IsValid) 
      { 
       TabMasterViewModel addNew = new TabMasterViewModel(); 
       addNew.FirstName = FormValue["FirstName"]; 
       addNew.LastName = FormValue["LastName"]; 
       _tabmasterService.Add(addNew); 
      } 
      return RedirectToAction("Index"); 
     } 
     #endregion