如何使选择按钮仅在列值与用户ID相同时才会出现?

时间:2017-05-09 04:19:34

标签: c# jquery ajax asp.net-mvc

如何使“选择”按钮仅在“医生”列值与“用户ID”相同时出现?例如,当前用户ID是Dr.One。所以我希望它只有那个博士输入的行才会显示选择按钮,但是将其隐藏在具有其他博士名称的行中。

<tr>
  <td>No</td>
  <td>Description</td>
  <td>Doctor</td>
  <td><button>Select</button></td>
</tr>
<tr> // This is the row that is entered by the current User ID
  <td>1</td>
  <td>Anything</td>
  <td>Dr.One</td>
  <td>Select</td>
</tr>
<tr> 
  <td>2</td>
  <td>Anything</td>
  <td>Dr.Two</td>
  <td></td>
</tr>

目前我正在使用AJAX加载表格,我试图将if-else放在<td>之间,但它会出现语法错误。

      $.ajax({
            url: '/Consultation/GetMyList',
            type: 'GET',
            data: $('#frmMyList').serialize(), // it will serialize the form data
            dataType: 'json',
            success: function (data) {

                var rowNum = 0;
                for (var i = 0; i < data.length; i++) {
                    rowNum += 1;
                    $("#LoadMyListTable tbody").append("<tr Class='odd gradeX subsequentmyList '>" +
                                "<td>" + rowNum + "</td>" +
                                "<td>" + data[i].Description + "</td>" +
                                "<td>" + data[i].UserID + "</td>" +
                                "<td style='text-align: center; vertical-align: middle;'><button type='button' class='select btn btn-default' data-refid='" + data[i].RefID + "'>Select</button></td>" +
                    "</tr>");
                }

            },
            error: function () {
                alert('Ajax Submit Failed ...');
            }
        }); // end ajax

GetMyList方法

public List<Consultation> GetMyList(String paid, string UserID)
    {
        using (DbContext dbb = new DbContext())
        {
            var query = from p in dbb.patient
                        join pv in dbb.patient_visit on p.paid equals pv.paid
                        join d in dbb.diagnosis on pv.pvid equals d.pvid
                        join u in dbb.users on d.enteredby equals u.UserID
                        where p.paid.Equals(paid)
                        orderby d.entereddt descending
                        select new { d.RefID, d.Description, u.UserID, d.enteredby };

            foreach (var item in query)
            {
                Consultation List = new Consultation();
                List.RefID = item.RefID;
                List.UserID = item.UserID;
                List.Description = item.Description;
                List.enteredby = item.enteredby;
                LoadMyList.Add(List);
            }

            return LoadMyList;
        }

    }

1 个答案:

答案 0 :(得分:2)

bool SbowButton视图模型中包含Consultation属性(或者只传回一组匿名对象。

var query = from p in dbb.patient
    .....
var data = query.Select(x => new
{
    RefID = x.RefID,
    UserID = x.UserID,
    Description = x.Description,
    ShowButton = x.UserID == yourCurrentUserID // not sure of your logic here    
});
return Json(data, JsonRequestBehavior.AllowGet);

然后在ajax成功回调中,有条件地构建你的html

var table = $('#LoadMyListTable tbody'); // cache it
$.ajax({
    ....
    success: function (data) {
        $.each(data, function(index, item) {
            var row = $('<tr></tr>').addClass('odd gradeX subsequentmyList');
            row.append($('<td></td>').text(item.RefID));
            row.append($('<td></td>').text(item.Description));
            row.append($('<td></td>').text(item.UserID));
            var buttonCell = $('<td></td>');
            if (item.ShowButton) {
                var button = $('<button></button>').attr('type', 'button').addClass('select btn btn-default').data('refid', item.RefID).html('Select');
                buttonCell.append(button);
            }
            row.append(buttonCell);
            table.apend(row);
        });
    }
    ....
});