简体   繁体   English

jQuery查找表行,其中对象==表数据

[英]JQuery find table row where Object == Table Data

I have seen kinda similar problems but nothing that seems to work for me. 我已经看到了类似的问题,但似乎没有什么对我有用。

I have an AJAX call in my JQuery which returns a list of Group Names, I then, for each object in the list, need to look through the table for a match and change the class of one of the cells in the row. 我在JQuery中有一个AJAX调用,该调用返回一个组名列表,然后,对于列表中的每个对象,我需要遍历表以查找匹配项并更改行中单元格之一的类。 So basically: 所以基本上:

  1. AJAX returns JSon result with a List<string> with Group Names. AJAX返回带有组名的List<string> JSon结果。
  2. Look for the td where List object == TD.Text() . List object == TD.Text()位置查找td。
  3. Change a class in the table row (I know how to do this so not to important) 更改表格行中的类(我知道该怎么做,所以不重要)

Jquery jQuery的

$("#UserGridView_DXMainTable .dxgv").click(function () {
            //Get data to send to controller
                $.ajax({
                    url: '@Url.Action("GetUserGroups", "Home")',
                    data: { 'userLogin': insertText },
                    type: "post",
                    datatype: 'json',
                    cache: false,
                    success: function (data) {
                        if (!jQuery.isEmptyObject(data)) {
                            data.GroupNames.each(function () { <== Is this correct?
                             <====== I cannot seem to find a working solution for here.
                            })
                        }
                    }
                });
            });
    });

Controller 调节器

public JsonResult GetUserGroups(string userLogin)
        {
            if (userLogin != null)
            {
                Manager manager = new Manager();
                var details = manager.GetUserData(userLogin);
                var userGroupsID = details.GroupsId;
                List<string> GroupNames = new List<string>();
                for(var i = 0; i < userGroupsID.Count(); i++)
                {
                    var Gdetails = manager.GetGroupData(userGroupsID[i]);
                    var GName = Gdetails.Name;
                    GroupNames.Add(GName);
                }

            return Json(new { GroupNames = GroupNames }, JsonRequestBehavior.DenyGet);

        }

        return Json(new { }, JsonRequestBehavior.DenyGet);
    }

I'm open to all suggestions, let me know if you want to know anything else. 我愿意接受所有建议,如果您想了解其他任何信息,请告诉我。 Thanks 谢谢

EDIT: 编辑:

Example of returned data: 返回的数据示例:

data.GroupNames = ["Default", "Normal User", "Manager" etc..] data.GroupNames = [“默认”,“普通用户”,“管理员”等。]

So if I understand correctly, GroupNames is an array of text and you are looking to see if this text matches any text in the td's? 因此,如果我理解正确, GroupNames是一个文本数组,您正在寻找该文本是否与td中的任何文本匹配? If then try this. 如果可以,请尝试此操作。

    $tds = $('td');
    $.each(data.GroupNames, function (index, val) {
        console.log(val + ' ' + index);
        $tds.each(function(){
            if($(this).text() === val){
                console.log('found');
            }
        });
    });

Demo 演示

The algorithm can be optimized by removing a td everytime it matches (unless a td can match multiple GroupNames) 可以通过在每次匹配时删除一个td来优化该算法(除非一个td可以匹配多个GroupName)

Hi Please change in controller from 嗨,请更改控制器

return Json(new { }, JsonRequestBehavior.DenyGet); 返回Json(new {},JsonRequestBehavior.DenyGet); --> return Json(new { }, JsonRequestBehavior.AllowGet); -> return Json(new {},JsonRequestBehavior.AllowGet);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM