如何在ajax脚本中使用IF语句?

时间:2014-06-03 02:18:33

标签: javascript jquery ajax

我想在日期为< = datetime.now时将我的日期时间字体颜色变为红色 任何想法如何把if语句放在ajax中?

这是我的代码

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
    $('#customerTable').dataTable({
        "bServerSide" : true,
        "sAjaxSource" : "Customer/AjaxHandler",
        "bProcessing" : true,
        "aoColumns": [
            {"sName": "CustomerId", "bVisible":false},
            {"sName": "NamaPerusahaan",
             "bSearchable": true,
             "bSortable": true,
             "fnRender": function (oObj){
                 return '<a href="/Customer/Details/' +
                     oObj.aData[0] + '">' + oObj.aData[1] + '</a>';
             }
            },
            {"sName": "Alamat1"},
            {"sName": "Telephone"},
            //below is the datetime
            {"sName": "TglNonAktif"},
            {"sName": "Edit",
             "bSearchable": false,
             "bSortable": false,
             "fnRender": function (oObj){
                 return '<a href="/Customer/Edit/' + oObj.aData[0] + '" class="btn mini blue"><i class="icon-edit"></i>Edit</a>';
             }
            },
            {"sName": "Delete",
             "bSearchable": false,
             "bSortable": false,
             "fnRender": function (oObj){
                 return '<a href="/Customer/Edit/' + oObj.aData[0] + '" class="btn mini red"><i class="icon-trash"></i>Delete</a>';
             }
            }
        ]
    });
});
</script>

这是控制器

public ActionResult AjaxHandler(JkueryDataTableParamModel param)
{
IEnumerable<Customer> filteredCustomers;
if (!string.IsNullOrEmpty(param.sSearch)
{
var nameFilter = Convert.ToString(Reguest["sSearch_1"]);
var isAlamatSearchable = Convert.ToBoolean(Reguest["sSearch_2"]);
var townFilter = Convert.ToString(Reguest["sSearch_3"]);

filteredCustomers = db.Customers.Where(c => isNamaPerusahaanSearchable && c.NamaPerusahaan.ToLower().Contains(param.sSearch.ToLower())|| isNamaPerusahaanSearchable && c.Alamat1.ToLower().Contains(param.sSearch.ToLower())|| isTelephoneSearchable && c.Telephone.ToLower().Contains(param.sSearch.ToLower()));
}
else
{
filteredCustomers = db.Customers;
}
var isNameSortable = Convert.ToBoolean (Reguest["bSortable_1"]);
var isAddresSortable = Convert.ToBoolean (Reguest["bSortable_2"]);
var isTownSortable = Convert.ToBoolean (Reguest["bSortable_3"]);
var sortColumnIndex = Convert.ToInt32 (Reguest["iSortCol_0"]);

Func<Customer, Obkect> orderingFunction;
switch(sortColumnIndex)
{
case 1:
orderingFunction = (x => isNameSortable ? x.NamaPerusahaan:"");
default:
orderingFunction = (x => isNameSortable ? x.NamaPerusahaan:"");
break;
}

var sortDirection = Reguest["sSortDir_0"];
if (sortDirection == "asc")
filteredCustomers = filteredCustomers.OrderBy (orderingFunction);
else
filteredCustomers = filteredCustomers.OrderByDescending(orderingFunction);

var displayedCustomers = filteredCustomers.Skip(param.iDisplayStart).Take(param.iDisplayLength);
var result = from c in displayedCustomers select new [] { Convert.ToString(c.CustomerId), c.NamaPerusahaan, c.Alamat1, c.Telephone, c.TglNonAktif.ToShortDateString(), "Edit", "Delete"};

return Json (new
{ 
sEcho = param.sEcho,
iTotalRecords = db.Customers.Count(),
iTotalDisplayRecords = filteredCustomers.Count(),
aaData = result
},
JsonReguestBehavior.AllowGet);
}

任何答案都可能有用:D 问候

1 个答案:

答案 0 :(得分:0)

如果您正在构建JavaScript对象文字并希望一个字段根据动态条件具有不同的值,那么您可以:

对于简单条件/ if语句 - 使用三元运算符:

{
    "color": (date <= datetime) ? "red" : "black",
    ...
}

对于更复杂的条件,具有两个以上选项的东西等,使用匿名函数:

{
    "color": (function() { if (...) return "red"; else return "black"; })(),
    ...
}

括号之间的代码声明函数内联,然后立即对其进行求值。评估根据函数体返回任意结果。结果将只是与给定对象键对应的值。


如果您回答@ silver关于日期(和颜色)变量的位置的问题,我可以更新JSON / ajax代码。