@model string

<div class="main">
    <div class="control-group clearfix">
        <div class="btn-group left">
            <button type="button" class="btn btn-default btn-sm ui-selectInvert">反选</button>
            <button type="button" class="btn btn-default btn-sm sure">确定</button>
        </div>
        <div class="right form-inline form-group-sm">
            <select id="sended" class="form-control ui-queryString ui-setName option_jj">
                <option value="username">按用户名</option>
                <option value="nickname">按昵称</option>
            </select>
            <input type="text" style="width:105px;" placeholder="请输入搜索关键词" class="form-control ui-queryString ui-keyword"/>
            <button type="button" class="btn btn-default btn-sm ui-queryString-search">确定</button>
            <span class="help-block"></span>
        </div>
    </div>
    <table border=0 cellpadding=0 cellspacing=0 width=100% class="table table-bordered">
        <thead><tr>
            <td align="center"  style="width:40px">选择</td>
            <td align="left">用户名</td>
        </tr>
        <tr class="item" id="item_{id}">
            <td align="center"  style="width:40px"><input type="checkbox" id="id" name="id" value="{id}"></td>
            <td align="left" class="username">{username}</td>
        </tr>


    </table>

    <div class="pagearea ui-pagePanel" data-custom-params="@Html.Raw(ViewBag.PageInfo)"></div>
</div>
<script type="text/javascript">
    document.write('<' + 'script type="text/javascript" src="' + baseUrl + '/js/list.js"></' + 'script>');
    var serverData=eval(@Html.Raw(Model??"null"));
    $(".item").ListBind(serverData);

    $(function () {
        var parent_username = parent.$("#username");
        var $item = $(".item");
        $(".sure").click(function () {
            var assignment = Assignment($item);
            var params = { valueField: "id", textField: "name" }
            parent_username.FillSelect(eval(assignment), params);
            var parentid = parentId(parent_username);//获取父级id
            var filterrepeat = filterRepeat(parentid, parent_username);//删除重复
            CloseDialog();
        })
        if (parent_username.children("option").length >= 1) {
            var parentid = parentId(parent_username);
            identicalId($item, parentid);//已加入表单的默认选中
        }
    })
    function filterRepeat(nums, parentObj) {
        var $option = parentObj.children("option");
        var num = nums.split(",");
        var nnum = num.sort();
        for (var s = 0; s < nnum.length; s++)
        {
            if (nnum[s] == nnum[s + 1]) {
                $option.eq(s).remove();
            }
        }
    }
    function parentId(parentObj) {
        var $option = parentObj.children("option");
        var str = "";
        $option.each(function (j) {
            if ($option.length - 1 == j) {
                str += $(this).val();
            } else {
                str += $(this).val() + ",";
            }
        })
        return str;
    }
    function identicalId(obj, nums) {
        var num = nums.split(",");
        obj.each(function (w) {
            var $this_val = $(this).find("input").val();
            for (var q = 0; q < num.length; q++) {
                if (num[q] == $this_val) {
                    $(this).find("input").attr({ "checked": "checked" });
                }
            }
        });
    }

    function Assignment(obj) {
        var str = "[";
        obj.each(function (i) {
            if ($(this).find("input").is(":checked")) {
                str += "{\"id\":\"";
                str += $(this).find("input:checked").val();
                str += "\",\"name\":\"";
                str += $(this).find(".username").text();
                if (obj.length == 1 || obj.length - 1 == i) {
                    str += "\"}";
                } else {
                    str += "\"},";
                }
            }
        })
        str += "]";
        return str;
    }

</script>


