2013-10-08 36 views
0

我FormTemplate類在我的項目添加MVC中的jqGrid激活/關閉按鈕

public class FormTemplate : BaseEntity 
{ 
    public virtual string Name { get; set; } 
    public virtual DateTime? DateCreation { get; set; } 
    public virtual FormTemplateGroup Group { get; set; } 
    public virtual bool Active { get; set; } 
    public virtual FormTemplateStatus Status { get; set; } 
    public virtual IList<QuestionBlock> QuestionBlocks { get; set; } 
    public virtual bool IsFreeze { get; set; } 
} 

,我使用MVC的jqGrid http://mvcjqgrid.skaele.it/Home/Formatters
顯示頁面

@(Html.Grid("Grid") 
      .SetCaption("List") 
      .AddColumn(new Column("Name").SetLabel("Name")) 
      .AddColumn(new Column("GroupFor").SetLabel("Group")) 
      .AddColumn(new Column("DateCreation").SetLabel("Date")) 
      .AddColumn(new Column("Status").SetLabel("Status")).SetSortOnHeaderClick(false) 
      .AddColumn(new Column("Id").SetLabel("&nbsp;").SetCustomFormatter("buttonize").SetWidth(220).SetAlign(Align.Center)) 
      .SetAutoWidth(false) 
      .SetRowNumbers(true) 
      .SetUrl(Url.Action("FormTemplateGridData")) 
      .SetAutoWidth(true) 
      .SetRowNum(10) 
      .SetRowList(new[] { 5, 10, 15, 20 }) 
      .SetViewRecords(true) 
      .SetPager("Pager")) 

我對FormTemplates名單在我的頁面上不顯示IsFreeze屬性的值,但是如果IsFreeze == true需要添加「激活」按鈕,並且對於每個FormTemplate都需要禁用按鈕。

我嘗試添加檢查功能buttonize

function buttonize(cellvalue, options, rowobject) { 
     var result = '<input type="button" value="Edit" onclick="editTemplate(' + options.rowId + ')">' + '&nbsp;' 
      + '<input type="button" value="Delete" onclick="deleteTemplate(' + options.rowId + ')">' + '&nbsp;'; 

     if (isFreezeTemplate(rowobject[4])) { 
      result += '<input type="button" value="Activate" onclick="activateTemplate(' + options.rowId + ')">'; 
     } 
     else { 
      result += '<input type="button" value="Deativate" onclick="deactivateTemplate(' + options.rowId + ')">'; 
     } 
     return result; 
    } 

添加功能

function isFreezeTemplate(id) { 
     var check = $.post('@Url.Action("IsFreezeFormTemplate")', { id: id }); 
     return check; 
    } 

,並在控制器

[HttpPost] 
    public bool IsFreezeFormTemplate(int id) 
    { 
     var formTemplate = 
      FormTemplateRepository.Query() 
      .Where(ft => ft.Id == id) 
      .SingleOrDefault(); 

     if (formTemplate.IsFreeze == true) return true; 
     return false; 
    } 

添加,但我只得到激活按鈕所有FormTemplates上我的頁面。
如何解決它?

+0

您是否知道['$ .post'](http://api.jquery.com/jQuery.post/)是一個異步對象,它返回'jqXHR'對象,但不返回操作中返回的值?你應該有一個屬性,你發送到你的代碼,而不是... –

+0

你能解釋我該怎麼辦? – Heidel

回答

1

您可以在網格中添加一個隱藏列,通過rowobject參數從中讀取buttonize函數中的值。

.AddColumn(new Column("IsFreeze").SetHidden(true)) 

這樣你就不需要ajax請求。

+0

非常感謝您的幫助! – Heidel

+0

想想吧,你可能甚至不需要列。如果來自服務器的數據中包含IsFreeze字段,則可能也在rowobject參數中。 –

0

我建議你填寫FormTemplateGridData行動IsFreeze(如果不這樣做),並直接在您的buttonize功能使用它:

function buttonize(cellvalue, options, rowobject) { 
    var result = '<input type="button" value="Edit" onclick="editTemplate(' + options.rowId + ')">' + '&nbsp;' 
     + '<input type="button" value="Delete" onclick="deleteTemplate(' + options.rowId + ')">' + '&nbsp;'; 

    if (rowobject["IsFreeze"]) { 
     result += '<input type="button" value="Activate" onclick="activateTemplate(' + options.rowId + ')">'; 
    } 
    else { 
     result += '<input type="button" value="Deactivate" onclick="deactivateTemplate(' + options.rowId + ')">'; 
    } 

    return result; 
} 

編輯 - 動作代碼

爲了精確地發送到電網,下面是網格數據動作的代碼:

public JsonResult FormTemplateGridData() 
{ 
    var donnees = new 
    { 
     total = 2, 
     page = 1, 
     records = 2, 
     rows = new List<FormTemplate> 
     { 
      new FormTemplate { Id = 1, Active = true, Name = "first", IsFreeze = true }, 
      new FormTemplate { Id = 2, Active = true, Name = "second", IsFreeze = true }, 
      new FormTemplate { Id = 3, Active = false, Name = "last", IsFreeze = false } 
     } 
    }; 

    return Json(donnees); 
} 
+0

我在我的網格中添加了'.AddColumn(new Column(「IsFreeze」).SetHidden(true))',但在'buttonuze'函數中if(rowobject [「IsFreeze」])'不起作用。我檢查了控制檯,'rowobject'是Array,它包含例如'rowobject:Array [6] 0:「Template1」 1:「Group 2」 2:「10.10.2010 0:00:00」 3:「新」 4:「假」 5:「1」'但我不明白如何從這個數組中獲得第四個值。我嘗試了'rowobject [4]',但它不起作用。 – Heidel

+0

if(rowobject [4] ==「True」 –

+0

@Robin van der Knaap非常感謝,我已經找到了這種方式。 – Heidel